对应的数字是 111。我通过取你答案的倒数来计算它:
1/0.009009009009009 = 111
顺便说一句,我并不是说你的答案是正确的,我只是在帮助你解释你得到的答案。1/7 的数字有更长的重复序列。
真正解决问题
我实际上真正解决了这个问题。这是我为解决它而编写的代码/注释。如果您只是在查看我的代码之前阅读了我的评论,那么您应该能够自己编写代码以发现这里发生了什么。如果你想被宠坏,就去最底层看看答案是什么。
# First, we need to figure out how digits in a decimal representation
# of a fraction get calculated. We will write a method that prints
# the first 50 decimal digits after the decimal point for the decimal
# representation of a/b, where a and b are integers.
def print_digits_after_decimal(a, b)
raise ArgumentError if !a.is_a?(Integer)
raise ArgumentError if !b.is_a?(Integer)
# We only care about what's happening after the decimal point.
r = a % b
print "#{a}/#{b} = ."
50.times do
r *= 10
digit = r / b
print digit
r = r % b
end
puts
end
print_digits_after_decimal(1, 7)
print_digits_after_decimal(11, 28)
# Results:
# 1/7 = .14285714285714285714285714285714285714285714285714
# 11/28 = .39285714285714285714285714285714285714285714285714
# The only thing that changes on each iteration of that loop is r.
# Observe that r is always within the finite range 0..(b-1). So
# eventually r MUST be equal to a value that it already had before.
# We will write a method to find that first repeated r value. This
# represents the state of the digit-generating state machine for the
# first state where the decimal digits start a repeating sequence.
def first_repeated_r(a, b)
raise ArgumentError if !a.is_a?(Integer)
raise ArgumentError if !b.is_a?(Integer)
r = a % b
log = []
while true do
return r if log.include?(r)
log << r
r = (r * 10) % b
end
end
# Now use that r value to generate the digits that repeat. We need to
# call the first_repeated_r method above, and generate digits until we
# see that r value again, and then return those digits as an array.
def repeating_decimal_sequence(a, b)
r_start = r = first_repeated_r(a, b)
digits = []
while true
r *= 10
digits << r / b
r = r % b
break if r == r_start
end
digits
end
# Keep in mind that each r value corresponds to a digit we print out,
# but many different r values can correspond to the same digit. We
# have not proved that the sequence returned by
# repeating_decimal_sequence doesn't contain a repeated sequence
# inside itself. We will write a method that takes an array of digits
# and shortens it to the smallest repeating sequence.
def decimal_sequence_deduplicate(digits)
(1..digits.size).each do |n|
subsequence = digits[0, n]
q, r = digits.size.divmod(n)
if r == 0 && subsequence * q == digits
return subsequence
end
end
raise "Impossible!" # math broke
end
p decimal_sequence_deduplicate([1, 5, 8]) # => [1, 5, 8]
p decimal_sequence_deduplicate([1, 5, 1, 5]) # => [1, 5]
# Now we can put these pieces together to answer your question.
answer = (1...1000).max_by do |n|
decimal_sequence_deduplicate(repeating_decimal_sequence(1, n)).size
end
puts answer # => 983
# And now we should feel kind of dumb because 983 is simply the
# largest prime number less than 1000, and Euler probably knew that
# without doing this much work!