Skip to content

Instantly share code, notes, and snippets.

@samir96
Created April 24, 2015 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samir96/c35889d77cd43e012928 to your computer and use it in GitHub Desktop.
Save samir96/c35889d77cd43e012928 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
using namespace std;
#include "BigIntegerLibrary.hh"
bool is_palindrome(BigInteger n){
string x = bigIntegerToString (n);
reverse(x.begin(),x.end());
BigInteger y = stringToBigInteger(x);
if (n == y){
return true;
} else{
return false;
}
}
BigInteger apply196(BigInteger n){
BigInteger candidato;
string x = bigIntegerToString (n);
reverse(x.begin(),x.end());
BigInteger y = stringToBigInteger(x);
candidato = y + n;
return candidato;
}
int main() {
int low, high, counterpalindrome = 0, becomepalindrome = 0, Lychrelcounter = 0, nonlychrel, lychrel;
BigInteger candidate;
cout << "Introduce el primer numero del rango." << endl;
cin >> low;
cout << "Introduce el segundo numero del rango." << endl;
cin >> high;
for(int i=low; i<=high; i++){
if(is_palindrome(i) == true){
counterpalindrome = counterpalindrome + 1;
} else{
candidate = i;
int counter = 0;
while(is_palindrome(candidate)==false && counter < 30){
candidate = apply196(candidate);
counter++;
if (is_palindrome(candidate) == true){
becomepalindrome++;
}
}
}
if (is_palindrome(candidate) == false) {
Lychrelcounter++;
cout << "Encontre un numero de Lychrel: " << i << endl;
}
}
cout << "El rango de numeros analizados fue de " << (high - low) + 1 << " numeros." << endl;
cout << "Encontre " << counterpalindrome << " numeros que son palindromos naturales." << endl;
cout << "Encontre " << becomepalindrome << " numeros que no son numeros de Lychrel." << endl;
cout << "Encontre " << Lychrelcounter << " numeros de Lychrel." << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment