Solved 009: php, ruby, c
This commit is contained in:
23
ProjectEuler/009/desc.yml
Normal file
23
ProjectEuler/009/desc.yml
Normal 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
24
ProjectEuler/009/solve.c
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
ProjectEuler/009/solve.php
Normal file
11
ProjectEuler/009/solve.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
9
ProjectEuler/009/solve.rb
Normal file
9
ProjectEuler/009/solve.rb
Normal 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
|
Reference in New Issue
Block a user