Skip to content

Instantly share code, notes, and snippets.

@samueljackson92
Created November 12, 2012 17:39
Show Gist options
  • Save samueljackson92/4060750 to your computer and use it in GitHub Desktop.
Save samueljackson92/4060750 to your computer and use it in GitHub Desktop.
Generating a sierpinski carpet with a perl script
#!/usr/bin/perl
use strict;
use warnings;
my @carpet;
my $total;
carpet(2);
foreach my $row(@carpet){
foreach my $val(@$row){
print "$val ";
}
print "\n";
}
print "Total number of 1's:\t$total\n";
sub carpet {
my $limit = -1 + 3**shift;
for my $x (0 .. $limit) {
for my $y (0 .. $limit) {
my $result = isPixelFilled($x, $y);
$carpet[$x][$y] = $result;
$total += $result;
}
}
}
sub isPixelFilled {
my ($x, $y) = @_;
while($x>0 || $y>0)
{
if($x%3==1 && $y%3==1) {
return 0;
}
$x /= 3;
$y /= 3;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment