Created
March 23, 2019 00:02
-
-
Save emiflake/8804b99d433754f6c28dd514bdb277e1 to your computer and use it in GitHub Desktop.
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 "libft.h" | |
size_t digit_mul(long long n) | |
{ | |
size_t i; | |
i = n % 10; | |
n /= 10; | |
while (n > 0) | |
{ | |
if (n % 10 == 0) | |
return (0); | |
i *= (n % 10); | |
n /= 10; | |
} | |
return (i); | |
} | |
size_t persistence(long long n) | |
{ | |
size_t step_count; | |
step_count = 0; | |
while (n > 10) | |
{ | |
n = digit_mul(n); | |
step_count++; | |
} | |
return (step_count); | |
} | |
int main(int argc, char **argv) | |
{ | |
long long max; | |
long long num; | |
long long best; | |
size_t per; | |
num = 0; | |
best = 1; | |
if (argc == 3) | |
{ | |
num = atoll(argv[1]); | |
max = atoll(argv[2]); | |
while (num < max) | |
{ | |
per = persistence(num); | |
if (per > best) | |
{ | |
printf("%lu: %llu\n", per, num); | |
best = per; | |
} | |
num++; | |
} | |
} | |
else | |
{ | |
ft_putendl("Enter two numbers (min, max)."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment