Skip to content

Instantly share code, notes, and snippets.

@axiomsofchoice
Created March 22, 2011 23:38
Show Gist options
  • Save axiomsofchoice/882342 to your computer and use it in GitHub Desktop.
Save axiomsofchoice/882342 to your computer and use it in GitHub Desktop.
A brute-force attempt to test a few examples for the "sequence" puzzle
let primes = [2,3,5,7] in let mult (x,y) = x*y in let {f 0 ls = ls ; fn ls = map mult (zip primes (f (n-1) ls)) } in f 3 primes
"""A brute-force attempt to test a few examples for the "sequence" puzzle given at
http://www.math.utah.edu/~cherk/puzzles.html
"A sequence
A sequence of natural numbers is determined by the following formula, A[n+1] = a[n] + f(n) Where f(n) is
the product of digits in a[n]. Is there an a[1] such that the above sequence is unbounded?"
"""
# Implements the a[n+1] = a[n] + f[n] recurrence relation
def recurrence(n,i):
digprod = int(reduce(lambda a,b: int(a)*int(b), str(n)))
if digprod == 0:
return (n,i)
else:
return recurrence(n + digprod,i+1)
# Use brute force to try and see if any of the first million numbers results in
# an unbounded sequence
for i in range(1,100000):
print "The sequence starting with the number %s is bounded at %s iterations" % recurrence(i,1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment