| KevinPluck.net | InvisiCalc | SuDoku Helper | Kakuro Combinations | rotxd.com |
|
Introduction Part 1 Part 2 Part 3 Part 4 Part 5 Part 6 More to come... Please add the factorbothering sub-reddit to your frontpage. |
Factor bothering - Part 1So let’s start, what's a factor? Basically it's a number (other than 1) that you multiply with another factor to get an integer. E.g. 2*2=4 so 2 is a factor of 4, 2*6=12, 3*4=12 so 2,6,3 and 4 are factors of 12. What I would like to do is have a look for any patterns in these factors, if any. We need to generate some factors first. I’ve created a little Perl script to do that: factorGenerator.pl
my $intToBeFactored=0;
my $intTrialDivisor=0;
my $intMaxTrialDivisor=0;
for($intToBeFactored=1 ; $intToBeFactored<=$ARGV[0] ; $intToBeFactored++)
{
$intMaxTrialDivisor=0.5+sqrt($intToBeFactored);
for($intTrialDivisor=2 ; $intTrialDivisor<$intMaxTrialDivisor ; $intTrialDivisor++)
{
if($intToBeFactored % $intTrialDivisor==0)
{
print("$intTrialDivisor ".$intToBeFactored/$intTrialDivisor." ");
}
}
print "\n";
}
The script is based on the trial divisor algorithm mainly because all the other ones did my head in. Here is some example output: C:\stuff\FactorBothering\Part1>factorGenerator.pl 12
2 2
2 3
2 4
3 3
2 5
2 6 3 4
(Yes, I'm on a windows machine) Each row shows the factors for that row count, so row 4 shows "2 2" while row 5, a prime, shows nothing. I'm committing heresy here by not including 1, but I'll live with it. Now we'll do some pretty stuff. First we need a good number of factors, say 1000. factorGenerator.pl 1000 > factors1000.txt
Have a look in factors1000.txt, we're going to plot that graphically now. I've coded a Python script (Perl's graphics libraries aren't worth the effort) that reads the above file and plots a pixel for each factor and marks out the prime numbers: plotFactors.py
import Image, ImageDraw
import os
size = 1000,1001
im=Image.new("1",size)
draw=ImageDraw.Draw(im)
draw.rectangle(((0,0),size),1) #set image bg to white
lineCount=0
factorFile=open('factors1000.txt','r')
for line in factorFile:
lineCount=lineCount+1
isPrime=1
for factor in line.split():
im.putpixel((int(factor)+10,lineCount),0)
isPrime=0
if isPrime:
draw.line(((4,lineCount),(6,lineCount)),0)
factorFile.close()
im.save("out.png","PNG")
And the resulting image:
There's certainly a lot of pretty detail there that we can explore! |