YoSoy196

The source code is in GitHub.

This program asks the user for the two boundaries of a range and then the program starts calculating for each value if it’s a palyndrome. 

For this the function check_palyndrome( ) is created. The function first saves the orignial number in form of a string. Once we have an String it’s very easy to reverse it. 

If you have a String you can treat it as a list, so if you want to reorder it you can use the [::-1], notation after it. What it does is position itself in the last index, and start decrementing it, thus writting the string backwards. This is also saved in a different variable. 

If the String and it’s reverse are the same, the function returns the value 0; wich in the main program is interpretated as adding 1 to the natural palyndromes count.

If they are not the same, then we take the reversed String and parse it into an int, and then add it to the original number. And update the palyndrome that we whant to check as the result of this sum. Then the process is repeated. If the String and it’s reverse are the same before the 30th try, the function returns a 1; which is the indicator for adding one to the non-Lychrels numbers.

If after the 30th try the String and its reverse are not the same, then we categorize it as a Lychrel’s number. So the function prints the message: “Found a Lychrel number: “with the number before returning a 2 that increments the Lychrels numbers count. 

The [::-1] notation is better explained here, as extended slice notation.

This is my of my .

CC BY 4.0 YoSoy196 by Manuel Lepe is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.