Ok so I want to have the sequence 0,1,0,1,0,1

Ok so I want to have the sequence 0,1,0,1,0,1,...
Its generated by this: 1/2 (1 + (-1)^n)

Now I want the Sequence 0,1,2,0,1,2,0,1,2,...
Wolfram Alpha tells me the function to get this is a_n = (n + 2) mod 3
BUT in my program I can't to use MOD or IF(F) because it slows stuff down too much, I want a nice function like I had before. Recursion is no problem. Wat do?

Other urls found in this thread:

mathworks.com/matlabcentral/newsreader/view_thread/9523
twitter.com/SFWRedditGifs

a mod n = a - (n * int(a/n))

Thanks for the answer but casting a division which could result into a float/double into an integer is just as bad using mod. If you want to use division it have to result in an integer. No casting allowed.

Are you allowed to set the first three as 0, 1, 2 and then say: a_n = n-3 ?

You can't make a sequence with a cycle greater than 2 without division and modular arithmetic in the real numbers. If you want to use exponentiation to create a sequence that cycles after 3 steps, then you need complex numbers. You can use the formula [(-0.5 + 0.5*i*sqrt(3))^(n-1) - (-0.5 - 0.5*i*sqrt(3))^(n-1)]/(i*sqrt(3)) + 1. This is assuming your index starts at 0.

trips of truth.

good solution

but a modular solution may be slower than that route

of course, the division by i*sqrt(3) is the same as multiplication by 1/(i*sqrt(3)), so this is still just multiplication.

excuse me:
modular will probably be quicker than the one with all the imaginary square roots

>of course, the root operation i*sqrt(3) is the same as exponent i*3**(0.5), so this is still just exponentiation
Yes, I know basic mathematics as well user

Depends on the hardware. Two and four quadrant multiplication can be done very fast.