aboutsummaryrefslogtreecommitdiff
path: root/c/wip/092-square_digit_chains.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-08-17 21:39:43 +0200
committerCharles <sircharlesaze@gmail.com>2019-08-17 21:39:43 +0200
commit3ffc76713f6db4c33f20588ce6896ea3c2bae2a7 (patch)
tree1025c801330f078e3a12da191f923ae8b6ddd81b /c/wip/092-square_digit_chains.c
parent9a65938232d1fa9e1afe9a6eb2de48d25ff738a6 (diff)
downloadproject_euler-3ffc76713f6db4c33f20588ce6896ea3c2bae2a7.tar.gz
project_euler-3ffc76713f6db4c33f20588ce6896ea3c2bae2a7.tar.bz2
project_euler-3ffc76713f6db4c33f20588ce6896ea3c2bae2a7.zip
wip directory for each language
Diffstat (limited to 'c/wip/092-square_digit_chains.c')
-rw-r--r--c/wip/092-square_digit_chains.c89
1 files changed, 89 insertions, 0 deletions
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)