Yo soy 196. Palindromes and Lychrel numbers

ANITA LAVA LA TINA

RECONOCER

RACE CAR

Here you can take a look at My code on GitHub.

You should really check out Orlando´s Blog post.  You´ll learn how to do it perfectly.

 

First of all, I really have to thank Orlando Lara. Thank you for your time doing those amazing videos teaching us mortals about #WSQ11. Also many thanks to my teacher Ken Bauer, for his video explaining how to use BigInteger s on the program. You should really check out Ken´s video, cause it is kind of complex how to use and compile programs with this BigIntegers.

Basically those were all my sources to make this program. I once again thank Orlando, for teaching us how to use strings and bool functions. Before this program, I had never used them.

Following Orlando´s guide (sorry for saying it so much, but is the truth), I made my program to work with only two functions. One flips out each number of the sequence, using this:

BigInteger Num_reverse (BigInteger i){

string neto = bigIntegerToString(i);
neto = string(neto.rbegin(),neto.rend());
i = stringToBigInteger(neto);

return i;
}

It is a command to flip out the string, but what the function does first is convert once again the number from BigInteger to string (which we had already done the inverse process). Then it reverses the string and converts it back to a BigInteger using “stringToBigInteger()”. 

So this function is called inside the main, on a for loop that runs from the lower bound of the sequence provided by the user to the highest. SO IT FLIPS EACH AND EVERY NUMBER OF THE SEQUENCE. After this, with an if, it checks out if the flipped number is equal to the original number. If this  is true, the we just found

natural palindrome, and the counter for this numbers (called NatPal_count on my code) gets +1.

The second function is the one that does all the math processes of inverting the numbers many times until it becomes a palindrome or it does not. So on my code (as well as on Orlando´s 🙂 ) does this process for 30 times, inside a for loop. In order to invert the number it just calls out the first function.

bool lych_finder (BigInteger i){

for(int n=0;n<30;n++){
BigInteger flip= Num_reverse(i);
if(flip == i)
return true;
i += flip;
}
return false;

}

SO BASICALLY, ALL THIS FUNTION DOES IS IT CALLS THE PRIOR FUNCTION 30 TIMES TO CHECK WHETER THE NUMBER BEING CHECKED IS A PALINDROME. Each and every time it cheks it, when it is not a palindrome it adds up the number plus its inverse ( i += flip;).  IF AFTER 30 TIMES OF REPEATING THIS PROCESS THE NUMBER HAS NOT BECOME A PALINDROME, THEN WE HAVE FOUND A LYCHREL NUMBER CANDIDATE.

Both of this functions were called on the main function inside a for loop. Condistions were checked with ifs and elses.

This were the instructions for the program, taken from Ken Bauer´s class blog:

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.

If you are lazy, you can take a look here at my code:

#include <iostream>
#include “BigIntegerLibrary.hh”
#include <string>
#include <sstream>

using namespace std;
BigInteger Num_reverse (BigInteger i){

string neto = bigIntegerToString(i);
neto = string(neto.rbegin(),neto.rend());
i = stringToBigInteger(neto);

return i;
}

bool lych_finder (BigInteger i){

for(int n=0;n<30;n++){
BigInteger flip= Num_reverse(i);
if(flip == i)
return true;
i += flip;
}
return false;

}

int main(){

BigInteger reverse, remainder, num, total_num = 0, Natpal_count = 0, pal_count = 0, lowB, highB, lycherls, analized_num;
string x0, x1;

do {
cout << “Provide the lower bound of the sequence: ” << endl;
cin >> x0;
lowB = stringToBigInteger(x0);

cout << “Provide the upper bound of the sequence: ” << endl;
cin >> x1;
highB = stringToBigInteger(x1);

if (highB < lowB)
cout << “The lower bound of the sequence must be less than the higher bound” << endl;

} while (lowB > highB);

for (analized_num = lowB; analized_num <= highB; analized_num++){

reverse = Num_reverse (analized_num);
total_num += 1;

if (reverse == analized_num)
Natpal_count ++;
else {
bool n = lych_finder(analized_num);
if(n == true)
pal_count++;
else{
cout<<“Lychrel number found: “<< analized_num << endl;
lycherls ++; }
}
}

cout << “The results for the range between ” << x0 << ” and ” << x1 << ” are: ” << endl;
cout << endl;
cout << total_num << ” numbers were analysed” << endl;
cout << “Natural Palindrome numbers total: ” << Natpal_count << endl;
cout << “Non Lychrel numbers found: ” << pal_count << endl;
cout << “Number of Lychrels found: ” << lycherls << endl;
}

 

CC BY-SA 4.0 Yo soy 196. Palindromes and Lychrel numbers by netosanchezb is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.