From 118a76b177978835db7cd215d6187e5fe25d07e9 Mon Sep 17 00:00:00 2001 From: FuryFire Date: Wed, 7 Mar 2012 11:33:15 +0100 Subject: [PATCH] Solved 009: php, ruby, c --- ProjectEuler/009/desc.yml | 23 +++++++++++++++++++++++ ProjectEuler/009/solve.c | 24 ++++++++++++++++++++++++ ProjectEuler/009/solve.php | 11 +++++++++++ ProjectEuler/009/solve.rb | 9 +++++++++ 4 files changed, 67 insertions(+) create mode 100644 ProjectEuler/009/desc.yml create mode 100644 ProjectEuler/009/solve.c create mode 100644 ProjectEuler/009/solve.php create mode 100644 ProjectEuler/009/solve.rb diff --git a/ProjectEuler/009/desc.yml b/ProjectEuler/009/desc.yml new file mode 100644 index 0000000..74b370f --- /dev/null +++ b/ProjectEuler/009/desc.yml @@ -0,0 +1,23 @@ +title: Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000. +url: http://projecteuler.net/problem=9 + +desc: | + A Pythagorean triplet is a set of three natural numbers, a b c, for which, + a^2 + b^2 = c^2 + For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + There exists exactly one Pythagorean triplet for which a + b + c = 1000. + Find the product abc. + +solution: | + Make a nested forloop for a and b in the range 1-1000 - Then c = 1000-a-b - Test if solution is valid. + +solutions: + solve.php: + desc: Basic solution + language: php + solve.rb: + desc: Basic solution + language: ruby + solve.c: + desc: ANSI C solution compiled with TCC + language: c \ No newline at end of file diff --git a/ProjectEuler/009/solve.c b/ProjectEuler/009/solve.c new file mode 100644 index 0000000..93c97fe --- /dev/null +++ b/ProjectEuler/009/solve.c @@ -0,0 +1,24 @@ +#include "stdio.h" +#include "math.h" + +int main( ) +{ + int a; + int b; + int c; + int cmp; + for(a = 1; a < 1000; a++) + { + for(b = 1; b < 1000; b++) + { + //Calculate the only valid value for c + c = 1000 - a - b; + if( pow(c,2) == (pow(a, 2 ) + pow( b, 2 ))) + { + int result = a * b * c; + printf("%i", result); + return 0; + } + } + } +} \ No newline at end of file diff --git a/ProjectEuler/009/solve.php b/ProjectEuler/009/solve.php new file mode 100644 index 0000000..539468b --- /dev/null +++ b/ProjectEuler/009/solve.php @@ -0,0 +1,11 @@ +