What math do I need to convert numbers according to this table? - math

Given an X, what math is needed to find its Y, using this table?
x
y
0
1
1
0
2
6
3
5
4
4
5
3
6
2
This is a language agnostic problem. I can't just store the array, and do the lookup. The input will always be the finite set of 0 to 6. It won't be scaling later.

This:
y = (8 - x) % 7
This is how I arrived at that:
x 8-x (8-x)%7
----------------
0 8 1
1 7 0
2 6 6
3 5 5
4 4 4
5 3 3
6 2 2

int f(int x)
{
return x["I#Velcro"] & 7;
}

0.048611x^6 - 0.9625x^5 + 7.340278x^4 - 26.6875x^3 + (45 + 1/9)x^2 - 25.85x + 1
Sometimes the simple ways are best. ;)

It looks like:
y = (x * 6 + 1) % 7

I don't really like the % operator since it does division so:
y = (641921 >> (x*3)) & 7;
But then you said something about not using lookup tables so maybe this doesn't work for you :-)
Update:
Since you want to actually use this in real code and cryptic numbers are not nice, I can offer this more maintainable variant:
y = (0x2345601 >> (x*4)) & 15;

Though it seems a bunch of correct answers have already appeared, I figured I'd post this just to show another way to have worked it out (they're all basically variations on the same thing):
Well, the underlying pattern is pretty simple:
x y
0 6
1 5
2 4
3 3
4 2
5 1
6 0
y = 6 - x
Your data just happens to have the y values shifted "down" by two indices (or to have the x values shifted "up").
So you need a function to shift the x value. This should do it:
x = (x + 5) % 7;
Resulting equation:
y = 6 - ((x + 5) % 7);

Combining the ideas in Dave and Paul's answer gives the rather elegant:
y = (8 - x) % 7`
(though I see I was beaten to the punch with this)

unsigned short convertNumber(unsigned short input) {
if (input <= 1) { return !input; } //convert 0 => 1, 1 => 0
return (8-input); //convert 2 => 6 ... 6 => 2
}

Homework?
How about:
y = (x <= 1 ? 1 : 8) - x

and no, i dont/cant just store the array, and do the lookup.
Why not?
yes, the input will always be the finite set of 0 to 6. it wont be scaling later.
Just use a bunch of conditionals then.
if (input == 0) return 1;
else if (input == 1) return 0;
else if (input == 2) return 6;
...
Or find a formula if it's easy to see one, and it is here:
if (input == 0) return 1;
else if (input == 1) return 0;
else return 8 - input;
Here's a way to avoid both modulo and conditionals, going from this:
y = (8 - x) % 7
We know that x % y = x - floor(x/y)*y
So we can use y = 8 - x - floor((8 - x) / 7) * 7

What about some bit-fu ?
You can get the result using only minus, logical operators and shifts.
b = (x >> 2) | ((x >> 1) & 1)
y = ((b << 3)|(b ^ 1)) - x

Related

Finding X & Y based off of Index

Good day all
I am having a math issue, it may be due to the lack of sleep but I am totally drawing a blank.
I need to find the x and y coordinates based off of the index.
So I know the width of the grid, the height and the index. But I dont know the X and Y coordinates. i need build a formula to get that data.
For example. I know the index of 9. Through a formula i need to be able to get the number 4 for X and 2 for Y
int numOfRows = 4
int numOfCols = 5
int index = 13
int X = ?
int Y = ?
//perform math magic
x = 4
y = 3
It is very simple:
public static void foo(int i) {
int x = i % 5 + 1;
int y = i / 5 + 1;
}
It gets much easier if you start counting with 0:
| 0 1 2 3 4
-----------------
0| 0 1 2 3 4
1| 5 6 7 8 9
2|10 11 12 13 14
3|...
4|
Let a be the number in the grid and numberOfCols the number of columns (5 in this example).
In that case, it's plain to see that
the row number is a / numberOfCols (without remainder) and
the column number is a modulo numberOfCols.
You can reduce your case to this case by adding 1 to the resulting row/col numbers.

Subtraction operation using only increment, loop, assign, zero

I am trying to build up subtraction, addition, division, multiplication and other operations using only following ones:
incr(x) - Once this function is called it will assign x + 1 to x
assign(x, y) - This function will assign the value of y to x (x = y)
zero(x) - This function will assign 0 to x (x = 0)
loop X { } - operations written within brackets will be executed X times
Using following rules it is straight forward to implement addition (add) like this:
ADD (x, y) {
loop X {
y = incr (y)
}
return y
}
However, I'm struggling to implement subtraction. I think that all the other needed operations could be completed using subtraction.
Any hint will be very appreciated.
Stephen Cole Kleene devised a way to perform integer subtraction using integer addition. However, it assumes that you cannot have negative integers. For example:
0 - 1 = 0
1 - 1 = 0
2 - 1 = 1
3 - 1 = 2
4 - 1 = 3
5 - 2 = 3
6 - 3 = 3
6 - 4 = 2
6 - 5 = 1
6 - 6 = 0
6 - 7 = 0
In your question, you implemented the addition operation using the increment operation.
Similarly, you can implement the subtraction operation using the decrement operation as follows:
sub(x, y) {
loop y
{ x = decr(x) }
return x
}
Now, all we need to do is implement the decrement operation.
This is where the genuis of Kleene shines:
decr(x) {
y = 0
z = 0
loop x {
y = z
z = incr(z)
}
return y
}
Here we've used all the four operations. This is how it works:
We have two base cases, y (the base case for 0) and z (the base case for 1):
y = 0 - 1 = 0
z = 1 - 1 = 0
Hence, we initialize them both to 0.
When x is 0 we run the loop 0 times (i.e. never) and then we simply return y = 0.
When x is 1 then we run the loop once, assign y = z and then simply return y = z = 0.
Notice that every time we run the loop y holds the result of the current iteration while z holds the result of the next iteration. This is the reason why we require two base cases. The decrement function is not a continuous function. It is a piecewise function:
decr(0) = 0
decr(n + 1) = n
Kleene realized this when he went to the dentist and the dentist extracted two of his teeth. He was frustrated while trying to solve this very problem and when the dentist extracted two of his teeth he realized that he required two base cases.

How to do "for all" in sum notation in Julia/JuMP

I am trying to add constraints to a linear optimization problem in Julia using JuMP. I am using the sum{} function however, I am having trouble with some of the constraints. Does anyone know how to write "for all" in JuMP (the upside down A)? Here is the code I have so far:
using JuMP
m = Model()
c= [3 5 2 ; 4 3 5 ; 4 5 3 ; 5 4 3 ; 3 5 4]
#variable(m, x[i=1:5,j=1:3] >= 0)
#objective(m,Min,sum{c[i,j]*x[i,j],i=1:5,j=1:3})
for i=1:5
#constraint(m, sum{x[i,j],i,j=1:3} <= 480)
end
What I am trying to get is this:
I am trying to use the for loop as a substitute of "for all i from 1 to 5" however I keep getting errors. Is there another way to do this?
In mathematical notation, you sum across i, and do so for each j.
In Julia/JuMP, you can think of "∀" as being a for loop ("for all"),
and a "Σ" as being a sum{ }:
using JuMP
m = Model()
c= [3 5 2;
4 3 5;
4 5 3;
5 4 3;
3 5 4]
# x_ij >= 0 ∀ i = 1,...,5, j = 1,...,3
#variable(m, x[i=1:5,j=1:3] >= 0)
#objective(m,Min,sum{c[i,j]*x[i,j],i=1:5,j=1:3})
# ∀j = 1,...,3
for j in 1:3
#constraint(m, sum{x[i,j],i=1:5} <= 480)
end

Math: What is the Equation that on increasing the integer x returns an alternate of 0 and 1?

What is the Equation that on increasing the integer x returns an alternate of 0 and 1
example
x = 22
result 1
x = 23
result 0
x = 24
result 1
Based on the example data, it would be modulo 2. Assuming x is an int (and C/C++/C#):
(x + 1) % 2;
In C or C++ this would be
int y = (x+1)%2;
mathematically,
y = (x+1) modulo 2
It's called modulo. You can use mod by 2 after adding one in the value.
x = 22
result = (x+1) modulo 2
In programming languages, it's often called %:
x = 22
result = (x+1) % 2 //<< result 1
x = 23
result = (x+1) % 2 //<< result 0
and so on..

Summing elems of array using binary recursion

I wasn't starting to understand linear recursion and then I thought I practice up on sorting algorithms and then quick sort was where I had trouble with recursion. So I decided to work with a simpler eg, a binary sum that I found online. I understand that recursion, like all function calls, are executed one # a time and not at the same time (which is what multi-threading does but is not of my concern when tracing). So I need to execute all of recursive call A BEFORE recursive call B, but I get lost in the mix. Does anyone mind tracing it completely. The e.g. I have used of size, n = 9 where elems are all 1's to keep it simple.
/**
* Sums an integer array using binary recursion.
* #param arr, an integer array
* #param i starting index
* #param n size of the array
* floor(x) is largest integer <= x
* ceil(x) is smallest integer >= x
*/
public int binarySum(int arr[], int i, int n) {
if (n == 1)
return arr[i];
return binarySum(arr, i, ceil(n/2)) + binarySum(arr,i + ceil(n/2), floor(n/2));
}
What I personally do is start with an array of size 2. There are two elements.
return binarySum(arr, i, ceil(n/2)) + binarySum(arr,i + ceil(n/2), floor(n/2)) will do nothing but split the array into 2 and add the two elements. - case 1
now, this trivial starting point will be the lowest level of the recursion for the higher cases.
now increase n = 4. the array is split into 2 : indices from 0-2 and 2-4.
now the 2 elements inside indices 0 to 2 are added in case 1 and so are the 2 elements added in indices 2-4.
Now these two results are added in this case.
Now we are able to make more sense of the recursion technique, some times understanding bottom up is easier as in this case!
Now to your question consider an array of 9 elements : 1 2 3 4 5 6 7 8 9
n = 9 => ceil(9/2) = 5, floor(9/2) = 4
Now first call (top call) of binarySum(array, 0, 9)
now n = size is not 1
hence the recursive call....
return binarySum(array, 0, 5) + binarySum(array, 5, 4)
now the first binarySum(array, 0 ,5) operates on the first 5 elements of the array and the second binarySum(array,5,4) operates on the last 4 elements of the array
hence the array division can be seen like this: 1 2 3 4 5 | 6 7 8 9
The first function finds the sum of the elements: 1 2 3 4 5
and the second function finds the sum of the elements 6 7 8 9
and these two are added together and returned as the answer to the top call!
now how does this 1+2+3+4+5 and 6+7+8+9 work? we recurse again....
so the tracing will look like
1 2 3 4 5 | 6 7 8 9
1 2 3 | 4 5 6 7 | 8 9
1 2 | 3 4 | 5 6 | 7 8 | 9
[1 | 2]___[3]___[4 5]___[6 7]___[8 9]
Till this we are fine..we are just calling the functions recursively.
But now, we hit the base case!
if (n == 1)
return arr[i];
[1 + 2]____[3]____[4 + 5]____[6 + 7]____[8 + 9]
[3 + 3] ____ [9] ____[13 + 17]
[6 + 9] [30]
[15 + 30]
[45]
which is the sum.
So for understanding see what is done to the major instance of the problem and you can be sure that the same thing is going to happen to the minor instance of the problem.
This example explains binary sum with trace in java
the trace is based on index of array , where 0 - is yours starting index and 8 is length of the array
int sum(int* arr, int p, int k) {
if (p == k)
return arr[k];
int s = (p + k) / 2;
return sum(arr, p, s) + sum(arr, s + 1, k);
}

Resources