From 3ab32fb2bb35283b74cedd96f961503004e27395 Mon Sep 17 00:00:00 2001 From: mjfernez Date: Sun, 9 Feb 2020 17:07:06 -0500 Subject: Added solution for 09 in C --- 09-Special-Pythagorean-Triplet/.pyth.py.swp | Bin 0 -> 12288 bytes 09-Special-Pythagorean-Triplet/pyth | Bin 0 -> 16704 bytes 09-Special-Pythagorean-Triplet/pyth.c | 24 ++++++++++++++++++++++++ 09-Special-Pythagorean-Triplet/pyth.py | 4 ---- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 09-Special-Pythagorean-Triplet/.pyth.py.swp create mode 100755 09-Special-Pythagorean-Triplet/pyth create mode 100644 09-Special-Pythagorean-Triplet/pyth.c diff --git a/09-Special-Pythagorean-Triplet/.pyth.py.swp b/09-Special-Pythagorean-Triplet/.pyth.py.swp new file mode 100644 index 0000000..73d234c Binary files /dev/null and b/09-Special-Pythagorean-Triplet/.pyth.py.swp differ diff --git a/09-Special-Pythagorean-Triplet/pyth b/09-Special-Pythagorean-Triplet/pyth new file mode 100755 index 0000000..e778cfc Binary files /dev/null and b/09-Special-Pythagorean-Triplet/pyth differ 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 +#include +#include + +/* 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; +} diff --git a/09-Special-Pythagorean-Triplet/pyth.py b/09-Special-Pythagorean-Triplet/pyth.py index a37f026..67c227d 100644 --- a/09-Special-Pythagorean-Triplet/pyth.py +++ b/09-Special-Pythagorean-Triplet/pyth.py @@ -5,10 +5,6 @@ # There exists exactly one Pythagorean triplet for which a + b + c = 1000. # Find the product abc. -import PIL -import math - - def isTriple(abc): if len(abc) > 3: return False -- cgit v1.2.3