--Originally published at The Talking Chalk
The Task
Create a program that determines how many possible lychrel numbers, non lychrel numbers, and palindromes exist between a range.
The Process
For sure this WSQ was the one that made the boat enter from calm waters to the tempest. It was not so complicated, yet it required brains. I even managed to find a video from another Problem Solving Course which helped to give body to my code; however, I still faced problems with the library.
The Code
#include<iostream>
#include<vector>
#include <math.h>
using namespace std;
#include “BigIntegerLibrary.hh”
BigInteger tester(BigInteger number)
{
BigInteger toturn=0;
while(number>0)
{
toturn=(toturn*10)+(number%10);
number=number/10;
}
return toturn;
}
BigInteger lychrels(BigInteger number)
{
BigInteger sumnums, toret, counter=0;
if(number==tester(number))
{
toret=1;
}
else
{
sumnums=number+tester(number);
while(counter!=30 && sumnums != tester(sumnums))
{
if (counter >=30)
{
toret=2;
cout<<“Lychrel number candidates.”<<number<<endl;
}
else
{
toret=3;
}
}
}
return toret;
}
int main()
{
BigInteger lowest, highest, lychrel=0, palindromes=0, toret, nonlyc=0, number;
cin>>lowest;
cin>>highest;
number=lowest;
for(int counter=0; number<highest; counter++)
{
number+=counter;
if(lychrels(number)==1)
{
palindromes++;
}
else if (lychrels(number)==2)
{
lychrel++;
}
else
{
nonlyc++;
}
}
cout<<“lychrels “<<lychrel<<endl;
cout<<“natural palindrome “<<palindromes<<endl;
cout<<“non-lychrel “<<nonlyc<<endl;
return 0;
}
The topics
#Basic types and their use