Created
October 17, 2014 02:32
-
-
Save mdciotti/8317a73ce48b128451fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
unsigned long long int reduce (unsigned long long int n) | |
{ | |
unsigned long long int result, d; | |
result = 1; | |
while (n > 0) { | |
d = n % 10; | |
n = (n - d)/10; | |
result *= d; | |
} | |
return result; | |
} | |
unsigned long long int reduceAndCount (unsigned long long int n) | |
{ | |
unsigned long long int count; | |
count = 0; | |
while (n > 10) { | |
n = reduce(n); | |
count++; | |
} | |
return count; | |
} | |
unsigned long long int findPersistence (unsigned long long int n) | |
{ | |
unsigned long long int i, x; | |
i = 0; | |
x = 0; | |
while (x != n) { | |
x = reduceAndCount(++i); | |
} | |
return i; | |
} | |
int main (int argc, char *argv[]) | |
{ | |
unsigned long long int n; | |
n = atoi(argv[1]); | |
printf("%llu\n", findPersistence(n)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment