1
0
Fork 0

Problem 55

This commit is contained in:
Jonathan Chan 2016-12-25 23:22:45 -08:00
parent d90bcf65e0
commit e2dfc54f9a
2 changed files with 39 additions and 0 deletions

BIN
55 Executable file

Binary file not shown.

39
55.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
long long reverse (long long n) {
long long num = n;
long long reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
bool isPalindrome(long long n) {
return n == reverse (n);
}
bool isLychrel (int n) {
long long num = n;
int lychrel = true;
int count = 0;
while (lychrel && count < 50) {
num += reverse (num);
lychrel = !isPalindrome (num);
count++;
}
return lychrel;
}
int main () {
int lychrels = 0;
for (int i = 1; i < 10000; i++) {
bool lychrel = isLychrel (i);
if (lychrel)
lychrels++;
}
printf ("%d Lychrels\n", lychrels);
}