Skip to content

Instantly share code, notes, and snippets.

@mpfund
Created March 10, 2015 16:43
Show Gist options
  • Save mpfund/b8a31562cd730820c79e to your computer and use it in GitHub Desktop.
Save mpfund/b8a31562cd730820c79e to your computer and use it in GitHub Desktop.
Prime Factors
<script src="bignumber.js"></script>
<canvas id='c' width="200" height="200">
</canvas>
<script>
function prime_factors(n){
var factors = [];
var d = 2;
while (n > 1){
while (n % d == 0){
factors.push(d);
n = parseInt(n/d);
}
d = d + 1;
}
return factors
}
var k = document.getElementById('c');
var ctx = c.getContext('2d');
ctx.fillStyle = "black";
for(var x=0;x<40000;x++){
var primes =prime_factors(x);
for(var k=0;k<primes.length;k++){
var index =primes[k] +1;
var posX = index % 200;
var posY = parseInt(index / 200);
ctx.fillRect(posX,posY,1,1);
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment