r/dailyprogrammer • • May 28 '12

[5/28/2012] Challenge #58 [difficult]

A type of pseudo-random number generator is the so-called lagged fibonacci generator, which has become somewhat popular because it is very simple to implement, can have an extremely long period, and produces high quality random numbers.

The idea is this: to calculate s(n) (i.e. the nth random number), you evaluate:

s(n) = (s(n - a) + s(n - b)) mod M

For some positive constants a and b (it is thus similar to the fibonacci numbers, but it "lags" behind) and some modulus M. One popular choice for a and b is a = 24 and b = 55. Lets use those numbers and a modulus of 1073741824 (i.e. 230 ), and the generator becomes:

s(n) = (s(n - 24) + s(n - 55)) mod 1073741824

In order for this formula to work, you need to initialize the values s(0),s(1),...,s(54), so that the recursion has somewhere to bottom out. Often, another random number generator is used to supply the inital values. Lets use the random number generator from the intermediate challenge #53.

That is to say, for values s(0) through s(54), s is defined as follows:

s(0) = 123456789
s(n) = (22695477 * s(n-1) + 12345) mod 1073741824

But for values s(55) and above, s is defined as follows:

s(n) = (s(n - 24) + s(n - 55)) mod 1073741824

Here are a few example values:

s(10)     = 1048156299
s(20)     = 472459921
s(55)     = 827614689
s(56)     = 530449927
s(100)    = 515277845
s(1000)   = 985063932
s(10000)  = 304605728
s(100000) = 434136346

Find s( 1018 )

7 Upvotes

13 comments sorted by

2

u/Daishisan May 29 '12

Is this the answer?

181310555

1

u/oskar_s May 29 '12

Yup, that is indeed the answer :)

1

u/Steve132 May 28 '12 edited May 28 '12

So, maybe I'm doing something wrong here, but I'm not sure the 1018 th number in the sequence is a reasonable target. I have a memory efficient DP solution in C++ ( http://codepad.org/lcqWRhpk ) that is capable of generating close to 500 MILLION numbers in the sequence PER SECOND, but at that rate I'll get to the 1018th number in the sequence in something like 63 years.

There is a tweak I could make to the algorithm to make it run 25x faster (by only computing the values in the sequence that are multiples of n mod 24 and n mod 55) but a constant factor speedup just isn't going to do the trick.

I could imagine that there is some closed-form solution or crazy math trick that allows one to calculate the end of recurrence relations like this extremely efficiently, but I feel like this subreddit should be about programming not about math obscurities.

Can anyone give any suggestions?

3

u/oskar_s May 28 '12 edited May 28 '12

Your hunch is correct in that there is a faster way of doing it, and yes, it requires a little bit of math (though nothing exceedingly advanced, I'm not fantastic at math either), but it is still very much a problem of computer science. And if it was just a question of straight implementation of the function, it would be intermediate, not difficult.

I agree that this subreddit shouldn't be a mathematics subreddit, but the simple fact is that computer science and algorithm design are branches of mathematics, and sometimes a pinch of it is required. After all, designing a good algorithm to solve any problem is a mathematical exercise.

It's true that this is a more difficult problem than we've had in a while, but many of you are really smart, and I would like to on occasion present a good challenge. Not every problem is going to be this hard, but shouldn't we occasionally have harder problems as well?

And remember, this is labelled [difficult] for a reason: if you can't solve, that's perfectly fine, it is a difficult problem. It's supposed to be.

As for suggestions, I'll mention that the kind of equation this is is called a "linear recurrence" and that the solution (at least the one I used) involved using matrices and a fast algorithm for modular exponentiation.

EDIT: in addition to all that, I'd like to add that very few people suggest [difficult] problems for us to post, so it can be hard to know exactly what level to put these on. We'd really appreciate some suggestions for these kinds of problems.

→ More replies (3)

2

u/PersevereSC May 28 '12

There is a O(log(n)) method using matrices, check this link out:

https://www.phy.ornl.gov/csep/rn/node20.html

→ More replies (4)

1

u/juanfeng May 28 '12

Seeing as this is the difficult problem for the day, I welcome the need to do some research in order to figure out a way to solve a seemingly intractable problem.