Skip to content

Instantly share code, notes, and snippets.

@dwskau
Created January 1, 2013 21:41
Show Gist options
  • Save dwskau/4430241 to your computer and use it in GitHub Desktop.
Save dwskau/4430241 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.630672268907563,"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 = 635;
//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");
var max = d3.max(triples, function(d){return d.B/d.A});
var colorScale = d3.scale.linear()
.range(["#FF0000", "#0000FF"])
triangles.selectAll().data(triples).enter().append('line')
.attr({
"x1":function(d){return d.A},
"y2":function(d){return d.B},
"transform": "scale(" + 5 + ")"
})
.style({
"stroke": function(d,i) {
return colorScale(Math.min(d.A/d.B, d.B/d.A));
},
"stroke-width":0.35693,
"stroke-opacity": 0.31329
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment