Permalink
Browse files

Day 15

* $ time ruby 15a.rb
  MRI: 16s
  GraalVM --native: 7s
  With Integer#times written in Ruby to enable OSR: 2s
  • Loading branch information...
eregon committed Dec 16, 2017
1 parent b9b5bd6 commit 51378b1805fef399d8efcacb19208375ea77194d
Showing with 48 additions and 0 deletions.
  1. +22 −0 2017/15a.rb
  2. +26 −0 2017/15b.rb
@@ -0,0 +1,22 @@
starts = [591, 393]
factors = [16807, 48271]
Generator = Struct.new(:start, :factor) do
def initialize(*)
super
@n = start
end
def next
@n = (@n * factor) % 2147483647
end
end
generators = starts.zip(factors).map { |start, factor|
Generator.new(start, factor)
}
p 40_000_000.times.count {
a, b = generators.map { |gen| gen.next % 2**16 }
a == b
}
@@ -0,0 +1,26 @@
starts = [591, 393]
factors = [16807, 48271]
multiples = [4, 8]
Generator = Struct.new(:start, :factor, :multiple) do
def initialize(*)
super
@n = start
end
def next
begin
@n = (@n * factor) % 2147483647
end until @n % multiple == 0
@n
end
end
generators = starts.zip(factors, multiples).map { |start, factor, multiple|
Generator.new(start, factor, multiple)
}
p 5_000_000.times.count {
a, b = generators.map { |gen| gen.next % 2**16 }
a == b
}

0 comments on commit 51378b1

Please sign in to comment.