Last active
November 1, 2016 09:56
-
-
Save stefandz/f0a82ec2087ee66b09d1 to your computer and use it in GitHub Desktop.
Factor square drawer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final boolean monochrome = true; // in this mode, primes are black, non-primes are white | |
// otherwise darker colours have fewer factors, lighter colours have more | |
final int windowWidth= 640; | |
final int windowHeight= 320; | |
final int plotWidth = 3; | |
final int plotHeight = 5; | |
void settings() { | |
size(windowWidth, windowHeight); | |
} | |
void setup(){ | |
int[] factors = new int[plotWidth*plotHeight]; | |
for(int i=0; i<plotWidth*plotHeight; i++){ | |
factors[i] = numberOfFactors(i+1); | |
//println(factors[i]); // only if you're keen | |
} | |
int maxFactor = max(factors); | |
noStroke(); | |
for(int i=0; i<plotWidth*plotHeight; i++){ | |
if(monochrome){ | |
fill(factors[i]==2 ? 0 : 255); | |
} else { | |
fill(factors[i]*255/maxFactor); | |
} | |
rect((windowWidth/plotWidth)*(i%plotWidth), (windowHeight/plotHeight)*(i/plotWidth), (windowWidth/plotWidth), (windowHeight/plotHeight)); | |
} | |
} | |
void loop(){ | |
} | |
int numberOfFactors(int number){ | |
if(number<=0) return -1; // only positive integers count | |
if(number==1) return 1; // we treat 1 as a special case for ease | |
int retVal = 2; // all numbers have at least 2 factors (1 and themselves) | |
for(int i=2; i<number; i++){ // don't include 1 or the number | |
if(number%i==0) retVal++; | |
} | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment