Wednesday, July 30, 2008

The Sieve of Eratosthenes

The Sieve of Eratosthenes is an ancient algorithm to calculate prime numbers. It involves starting at 2 and removing all numbers that can be divided by it evenly. You then go to the next number remaining and do the same, repeating until you have reached the end of your finite list of numbers.

Here is my perl implementation. Be gentle. I'm just learning the language. Post constructive comments if you have any ideas about what I could have done better.

Note: Blogger isn't allowing me to format the code properly. It all works*, but it looks ugly. It is much nicer looking in Komodo Edit and vim.

#!/usr/bin/perl -w
#The Sieve of Eratosthenes
use strict;

if($#ARGV != 0){
print "Usage: sieve.pl n \nn must be an integer greater than 1\n";
exit;
}
my $max = $ARGV[$1];
if($max < 2){
print "Argument must be larger than 1\n";
exit;
}
my @list = (1..$max);
my @sublist = ();
my @primes = ();
my $current = 0;
my $i = 0;
my $list_length = 0;

$list_length = @list;
my $nonzero = 1;
while ($nonzero){
for($i = $nonzero; $i < $list_length; $i++){
if($list[$i] != 0){
$nonzero = $i;
last;
}
elsif($i == $list_length - 1 && $list[$i] == 0){
$nonzero = 0;
last;
}
}
$current = $list[$nonzero];
for($i = $nonzero; $i < $list_length; $i++){
if($list[$i] % $current == 0){
$list[$i] = 0;
}
}
push(@primes, $current) if $nonzero;
}
print "@primes\n";

*There is something wrong with line 9. It works, but it says, "Use of uninitialized value in array element at sieve.pl line 9." I don't yet know how to fix this, but it returns the proper results.

Friday, July 25, 2008

Darkroom

I developed my own film for the first time a few days ago. Here is a gallery of the best shots from my first two rolls of film. The square ones were shot on my Mamiya C330 medium format TLR. The smaller rectangular shot was from my Canon EOS 630 35mm SLR.

Gallery

Since then, I managed to find the SLR I've been looking for over the past several years. I've never actively searched for it, I just planned to get one if I ever found one for a good price. The Canon EOS Elan IIe was on sale this week at Mpex. I got one for $30. It works great. The only pictures I've developed from it were on the end of the last roll I shot with the 630. That part of the roll was ruined because I didn't realize that my reel was a 24 exposure reel until I had my 36 exposure reel loaded on it and cut off of the cartridge. Those exposures range from severely underdeveloped to undeveloped. What I was able to see looked decent, but I can't tell for sure. I'll know more about that camera next time I develop film, which should be today or Sunday.

Thursday, July 24, 2008

12,456

Do I spend too much time on the web? Probably not.

This is Google Reader. From the large number, I assume it counts every article view to be a page visit. I've only been using Google Reader for a few months. I can't possibly have made over 12K separate visits.