diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-03 07:02:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-03 07:02:29 +0000 |
| commit | 32ecf22a49491cb28e95a79fe3976943a6b41524 (patch) | |
| tree | 35a228f5bb3b81b73a98e27212e7c987c9a71bf3 /challenge-137/paulo-custodio/python/ch-2.py | |
| parent | f2e30e883ce4831af8e13a5f53f8a69602bda8b6 (diff) | |
| parent | 87883da68b695f54a3bd4f108fc5c31784879dac (diff) | |
| download | perlweeklychallenge-club-32ecf22a49491cb28e95a79fe3976943a6b41524.tar.gz perlweeklychallenge-club-32ecf22a49491cb28e95a79fe3976943a6b41524.tar.bz2 perlweeklychallenge-club-32ecf22a49491cb28e95a79fe3976943a6b41524.zip | |
Merge pull request #5152 from pauloscustodio/devel
Devel
Diffstat (limited to 'challenge-137/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-137/paulo-custodio/python/ch-2.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-137/paulo-custodio/python/ch-2.py b/challenge-137/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..cbdf04d268 --- /dev/null +++ b/challenge-137/paulo-custodio/python/ch-2.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +# TASK #2 > Lychrel Number +# Submitted by: Mohammad S Anwar +# You are given a number, 10 <= $n <= 1000. +# +# Write a script to find out if the given number is Lychrel number. To keep the +# task simple, we impose the following rules: +# +# a. Stop if the number of iterations reached 500. +# b. Stop if you end up with number >= 10_000_000. +# +# [UPDATED][2021-11-01 16:20:00]: If you stop because of any of the above two +# rules then we expect 1 as an output. +# +# According to wikipedia: +# +# A Lychrel number is a natural number that cannot form a palindrome through +# the iterative process of repeatedly reversing its digits and adding the +# resulting numbers. +# +# Example 1 +# Input: $n = 56 +# Output: 0 +# +# After 1 iteration, we found palindrome number. +# 56 + 65 = 121 +# Example 2 +# Input: $n = 57 +# Output: 0 +# +# After 2 iterations, we found palindrome number. +# 57 + 75 = 132 +# 132 + 231 = 363 +# Example 3 +# Input: $n = 59 +# Output: 0 +# +# After 3 iterations, we found palindrome number. +# 59 + 95 = 154 +# 154 + 451 = 605 +# 605 + 506 = 1111 + +import sys + +MAX_ITER = 500 +MAX_NUM = 10000000 + +def is_lychrel(n): + for i in range(0, MAX_ITER): + if n > MAX_NUM: + break + rev_n = int(str(n)[::-1]) + if n == rev_n: + return False + n += rev_n + return True + +if is_lychrel(int(sys.argv[1])): + print(1) +else: + print(0) |
