## Flag without an IF condition

Primes, divisors, arithmetic, number properties, ...
MuthuVeerappanR
Posts: 364
Joined: Sun Mar 22, 2015 2:30 pm
Location: India
Contact:

### Flag without an IF condition

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

It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

nicolas.patrois
Posts: 117
Joined: Fri Jul 26, 2013 3:54 pm
Contact:

### Re: Flag without an IF condition

Use the complex i.

MuthuVeerappanR
Posts: 364
Joined: Sun Mar 22, 2015 2:30 pm
Location: India
Contact:

### Re: Flag without an IF condition

Thanks nicolas. But using complex i in C will be slightly tough right? Is there a relatively simpler way?

It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

nicolas.patrois
Posts: 117
Joined: Fri Jul 26, 2013 3:54 pm
Contact:

### Re: Flag without an IF condition

Or just manage an array with your four numbers in it.

v6ph1
Posts: 116
Joined: Mon Aug 25, 2014 6:14 pm

### Re: Flag without an IF condition

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

MuthuVeerappanR
Posts: 364
Joined: Sun Mar 22, 2015 2:30 pm
Location: India
Contact:

### Re: Flag without an IF condition

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.

It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

jaap
Posts: 540
Joined: Tue Mar 25, 2008 3:57 pm
Contact:

### Re: Flag without an IF condition

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);
}
Last edited by jaap on Wed Dec 23, 2015 10:25 am, edited 2 times in total.

MuthuVeerappanR
Posts: 364
Joined: Sun Mar 22, 2015 2:30 pm
Location: India
Contact:

### Re: Flag without an IF condition

Thanks jaap..

It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

euler
Posts: 3113
Joined: Sun Mar 05, 2006 4:49 pm
Location: Cheshire, England
Contact:

### Re: Flag without an IF condition

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, ...

impudens simia et macrologus profundus fabulae

MuthuVeerappanR
Posts: 364
Joined: Sun Mar 22, 2015 2:30 pm
Location: India
Contact:

### Re: Flag without an IF condition

Nice euler.. Thank you very much..

It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.