Flag without an IF condition

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

Flag without an IF condition

Post by MuthuVeerappanR » 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
Image
It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

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

Re: Flag without an IF condition

Post by nicolas.patrois » Mon Dec 14, 2015 6:34 pm

Use the complex i.
Image

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

Re: Flag without an IF condition

Post by MuthuVeerappanR » 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?
Image
It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

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

Re: Flag without an IF condition

Post by nicolas.patrois » Mon Dec 14, 2015 8:34 pm

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

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

Re: Flag without an IF condition

Post by v6ph1 » 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
Image

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

Re: Flag without an IF condition

Post by MuthuVeerappanR » 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.
Image
It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

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

Re: Flag without an IF condition

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

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

Re: Flag without an IF condition

Post by MuthuVeerappanR » Tue Dec 15, 2015 11:12 am

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

User avatar
euler
Administrator
Posts: 3075
Joined: Sun Mar 05, 2006 4:49 pm
Location: Cheshire, England
Contact:

Re: Flag without an IF condition

Post by euler » 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, ...
Image
impudens simia et macrologus profundus fabulae

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

Re: Flag without an IF condition

Post by MuthuVeerappanR » Sun Jan 03, 2016 10:22 pm

Nice euler.. Thank you very much..
Image
It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

Post Reply