Skip to content

Instantly share code, notes, and snippets.

@dwskau
Created January 1, 2013 06:38
Show Gist options
  • Save dwskau/4425588 to your computer and use it in GitHub Desktop.
Save dwskau/4425588 to your computer and use it in GitHub Desktop.
Pythagorean Triples
{"description":"Pythagorean Triples","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"tab":"edit","display_percent":0.7,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"hidepanel":false}
//draws all pythagorean triples with perimeters less than specified
//max perimeter (CAUTION: high values take a long time to calculate)
var perimeter = 1000;
//storage for the triples
var triples = [];
//calculate the triples (brute force)
for(var c=1; c<perimeter; c++)
for(var b=1; b<(perimeter-c); b++)
for(var a=1; a<(perimeter-(b+c)); a++)
if((a*a + b*b) === c*c)
triples.push({A:a, B:b, C:c});
//draw the triples
var svg = d3.select("svg");
var triangles = svg.append("svg:g");
triangles.selectAll().data(triples).enter().append('svg:line')
.attr("stroke", "black")
.attr("stroke-width",.1)
.attr("stroke-opacity", 1)
.attr("x1",function(d){return d.A})
.attr("y2",function(d){return d.B})
.attr("transform", "scale(" + 3 + ")")
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment