-
Notifications
You must be signed in to change notification settings - Fork 0
/
LychrelNumbers.cpp
45 lines (39 loc) · 966 Bytes
/
LychrelNumbers.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
// This file is written by Alireza Kamyab
// Answer to projecteuler.net/problem=55
#include <iostream>
#include <cinttypes>
#include <ctime>
using namespace std;
uint64_t reverse(uint64_t number);
bool isPalindromic(uint64_t number);
bool isLychrel(const uint64_t& number);
int main() {
int count = 0;
for(int i = 1; i < 10000; i++){
if(isLychrel(i)) {
count++;
}
}
cout << count << endl;
return 0;
}
bool isLychrel(const uint64_t& number){
uint64_t temp = number + reverse(number);
for(int i = 0; i < 50; i++) {
if(isPalindromic(temp)) return false;
temp = temp + reverse(temp);
}
return true;
}
bool isPalindromic(uint64_t number) {
if(number == reverse(number)) return true;
else return false;
}
uint64_t reverse(uint64_t number) {
uint64_t temp = 0;
while(number > 0) {
temp = (temp * 10) + (number % 10);
number /= 10;
}
return temp;
}