Solved 009: php, ruby, c

This commit is contained in:
FuryFire
2012-03-07 11:33:15 +01:00
parent 43cdbe80cb
commit 118a76b177
4 changed files with 67 additions and 0 deletions

23
ProjectEuler/009/desc.yml Normal file
View File

@ -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

24
ProjectEuler/009/solve.c Normal file
View File

@ -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;
}
}
}
}

View File

@ -0,0 +1,11 @@
<?php
for ($a = 1; $a < 1000; $a++) {
for ($b = 1; $b < 1000; $b++) {
//Make it run reverse in order to find solution quickly
$c = 1000 - $a - $b;
if (pow($a, 2) + pow($b, 2) == pow($c, 2)) {
echo $a * $b * $c;
die;
}
}
}

View File

@ -0,0 +1,9 @@
(1..1000).each do |a|
(1..1000).each do |b|
c = 1000 - a - b
if( a ** 2 + b ** 2 == c ** 2)
puts a * b *c
exit
end
end
end