Created
March 22, 2019 02:03
-
-
Save Grumblesaur/58943d70247634188fc5f7d5e428c4fb to your computer and use it in GitHub Desktop.
when Matt Parker started writing his Python script in this video ( https://www.youtube.com/watch?v=Wim9WJeDTHQ ), I decided to pause it and write my own
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
import sys | |
import operator | |
from functools import reduce | |
def digit_product(n): | |
return reduce(operator.mul, map(int, list(str(n)))) | |
def persistence(n, return_pair=False): | |
original = n | |
digits = len(str(n)) | |
iterations = 0 | |
while digits > 1: | |
n = digit_product(n) | |
iterations += 1 | |
digits = len(str(n)) | |
return (original, iterations) if return_pair else iterations | |
def main(*args): | |
if args: | |
for arg in args: | |
print(persistence(int(arg), return_pair=True)) | |
else: | |
try: | |
x = input("Enter a non-negative number: ") | |
x = int(x) | |
except ValueError as e: | |
print(e) | |
return 1 | |
else: | |
print(persistence(x)) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main(*(sys.argv[1:]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment