Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmervine/b5d985398b3ca7ba16aa to your computer and use it in GitHub Desktop.
Save jmervine/b5d985398b3ca7ba16aa to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
int sqr(int x) {
return x * x;
}
int p_find_trip(int final_sum) {
int iterations = 0;
int stop = ((final_sum / 2) + 1);
for (int a = 0; a < stop; a++) {
int aSquare = sqr(a);
for (int b = a; b < stop; b++) {
iterations++;
int bSquare = sqr(b);
double cDbl = sqrt(double(aSquare) + double(bSquare));
int c = int(cDbl);
double cCheck = double(c);
if (cDbl == cCheck && (a + b + c) == final_sum) {
int abcMul = (a * b * c);
std::cout << a << " " << b << " " << c << " " << (a * b * c) << "\n";
std::cout << b << " " << a << " " << c << " " << (a * b * c) << "\n";
}
}
}
std::cout << "iterations: " << iterations << "\n";
return 0;
}
int main() {
return p_find_trip(1000);
}
package main
import (
"fmt"
"math"
)
func pTripFind(final_sum int) {
iterations := 0
stop := (final_sum / 2)+1
for a := 0; a < stop; a++ {
aSquare := a * a
for b := a; b < stop; b++ {
iterations++;
bSquare := b * b
cflt := math.Sqrt(float64(aSquare + bSquare))
c := int(cflt)
cchk := float64(c)
if cflt == cchk && (a+b+c) == final_sum {
abcMul := a * b * c
fmt.Println(a, b, c, abcMul)
fmt.Println(b, a, c, abcMul)
}
}
}
fmt.Printf("iterations: %d", iterations)
}
func main() {
pTripFind(1000)
}
'use strict';
var square = function (x){
return x*x;
};
function pTripFind(finalSum){
var iterations = 0;
var stop = (finalSum/2)+1;
var a, b, c, aSquare, bSquare;
for (a=0; a<stop; a++){
aSquare = square(a);
for (b=0; b<stop; b++){
iterations++;
bSquare = square(b);
c = Math.sqrt(aSquare + bSquare);
if (c === Math.round(c) && (a + b + c) == finalSum) {
console.log (a,b,c,a*b*c);
}
}
}
console.log("iterations:", iterations);
}
pTripFind(1000);
import math
def square(x):
return (x * x)
def p_trip_find(final_sum):
iterations = 0
stop = ((final_sum / 2) + 1)
for a in xrange(stop):
aSquare = square(a)
for b in range(a, stop):
iterations = iterations + 1
bSquare = square(b)
c = math.sqrt(aSquare + bSquare)
if (c == round(c) and (a + b + c) == final_sum):
print "{} {} {} {}".format(a, b, c, (a * b * c))
print "{} {} {} {}".format(b, a, c, (a * b * c))
print "iterations: {}".format(iterations)
p_trip_find(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment