WSQ11 – Yo soy 196

this program recives two number values, a low number and a higher number, and check the numbers from the lower to the highest number reporting: the range of numbers analyzed, number of natural palindromes, number of non-lychrel numbers, the amount of lychrel numbers found and print the lychrel numbers found.

to start i created a function to invert the integers so i could compare them.

def reverse(a):

        a = str(a)

        a = a[::-1]

        a = int(a)

        return a

this turns 123 into “123” into “321” into 321.

a function to determine if a number is a natural palindrome.

def palin(a):

      a1 = a

      a2 = reverse(a)

      if a1 == a2:

          return 1

      else:

          return 0

and a function to add the numbers to their inverse until they become a palindrome or reach a set amount of tries.

def nopalin(a)

    c = 0

    while c < 30:

       a1 = a

       a2 = reverse(a1)

       a3 = a1+a2

       a4 = reverse(a3)

       if a3 == a4:

          return 1

          break

       if a3 != a4:

          a = a3

          c += 1

       if c >= 30:

           return 0

that will take a number, invert it and add them until it becomes a palindrome for 30 tries, if it does not becomes a palindrome before then it will count as a lychrel number. after those 3 functions all you need is arrange them and add counters an a print order for the lychrel number found.

 

reference for the reverse function:

http://stackoverflow.com/questions/931092/reverse-a-string-in-python

my code in github:

https://github.com/nazare52/progra/blob/master/wsq11.py

CC BY 4.0 WSQ11 – Yo soy 196 by sergio is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.