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
2
u/leonardo_m May 21 '12 edited May 21 '12
A shorter D version more than twice slower than your very nice C version, topNCopy uses a binary heap:
Edit: removed my C alternative version because it wasn't really an improvement over your simple C version. It seems binary search, heaps, and even a linear search with sentinel (on a [MAX_COUNT + 1] array) plus memmove are slower than your unguarded linear search plus memmove. I don't know why.