diff options
author | mjfernez <mjfernez@gmail.com> | 2020-02-09 17:07:06 -0500 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2020-02-09 17:07:06 -0500 |
commit | 3ab32fb2bb35283b74cedd96f961503004e27395 (patch) | |
tree | 458a2291668b5d3be403571b7e9808c8937eb76f /09-Special-Pythagorean-Triplet/pyth.c | |
parent | c9a6a1b8a3ec380adaf74f0ce8d650e97e38f474 (diff) | |
download | Project_Euler_Solutions-3ab32fb2bb35283b74cedd96f961503004e27395.tar.gz |
Added solution for 09 in C
Diffstat (limited to '09-Special-Pythagorean-Triplet/pyth.c')
-rw-r--r-- | 09-Special-Pythagorean-Triplet/pyth.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/09-Special-Pythagorean-Triplet/pyth.c b/09-Special-Pythagorean-Triplet/pyth.c new file mode 100644 index 0000000..86a12b0 --- /dev/null +++ b/09-Special-Pythagorean-Triplet/pyth.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +/* Actually found a neat math solution to make this one quicker + * since (a + b + c) = 1000 + * and a^2 + b^2 = c^2 + * + * We can say: + * c = 1000 - (a + b) + * c^2 = (1000 - (a + b))^2 + * and + * a^2 + b^2 = (1000 - (a + b))^2 + * So you need to find an a and b that satisfy this + * We know a and b are less than 500 since c must be larger +*/ + +int main(){ + for(long a = 1; a < 500; a++) + for(long b = 1; b < 500; b++) + if(pow(a, 2) + pow(b, 2) == pow((1000 - (a + b)), 2)) + return printf("%d\n", a * b * (long) sqrt(pow(a, 2) + pow(b, 2))); + return 1; +} |