summaryrefslogtreecommitdiff
path: root/p9
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2013-02-13 23:04:27 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2013-02-13 23:04:27 +0200
commitfc3ad977c54a060a2b8aab47a7cea9ece371f05d (patch)
treee1cb036d38f67ff462a50379bbb32eac2c5fd3bb /p9
parent77cdad53d8673c731718723250953f6c4ed5d504 (diff)
downloadeuler-c-fc3ad977c54a060a2b8aab47a7cea9ece371f05d.tar.gz
euler-c-fc3ad977c54a060a2b8aab47a7cea9ece371f05d.zip
problems 3 - 9
Diffstat (limited to 'p9')
-rw-r--r--p9/CMakeLists.txt7
-rw-r--r--p9/main.c89
2 files changed, 96 insertions, 0 deletions
diff --git a/p9/CMakeLists.txt b/p9/CMakeLists.txt
new file mode 100644
index 0000000..987dbf1
--- /dev/null
+++ b/p9/CMakeLists.txt
@@ -0,0 +1,7 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+PROJECT(euler_problem9 C)
+INCLUDE_DIRECTORIES(../common)
+SET(SOURCES main.c ../common/utils.c)
+ADD_EXECUTABLE(p9 ${SOURCES})
+TARGET_LINK_LIBRARIES(p9 m)
+SET_SOURCE_FILES_PROPERTIES(${SOURCES} PROPERTIES COMPILE_FLAGS -g)
diff --git a/p9/main.c b/p9/main.c
new file mode 100644
index 0000000..c75fea9
--- /dev/null
+++ b/p9/main.c
@@ -0,0 +1,89 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int gen(int p, int pmax, int q)
+{
+ int a, b, c;
+
+ for (; p < pmax; p += 2)
+ {
+ int _q = q;
+
+ for (; _q < p; _q += 2)
+ {
+ if (!pythagorean_triplet(p, _q, &a, &b, &c))
+ continue;
+
+ printf("p:%d q:%d a:%d\tb:%d\tc:%d\n", p, _q, a, b, c);
+
+ if (a+b+c == 1000)
+ goto found;
+
+ if (1000 % (a+b+c) == 0)
+ {
+ int m = 1000 / (a+b+c);
+
+ printf("triplet sum (%d) is divisor of 1000, multiply by %d\n",
+ a+b+c, m);
+
+ a *= m;
+ b *= m;
+ c *= m;
+
+ if (a+b+c == 1000)
+ goto found;
+ }
+ }
+ }
+
+ return 0;
+
+found:
+
+ printf("got it: a:%d b:%d c:%d a*b*c=%d\n", a, b, c, a*b*c);
+
+ return 1;
+}
+
+int main(int argc, char **argv)
+{
+ // some different ways to generate pythagorean triples, let's hope one of
+ // these yields the result
+
+ if (gen(3, 20, 3))
+ return 0;
+
+ printf("--\n");
+
+ if (gen(3, 20, 2))
+ return 0;
+
+ printf("--\n");
+
+ if (gen(2, 20, 3))
+ return 0;
+
+ // and of course the brute-force way :-) (this could be optimized by
+ // making the condition b<1000 -> b<1000-a and just stating c=1000-a-b
+ // instead of iterating values of c).
+
+ // int a, b, c;
+
+ // for (a = 1; a < 1000; a++)
+ // {
+ // for (b = a+1; b < 1000; b++)
+ // {
+ // for (c = b+1; c < 1000; c++)
+ // {
+ // if ((a + b + c == 1000) && (a*a + b*b == c*c))
+ // {
+ // printf("a:%d b:%d c:%d a*b*c:%d\n", a, b, c, a*b*c);
+ // return 0;
+ // }
+ // }
+ // }
+ // }
+
+ return 0;
+}
+