[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Ok so I want to have the sequence 0,1,0,1,0,1,... Its generated

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 23
Thread images: 1

File: homo_sompson.png (553KB, 720x540px) Image search: [Google]
homo_sompson.png
553KB, 720x540px
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?
>>
>>8947238
a mod n = a - (n * int(a/n))
>>
>>8947266
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.
>>
>>8947383
Are you allowed to set the first three as 0, 1, 2 and then say: a_n = n-3 ?
>>
>>8947238
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.
>>
>>8947444
trips of truth.

good solution

but a modular solution may be slower than that route
>>
>>8947444
of course, the division by i*sqrt(3) is the same as multiplication by 1/(i*sqrt(3)), so this is still just multiplication.
>>
>>8947454
excuse me:
modular will probably be quicker than the one with all the imaginary square roots
>>
>>8947455
>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 anon
>>
>>8947459
Depends on the hardware. Two and four quadrant multiplication can be done very fast.
>>
>>8947444
>>8947238
Overall, this method isn't very good. Testing this out in Matlab, it is apparent that every time the sequence is supposed to be 0, the floating point representation contains some small remnants due to either the cancellation from subtraction, or because the finite precision machine can't actually do computations with the true value of sqrt(3). The error becomes noticeable for all terms of the sequence at the 200th term, or before. If this process were to be implemented, then it would need to be symbolic, and recursively defined. Really, there is nothing wrong with if statements, what I suspect is happening here is that you are using naive numerical methods. Take the tail recursion method, for example. The tail recursion method is a method where the initial call contains 0, 1, 2 and an integer n. When n gets incremented by -1, then the dummy's value copies the leftmost slot, the leftmost slot copies the middle slot, the middle slot copies the rightmost slot, and the rightmost slot copies the dummy. This method is shit because there are exponentially more steps per symbol in the input (because n is expressed in base 2 or greater). To get around this, we can use a more advanced method. If you want to find n mod 3, then we will use a tripling method. Essentially what we are going to do is compute powers of 3 (in base 2) and compare the number to the number n until we find a power of 3 that is larger than, or equal to n. Once we find it, then we will subtract the largest possible power of 3 as many times as possible (0, 1 or 2 times), then divide down to the next largest power of 3 (better yet, you could just keep a table of powers of 3 stored in memory, and just reference it), and subtract it as many times as possible, and so on and so forth, until we are down to 3^1. When we can't subtract any more copies of 3^1, then the remainder is n mod 3. This method is way better, because the number of steps goes up linearly, as the input size increases.
>>
>>8947562
note, computing 3^(n+1) using just 3^n can be done in 2 addition steps, so that part of the algorithm works in linear time.
>>
>>8947444
>You can't make a sequence with a cycle greater than 2 without division and modular arithmetic in the real numbers.

Yes I can you fucking piggot
(-1)^ [ n(n+1)/2]
>>
>>8947573
(a_n, a_n+1) = ((-1)^n, (-1)^n), n starts at 1
The sequence still has cycle 2
>>
>>8947595
*a_2n, and a_2n+1
>>
>>8947238
a(n) = 3 - a(n-1) - a(n-2) for n > 1.
a(n) = 1 - 0^((-1)^(n/3)-(-1)^n) + 0^((-1)^((n+1)/3)+(-1)^n).
a(n) = 1 + (-1)^((2*n+4)/3)/3 + (-1)^((-2*n-4)/3)/3 + 2*(-1)^((2*n+2)/3)/3 + 2*(-1)^((-2*n-2)/3)/3.
>>
>>8947602
>>8947566
>>8947495
>>8947459

You know guys, in the context of how a computer actually works, it's easier to generate the sequence 0,1,2,0,1,2... than it is to generate the sequence n = 1,2,3,4,5,6...
>>
>>8947619
For example.

Take n
Read the last two bits of n = 00,01,11,00,01
Done
>>
>>8947625
Oh right. 3.

Well fuck n, use a shift register jesus
>>
>>8947238
>I can't to use MOD or IF(F) because it slows stuff down too much
These would normally be very fast. First, make sure you really know what's fast and slow by actually testing it.
>>
>>8947562
>Matlab

Try some of these:
https://www.mathworks.com/matlabcentral/newsreader/view_thread/9523
>>
>>8947238
a_n = 1 + (1-2*cos(2*Pi*(n-1)/3)) * sin(2*Pi*(n-1)/3)) / sqrt(3)
>>
>>8947562
The tripling method is kind of shit because you have to store the powers of 3, but there is a better way. Because the machine works in binary, the doubling method works way better because dividing by 2 takes almost no time. In particular, you would start with 3, compare 3 to n, and if 3 is too small, you double it. Compare again, and, if it is too small, double it. Once the doubling method gets a larger value than n, then the division step will only involve removing a 0 from the end of the number.
Thread posts: 23
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.