Created
November 12, 2012 17:39
-
-
Save samueljackson92/4060750 to your computer and use it in GitHub Desktop.
Generating a sierpinski carpet with a perl script
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
#!/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