Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active August 29, 2015 14:00
Show Gist options
  • Save jmervine/16dbd9f599d201106712 to your computer and use it in GitHub Desktop.
Save jmervine/16dbd9f599d201106712 to your computer and use it in GitHub Desktop.
Pythagorean Triplet - run time of go vs. java vs. node vs. ruby vs. python vs. perl vs. php vs. c++

Pythagorean Triplet

Note: The method used below is very slow, with 1bb iterations when trying to find 1000. For a faster method see: https://gist.github.com/jmervine/b5d985398b3ca7ba16aa (~125k iterations)


A Pythagorean triplet is a set of three natural numbers, a b c, for which,

a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

From: https://github.com/Spencer-Allen/ProjectEuler/blob/master/Project_Euler_Q09.js

In all solutions, I tried to stay a true to the solution in the above.

Unsurprisingly, Go is the fastest and Node isn't that far behind. What's surprised me is how slow Ruby, Python and especially Perl were. I wasn't surprised at all at how slow PHP was.

Go - v1.2.1 linux/amd64
jmervine@laptop:/tmp$ time go run p.go 
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    0m2.315s
user    0m2.260s
sys     0m0.044s

# pre-compiled
jmervine@laptop:/tmp$ go build -o p_go p.go 
jmervine@laptop:/tmp$ time ./p_go 
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    0m1.913s
user    0m1.908s
sys     0m0.004s

Java OpenJDK 1.6.0_30
jmervine@laptop:~/Development$ time java p
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    0m2.252s
user    0m2.256s
sys     0m0.016s
Node v0.10.26
jmervine@laptop:/tmp$ time node p.js
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    0m9.133s
user    0m9.200s
sys     0m0.024s
C++
jmervine@laptop:~/Development$ time ./p.out
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    0m11.029s
user    0m11.012s
sys     0m0.000s
Ruby v1.9.3p194
jmervine@laptop:/tmp$ time ruby p.rb
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    6m5.558s
user    6m5.068s
sys     0m0.028s
Python v2.7.3
jmervine@laptop:/tmp$ time python p.py 
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    7m14.840s
user    7m14.336s
sys     0m0.008s
PHP v5.5.11-3
jmervine@laptop:/tmp$ time php p.php 
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    9m28.152s
user    9m27.472s
sys     0m0.020s

Perl v5.14.2
jmervine@laptop:~/Development$ time perl p.pl 
0 500 500 0
200 375 425 31875000
375 200 425 31875000
500 0 500 0

real    22m41.903s
user    22m39.352s
sys     0m0.356s
#include <iostream>
int sqr(int x) {
return x * x;
}
int p_find_trip(int final_sum) {
for (int a = 0; a < final_sum; a++) {
for (int b = 0; b < final_sum; b++) {
for (int c = 0; c < final_sum; c++) {
if (sqr(a) + sqr (b) == sqr(c) && (a+b+c) == final_sum) {
std::cout << a << " " << b << " " << c << " " << (a * b * c) << "\n";
}
}
}
}
return 0;
}
int main() {
return p_find_trip(1000);
}
package main
import (
"fmt"
)
func square(x int) int {
return x * x
}
func pTripFind(final_sum int) {
for a := 0; a < final_sum; a++ {
for b := 0; b < final_sum; b++ {
for c := 0; c < final_sum; c++ {
if square(a)+square(b) == square(c) && (a+b+c) == final_sum {
fmt.Println(a, b, c, (a * b * c))
}
}
}
}
}
func main() {
pTripFind(1000)
}
public class p {
public static void main(String[] args) {
pTripFind(1000);
}
public static void pTripFind(int finalSum) {
int a, b, c;
for (a=0; a<finalSum; a++){
for (b=0; b<finalSum; b++){
for (c=0; c<finalSum; c++){
if (square(a) + square(b) == square (c) && a + b + c == finalSum){
System.out.println(a + " " + b + " " + c + " " + a*b*c);
}
}
}
}
}
public static int square(int x) {
return x * x;
}
}
// From: https://github.com/Spencer-Allen/ProjectEuler/blob/master/Project_Euler_Q09.js
// and used as a base sample for all other solutions
var square = function (x){
return x*x;
};
function pTripFind(finalSum){
for (a=0; a<finalSum; a++){
for (b=0; b<finalSum; b++){
for (c=0; c<finalSum; c++){
if (square(a) + square(b) === square (c) && a + b + c === finalSum){
console.log (a,b,c,a*b*c);
}
}
}
}
}
pTripFind(1000);
<?php
$square = function($x) {
return $x*$x;
};
function pTripFind($final_sum) {
for ($a = 0; $a < $final_sum; $a++) {
for ($b = 0; $b < $final_sum; $b++) {
for ($c = 0; $c < $final_sum; $c++) {
if ($square($a) + $square($b) === $square($c) && ($a+$b+$c) === $final_sum) {
$p = $a*$b*$c;
print "{$a} {$b} {$c} {$p}";
}
}
}
}
}
pTripFind(1000);
#!/usr/bin/perl
use strict;
sub square {
my $n = shift;
return $n*$n;
}
sub pTripFind {
my $final_sum = shift;
my $a;
my $b;
my $c;
my $p;
for ($a = 0; $a < $final_sum; $a++) {
for ($b = 0; $b < $final_sum; $b++) {
for ($c = 0; $c < $final_sum; $c++) {
if (square($a) + square($b) == square($c) && ($a+$b+$c) == $final_sum) {
$p = $a*$b*$c;
print "$a $b $c $p\n";
}
}
}
}
}
pTripFind(1000);
def square(x):
return x*x
def p_trip_find(final_sum):
for a in xrange(final_sum):
for b in xrange(final_sum):
for c in xrange(final_sum):
if (square(a) + square(b) == square(c) and a + b + c == final_sum):
print "{} {} {} {}".format(a, b, c, a*b*c)
p_trip_find(1000)
def square(x)
x*x
end
def p_trip_find(final_sum)
(0..final_sum).each do |a|
(0..final_sum).each do |b|
(0..final_sum).each do |c|
if square(a)+square(b) == square(c) && a+b+c === final_sum
puts "#{a} #{b} #{c} #{a*b*c}"
end
end
end
end
end
p_trip_find(1_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment