Page 1 of 1

### Flag without an IF condition

Posted: Mon Dec 14, 2015 4:57 pm
If I want a flag that goes like 1, -1, 1, -1, 1, ... in every iteration of a FOR loop, I could use the following equation.

t *= -1, starting with t = 1.

For 0, 1, 0, 1, 0, 1, ..., I can use,

t = 1 - t, starting with t = 0

Now if I want something like 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, ....(three 1's followed by three -1's and so on..) what should I be using. I've been trying to do it without an IF condition and I cant figure out anything.

Any help would be greatly appreciated.

Thank you

### Re: Flag without an IF condition

Posted: Mon Dec 14, 2015 6:34 pm
Use the complex i.

### Re: Flag without an IF condition

Posted: Mon Dec 14, 2015 6:50 pm
Thanks nicolas. But using complex i in C will be slightly tough right? Is there a relatively simpler way?

### Re: Flag without an IF condition

Posted: Mon Dec 14, 2015 8:34 pm
Or just manage an array with your four numbers in it.

### Re: Flag without an IF condition

Posted: Mon Dec 14, 2015 10:48 pm
There are many options:
• A polynomal modulo a period and increment modulo this period
• Array with values and cyclic index (using modulo)
• Linear recurrences
• Linear recurrences using matrix and vectors
• For periods 2, 4, 16, 32, 64: bit-rotation
• ...
-- v6ph1

### Re: Flag without an IF condition

Posted: Tue Dec 15, 2015 12:41 am
Thanks v6ph1 for the suggestions.

Linear recurrence is the one I was looking for I suppose.

Can you please explain your first and second option with examples? I cant understand them. Thanks.

### Re: Flag without an IF condition

Posted: Tue Dec 15, 2015 7:28 am
MuthuVeerappanR wrote:Thanks v6ph1 for the suggestions.

Linear recurrence is the one I was looking for I suppose.

Can you please explain your first and second option with examples? I cant understand them. Thanks.
Presumably something like this:

Code: Select all

int i,x,s;
for( i=0; i<100; i++){
x=i%6;
s = ((((6*x - 75)*x + 320)*x - 525)*x + 274 )*x/60 - 1;
printf("%d\n",s);
}

Code: Select all

int i,s;
int array[] = {1,1,1,-1,-1,-1};
for( i = 0; i<100; i++){
s = array[i%6];
printf("%d\n",s);
}

### Re: Flag without an IF condition

Posted: Tue Dec 15, 2015 11:12 am
Thanks jaap..

### Re: Flag without an IF condition

Posted: Wed Dec 30, 2015 5:24 pm
Is there a reason that you're trying to avoid using selection?

Here is an alternative solution that it is easily customisable.

Code: Select all

ones = 3
for i in range(20):
flag = -(2 * ((i % (2 * ones)) // ones) - 1)
print(flag)
#endfor
If "ones" is set to 3 then this is how it works...

Code: Select all

i = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ...
i % 6 = 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, ...
(i % 6) // 3 = 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, ...
2 * ((i % 6) //3) = 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 2, 2, 0, 0, ...
2 * (((i % 6) //3) - 1 = -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, ...
-(2 * (((i % 6) //3) - 1) = 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, ...

### Re: Flag without an IF condition

Posted: Sun Jan 03, 2016 10:22 pm
Nice euler.. Thank you very much..