Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created November 30, 2011 21:55
Show Gist options
  • Select an option

  • Save jakedobkin/1411151 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1411151 to your computer and use it in GitHub Desktop.
Euler 26
# we know the period of a repeating decimal p is always less than p
# so we can count down- when we find a big repeater, we save it
# and stop counting when i is less than biggest period
i = 1000
biggest_period = 0
while (i > 0):
#compute the order of 10 modulo p
j = 1
while j < 1000:
order = (10**j)%i
if (order == 1):
# print "1/%r terminates in %r digits" % (i,j)
if biggest_period < j:
biggest_i = i
biggest_period = j
break
j = j + 1
if i < biggest_period:
break
i = i - 1
print "for p<1000, 1/%d the longest period, which is %d digits long" % (biggest_i, biggest_period)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment