Created
April 5, 2021 12:39
-
-
Save cjheath/5d08d04df615cd772ac63be64ce3a459 to your computer and use it in GitHub Desktop.
Find integers n whose reciprocal has n-1 repeating digits
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
MAX_DIGITS = 1000 | |
7.upto(MAX_DIGITS) do |i| | |
scaled_reciprocal = (10**(i*2)) / i | |
digits = sprintf("%0#{2*i}d", scaled_reciprocal) | |
# printf "%d -> 0.%s...\n", i, digits | |
first_repeat = nil | |
offset = nil | |
1.upto(i-1) do |r| | |
# Break the digits into as many groups of r characters as possible | |
repetitions = digits.scan(/.{#{r}}/) | |
if repetitions.uniq.size == 1 # All the groups are identical | |
# puts "\trepeats #{repetitions[0]}" | |
first_repeat = r unless first_repeat | |
end | |
end | |
if first_repeat == i-1 | |
puts "\t1/#{i} = 0.`#{digits[0,first_repeat]}`" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment