Skip to content

Instantly share code, notes, and snippets.

@emiflake
Created March 23, 2019 00:02
Show Gist options
  • Save emiflake/8804b99d433754f6c28dd514bdb277e1 to your computer and use it in GitHub Desktop.
Save emiflake/8804b99d433754f6c28dd514bdb277e1 to your computer and use it in GitHub Desktop.
#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