Created
March 16, 2012 23:25
-
-
Save Mjiig/2053561 to your computer and use it in GitHub Desktop.
Lychrel numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
bool palindrome ( unsigned long long int i ); | |
unsigned long long int reverse ( unsigned long long int i ); | |
bool lychrel ( unsigned long long int i ); | |
int main ( void ) | |
{ | |
unsigned long long int i=0; | |
int count=0; | |
for(i=1;i<10000;i++) | |
{ | |
if(lychrel(i)) | |
{ | |
count++; | |
} | |
} | |
printf("\n\%d\n", count); | |
return 0; | |
} | |
bool lychrel ( unsigned long long int i ) | |
{ | |
int j; /*iteration counter*/ | |
bool lychrel = true; | |
i = i + reverse ( i ); | |
for ( j = 1; j <= 30 ; j++ ) | |
{ | |
if ( palindrome ( i ) ) | |
{ | |
lychrel = false; | |
break; | |
} | |
i = i + reverse ( i ); | |
} | |
return lychrel; | |
} | |
unsigned long long int reverse ( unsigned long long int i ) | |
{ | |
unsigned long long int ret = 0; | |
while ( i != 0 ) | |
{ | |
ret *= 10; | |
ret += i % 10; | |
i /= 10; | |
} | |
return ret; | |
} | |
bool palindrome ( unsigned long long int i ) | |
{ | |
return ( i == reverse ( i ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment