r/dailyprogrammer • • 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 1073741824

So 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

19 comments sorted by

View all comments

Show parent comment

1

u/TweenageDream May 16 '12

Eh, i optimized it a bit, now runs nearly 10 times faster, optimized it because we're generating the first 10 mil consecutive numbers, i stop keeping track of all of them, and instead just keep track of the last one. Although my first solution would work if you wanted to generate random seeds / numbers

class Pseudo_random
    attr_accessor :gened, :largest

    def initialize
        @largest = Array.new(1000){|i| 0}
    end
    
    def s(n,last=nil)
        return 123456789 if n == 0 or last.nil?
        ret = (22695477 * last + 12345) % 1073741824
        if ret > @largest[0]
            (@largest << ret).shift
            @largest.sort!
        end
        ret
    end
end

s = Time.now
pr = Pseudo_random::new
last = nil
1e7.to_i.times{ |n| last = pr.s(n,last)}
ans = pr.largest.inject(:+)
puts "Found #{ans} in #{Time.now - s} seconds!

output:

Found 1073683936567 in 15.156 seconds!