Yo soy 196

--Originally published at Fundamentos de Programación

Long time ago I tweeted that I was going to do all the proyects from a previous Ken’s course but it’s been a long time and I haven’t  posted a single solution.

0mabexr_700wa_0+

It’s not that I haven’t done them, is just that I didn’t know which one to post, so I chose the lychrell number challenge because I thought it was really interesting, I didn´t even knew that kind of numbers had a name!

So here is what the challenge said:

WSQ11 – Yo Soy 196

Lychrel numbers are natural numbers that do not form a palindrome after successive additions to their inverse. See details on Wikipedia:http://en.wikipedia.org/wiki/Lychrel_number

What to Do

Your jobs is to create a program that asks the user for two pieces of data:

  • The lower bound of the sequence
  • The upper bound of the sequence
Then you check the values from the lower bound (inclusive) to the upper bound (inclusive) and make a report of them. During the analysis of each number, if a Lychrel number is found it should be reported immediately with something like “Found a Lychrel number: 196”

Details

The report must show:
  • The range of numbers analysed (lower to upper bound)
  • The number of natural palindromes (no addition to inverse needed)
  • The number of non-Lycherels encountered (become palindromes)
  • The number of Lycherel number candidates (that did not converge to palindrome)

Since you will not be able to prove that a number is Lycherel (since you cannot computer forever to check), our definition for a Lycherel candidate will be if a number does not converge after 30 iterations of applying the addition to the inverse.

Here is my solution:

yosoy1

yosoy2

We begin validating the user’s input as always with try and except inside a loop.

We create a function that reverses

yosoy3
number in order for us to compare it to the original number.

The for willdo one of three:

Indicate that it is a natural palindrome if without any cycle repetition it is a palindrome.

If the number after 30 or less loops becomes a palindrome, and

If the number, after 30+ loops is still not a palindrome.

The output is the amount of natural palindromes, the number of non-lychrell numbers and print the lichrel numbers, like this:

yosoy3

It was pretty fun to make. If you have trouble understanding what the program is supposed to do (like me) check out Ken’s video:

Thanks guys!

Follow me on twitter @danigguemez

Code on Github: https://github.com/Guemez/TC1014/blob/master/yo_soy_196.py

Sources: http://kenscourses.com/tc101winter2015/course-home-page-for-tc1014/wsqs-tc1014/wsq11-yo-soy-196/

#TC101 #python #KenCoursesChallenge #yosoy196