diff options
Diffstat (limited to 'c')
| -rw-r--r-- | c/wip/053-combinatoric-selections.c | 43 | ||||
| -rw-r--r-- | c/wip/092-square_digit_chains.c | 89 |
2 files changed, 132 insertions, 0 deletions
diff --git a/c/wip/053-combinatoric-selections.c b/c/wip/053-combinatoric-selections.c new file mode 100644 index 0000000..9006f5a --- /dev/null +++ b/c/wip/053-combinatoric-selections.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> + + +typedef long long unsigned int Natural; + +Natural factorial(Natural n) +{ + if (n == 1 || n == 0) return 1; + else return n * factorial(n - 1); +} + +Natural combination(Natural n, Natural r) +{ + if (r > n) exit(1); + Natural num = 0; + /* for (Natural i = 0; i < r - 1; i++) */ + /* num *= n - i; */ + /* return num / factorial(r); */ + /* if (factorial(r) * factorial(n - r) == 0) return 0; */ + return (factorial(n) / (factorial(r) * factorial(n - r))); + /* return (factorial(r) * factorial(n - r)); */ +} + +int main(void) +{ + printf("10! = %llu\n", factorial(10)); + printf("23! = %llu\n", factorial(23)); + printf("(23 - 10)! = %llu\n", factorial(23 - 10)); + int counter = 0; + Natural comb = 0, n, r; + /* for (n = 1; n <= 24; n++) */ + /* for (r = 1; r <= n; r++) { */ + /* comb = combination(n, r); */ + /* if (comb > 1000000) { */ + /* counter++; */ + /* comb = combination(23, 10); */ + printf("%llu C %llu = %llu\n", n, r, comb); + /* } */ + /* } */ + printf("counter = %d", counter); + return 0; +} diff --git a/c/wip/092-square_digit_chains.c b/c/wip/092-square_digit_chains.c new file mode 100644 index 0000000..7029ccd --- /dev/null +++ b/c/wip/092-square_digit_chains.c @@ -0,0 +1,89 @@ +#include <stdio.h> +#include <stdbool.h> +#include <math.h> + + +int lenHelper(int x) +{ + if (x >= 10000000) return 8; + if (x >= 1000000) return 7; + if (x >= 100000) return 6; + if (x >= 10000) return 5; + if (x >= 1000) return 4; + if (x >= 100) return 3; + if (x >= 10) return 2; + return 1; +} + +int get_digit(int nb, int digit_index) +{ + if (digit_index == 1) + return nb % 10; + + return (int)(nb / pow(10.0, (double)(digit_index-1))) % 10; +} + +int next_iteration(int nb) +{ + int i, sum = 0; + for (i = 0; i < lenHelper(nb); i++) { + sum += pow(get_digit(nb, i), 2); + } + return sum; +} + +bool end_of_square_digit_chain_is_89(int nb) +{ + bool once = false; + int next_nb; + for (int i = 0; i < 10; i++) { + // printf("%d", next_nb); + next_nb = next_iteration(nb); + if (next_nb == 89 || next_nb == 1) { + if (once) { + return next_nb == 89 ? true : false; + } + once = true; + } + nb = next_nb; + } + return false; +} + + +int main(void) +{ + int i, counter = 0; + for (i = 1; i < 10000000; i++) { + // printf("%d\n", i); + if (end_of_square_digit_chain_is_89(i)) + counter++; + if (i % 1000000 == 0) + printf("%d\n", counter); + } + + printf("%d\n", counter); + + return 0; +} + + + +// def end_of_square_digit_chain_is_89(nb): +// once = False +// while True: +// next_nb = sum([int(x)**2 for x in str(nb)]) +// if next_nb == 89 or next_nb == 1: +// if once: +// return True if next_nb == 89 else False +// once = True +// nb = next_nb + +// counter = 0 +// for i in range(1, 10_000_000): +// if end_of_square_digit_chain_is_89(i): +// counter += 1 +// if i % 1000_000 == 0: +// print(counter) +// pass +// print(counter) |
