15th day done, stole Christoffers naming scheme :<

This commit is contained in:
Alexander Munch-Hansen 2017-12-25 23:37:11 +01:00
parent ebc3014c48
commit d7d1ed86db
2 changed files with 70 additions and 0 deletions

30
15_1.rb Normal file
View File

@ -0,0 +1,30 @@
GEN_A_START = 679
GEN_B_START = 771
GEN_A_TEST_S = 65
GEN_B_TEST_S = 8921
GEN_A_FAC = 16807
GEN_B_FAC = 48271
MOD = 2147483647
def compare a, b
a_bin = a.to_s(2)[-16..-1]
b_bin = b.to_s(2)[-16..-1]
a_bin == b_bin
end
matches = 0
a_rem = GEN_A_START
b_rem = GEN_B_START
for i in 1..(40*10**6) do
if i % 1000000 == 0 then puts i end
a_rem = (a_rem * GEN_A_FAC) % MOD
b_rem = (b_rem * GEN_B_FAC) % MOD
if compare a_rem,b_rem then matches += 1 end
end
puts matches

40
15_2.rb Normal file
View File

@ -0,0 +1,40 @@
GEN_A_START = 679
GEN_B_START = 771
GEN_A_TEST_S = 65
GEN_B_TEST_S = 8921
GEN_A_P = 4
GEN_B_P = 8
GEN_A_FAC = 16807
GEN_B_FAC = 48271
MOD = 2147483647
def compare a, b
a_bin = a.to_s(2)[-16..-1]
b_bin = b.to_s(2)[-16..-1]
a_bin == b_bin
end
matches = 0
def find_next val, fac, mul
while(true) do
val = (val * fac) % MOD
break if val % mul == 0
end
val
end
a_rem = GEN_A_START
b_rem = GEN_B_START
for i in 1..(5*10**6) do
if i % 1000000 == 0 then puts i end
a_rem = find_next a_rem, GEN_A_FAC, GEN_A_P
b_rem = find_next b_rem, GEN_B_FAC, GEN_B_P
if compare a_rem, b_rem then matches +=1 end
end
puts matches