r/dailyprogrammer • u/oskar_s • May 16 '12
[5/16/2012] Challenge #53 [intermediate]
A simple pseudo-random number generator looks like this:
s(0) = 123456789
s(n) = (22695477 * s(n-1) + 12345) mod 1073741824So each number is generated from the previous one.
Using this generator, generate 10 million numbers (i.e. s(0) through s(9,999,999)) and find the 1000 largest numbers in that list. What is the sum of those numbers?
Try to make your solution as efficient as possible.
- Thanks to sim642 for submitting this problem in /r/dailyprogrammer_ideas! If you have a problem that you think would be good, head on over there and help us out!
11
Upvotes
View all comments
Show parent comment
1
u/HazzyPls 0 0 May 22 '12
Which compilers and optimization levels did you use? My timing came from clang with O2. Last I heard, D compilers lagged behind C compilers with regards to optimizations because they're quite a bit newer. But it's been a while, so I don't know if that's still the case. I think switching from O0 to O2 halved the run time of my code.
I don't get why memmove would be faster, unless there's some fancy optimization the compiler can do with it. I mean, worse case memmove's moving 999 * 8 = 7992 bytes into a buffer, and then out again. How is that faster than something like a Linked List, which I was going to use originally, before getting a bit fed up with how much more complicated my little program got.
You've made me thoroughly curious about this....