Created
November 28, 2016 21:27
-
-
Save montekaka/4b2eb3680bfecb1a529d69ae5d87831f to your computer and use it in GitHub Desktop.
26 created by montekaka - https://repl.it/E90n/37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reciprocal_cycles(numerator, num_of_denominators) | |
max_length = 0 | |
answer = 0 | |
i = 2 | |
while i <= num_of_denominators | |
p "1/#{i} has #{division(numerator, i)}" | |
if division(numerator, i) > max_length | |
max_length = division(numerator, i) | |
answer = i | |
end | |
i+=1 | |
end | |
answer | |
end | |
def division(numerator, denominator, i = 0, answer = [], remainders = []) | |
if numerator % denominator == 0 || remainders.uniq.count < remainders.count | |
k = remainders.index(remainders.last) | |
answer = answer[0...-1] + answer[k...-1] | |
reciprocal_sequence(answer).count | |
else | |
k = 0 | |
while numerator < denominator | |
numerator = numerator * 10 | |
remainders << numerator | |
if k > 0 | |
answer << 0 | |
end | |
k+=1 | |
i+=1 | |
end | |
answer << (numerator / denominator) | |
division(numerator % denominator, denominator, i, answer, remainders) | |
end | |
end | |
def reciprocal_sequence(a) | |
answer = [] | |
i = 0 | |
while a.count > 0 && answer.count == 0 | |
if a.count > 1 && (a[0...(a.count/2)] == a[(a.count/2)..-1]) | |
answer = a[0...(a.count/2)] | |
end | |
a = a[1..-1] | |
end | |
answer | |
end | |
reciprocal_cycles(1, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment