-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome.cpp
76 lines (71 loc) · 1.65 KB
/
palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <string>
#include <iostream>
#include "BigIntegerLibrary.hh"
using namespace std;
bool is_palindrome(BigInteger n){
string s = bigIntegerToString (n);
reverse (s.begin(), s.end());
BigInteger x = stringToBigInteger (s);
if (n == x){
return true;
} else {
return false;
}
}
BigInteger apply196(BigInteger n){
string p = bigIntegerToString (n);
reverse (p.begin(), p.end());
BigInteger q = stringToBigInteger (p);
n = n + q;
return n;
}
void Space (){
cout << endl;
}
int main() {
int a;
int b;
int np = 0;
int nl = 0;
int l = 0;
int i;
BigInteger x;
cout << "Enter the lower bound:" << endl;
cin >> a;
cout << "Enter the upper bound:" << endl;
cin >> b;
Space ();
for (i = a; i <= b; i++){
cout << "Analyzing number: " << i << endl;
if (is_palindrome(i) == true){
cout << "This number is a natural palindrome!" << endl;
np++;
Space ();
} else {
x = apply196 (i);
for (int c = 1; c <= 30; c++){
if (is_palindrome (x) == true){
cout << "This number is a non-Lychrel number!" << endl;
nl++;
Space ();
break;
} else {
x = apply196 (x);
}
}
if (is_palindrome (x) == false){
cout << "This number is a Lychrel number!" << endl;
l++;
Space ();
}
}
}
Space ();
cout << "Analysis Report: " << endl;
Space ();
cout << "Number of Values Analyzed: " << (b - a) + 1 << endl;
cout << "Number of Natural Palindromes: " << np << endl;
cout << "Number of Non-Lychrel Numbers: " << nl << endl;
cout << "Number of Lychrel Numbers: " << l << endl;
return 0;
}