-
Notifications
You must be signed in to change notification settings - Fork 34
/
zig-zag_primes.pl
executable file
·66 lines (52 loc) · 1.25 KB
/
zig-zag_primes.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 11 April 2015
# https://github.com/trizen
# A zig-zag matrix with the primes highlighted in blue
use 5.010;
use strict;
use warnings;
use GD::Simple;
use ntheory qw(is_prime);
sub zig_zag {
my ($w, $h) = @_;
#
## Code from: https://rosettacode.org/wiki/Zig-zag_matrix#Perl
#
my (@r, $n);
$r[$_->[1]][$_->[0]] = $n++
for
sort { $a->[0] + $a->[1] <=> $b->[0] + $b->[1] or ($a->[0] + $a->[1]) % 2 ? $a->[1] <=> $b->[1] : $a->[0] <=> $b->[0] }
map {
my $e = $_;
map { [$e, $_] } 0 .. $w - 1
} 0 .. $h - 1;
return \@r;
}
my $x = 1000;
my $y = 1000;
my $matrix = zig_zag($x, $y);
# create a new image
my $img = GD::Simple->new($x, $y);
my $white = 1;
$img->fgcolor('white');
foreach my $i (0 .. $x - 1) {
$img->moveTo(0, $i);
foreach my $j (0 .. $y - 1) {
if (is_prime($matrix->[$i][$j])) {
if ($white) {
$img->fgcolor('blue');
$white = 0;
}
}
elsif (not $white) {
$img->fgcolor('white');
$white = 1;
}
$img->line(1);
}
}
open my $fh, '>:raw', 'zig-zag_primes.png';
print $fh $img->png;
close $fh;