Skip to content

Instantly share code, notes, and snippets.

@dblume
Created June 30, 2018 23:41
Show Gist options
  • Save dblume/fc6e389c0b7c3e8cdfde2b7b97956d25 to your computer and use it in GitHub Desktop.
Save dblume/fc6e389c0b7c3e8cdfde2b7b97956d25 to your computer and use it in GitHub Desktop.
Father's Day puzzle #2: Smallest number with persistence five.
#!/usr/bin/env python
# Father's Day Card Puzzle #2
# A number's persistence is the number of steps to reduce it to a single
# digit by multiplying all its digits to obain a second number, then
# multiplying all the digits of that number to obtain a third number, and so
# on until a one-digit number is obtained.
#
# For example 77 has a persistence of four because it requires four steps to
# reduce it to one digit: 77 -> 49 -> 36 -> 18 -> 8. The smallest number of
# persistence one is 10. The smallest number of persistence two is 25. The
# smallest number of persistence 3 is 39, and the smallest of persistence four
# is 77. What is the smallest number of persistence 5?
import operator
__author__ = "David Blume"
__license__ = "MIT"
def persistence(n):
s = str(n)
if len(s) == 1:
return 0
return 1 + persistence(reduce(operator.mul, [int(i) for i in s]))
assert(persistence(77) == 4)
assert(persistence(39) == 3)
assert(persistence(25) == 2)
x = 0
while persistence(x) != 5:
x += 1
print x, "has persistence", persistence(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment