Skip to content

Instantly share code, notes, and snippets.

@Adals20
Created April 25, 2017 02:29
Show Gist options
  • Save Adals20/f20fbd8c15d30adcd9c5fede728394c0 to your computer and use it in GitHub Desktop.
Save Adals20/f20fbd8c15d30adcd9c5fede728394c0 to your computer and use it in GitHub Desktop.
// Solving Problems with Programming
// Professor Ken Bauer
//
// Your name here
// Your student# here
// If you worked with somebody else, you need to put their names here
#include <iostream>
#include <string>
using namespace std;
#include "BigIntegerLibrary.hh"
bool espalindromo(BigInteger n){
string x = bigIntegerToString (n);
x= string (x.rbegin(),x.rend()); //This assignation helps to make the operation of reverse the string value from the beginning
//(x.rbegin()) to the end (x.rend()). Therefore all that string will be reverse and that reverse value wil have it the string variable x
BigInteger y = stringToBigInteger(x); //Declarate BigInteger variable y. Then we have an assignation operation between 'y' and 'x' and
//stands for convert the reverse string value of x to reverse and numeric BigInteger value and save this new value of BigInteger to
//BigInteger variable 'y'
if (n == y){
return true;
}
else{
return false;
}
}
BigInteger becomepalindrom(BigInteger n){
BigInteger candidatoo;
string x = bigIntegerToString (n);
x=string(x.rbegin(),x.rend());
BigInteger y = stringToBigInteger(x);
candidatoo = y + n;
return candidatoo;
}
int main(){
int bajo, alto, contadorpali = 0, palindromohecho = 0, countadorLychrel = 0;
BigInteger candidate;
cout << "Dame el valor más bajo de la lista de numeros a evaluar: ";
cin >> bajo;
cout << "Dame el valor más alto de la lista de numeros a evaluar: ";
cin >> alto;
cout<<"\nCalculando los numeros: palindromos, no-Lychrel o Lychrel candidatos\n"<<endl;
for(int i=bajo; i<=alto; i++) {
if(espalindromo(i) == true){
contadorpali = contadorpali + 1;
}
else
{
candidate = i;
int counter = 1;
while(espalindromo(candidate)==false && counter <= 30){
candidate = becomepalindrom(candidate);
counter++;
if (espalindromo(candidate) == true){
palindromohecho++;
}
}
}
if (espalindromo(candidate) == false){
countadorLychrel++;
cout << "Numeros de Lychrel encontrados: " << i << endl;
}
}
cout << "Los resultados del rango "<<bajo<<" al "<<alto<<endl;
cout << "\nPalindromos naturales: " << contadorpali<<endl;
cout << "No Lychrels (pero se pueden convertir en palindromos): " << palindromohecho<<endl;
cout << "Candidates Lychrel: " << countadorLychrel<<endl<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment