-
Notifications
You must be signed in to change notification settings - Fork 34
/
reversed_prime_triangles.pl
executable file
·102 lines (81 loc) · 2.37 KB
/
reversed_prime_triangles.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 26 July 2015
# Website: https://github.com/trizen
# Generate a reversed set of number triangles
# with the prime numbers represented by blue pixels.
use 5.010;
use strict;
use warnings;
use Imager qw();
use ntheory qw(is_prime);
sub triangle {
my ($rows, $type) = @_;
my @triangle = ([1]);
my $n = 1;
foreach my $i (1 .. $rows) {
if ($type == 1) {
foreach my $j (0 .. $#triangle) {
push @{$triangle[$j]}, ++$n;
unshift @{$triangle[$j]}, ++$n;
}
}
elsif ($type == 2) {
foreach my $j (reverse 0 .. $#triangle) {
push @{$triangle[$j]}, ++$n;
unshift @{$triangle[$j]}, ++$n;
}
}
elsif ($type == 3) {
foreach my $j (0 .. $#triangle) {
unshift @{$triangle[$j]}, ++$n;
}
foreach my $j (reverse 0 .. $#triangle) {
push @{$triangle[$j]}, ++$n;
}
}
elsif ($type == 4) {
foreach my $j (reverse 0 .. $#triangle) {
unshift @{$triangle[$j]}, ++$n;
}
foreach my $j (0 .. $#triangle) {
push @{$triangle[$j]}, ++$n;
}
}
else {
die "Invalid type: $type";
}
unshift @triangle, [++$n];
}
return \@triangle;
}
sub triangle2img {
my ($triangle) = @_;
my $rows = $#{$triangle} + 1;
my $blue = Imager::Color->new('#0000FF');
my $white = Imager::Color->new('#FFFFFF');
my $img = Imager->new(xsize => $rows * 2, ysize => $rows);
$img->box(filled => 1, color => $white);
foreach my $i (0 .. $rows - 1) {
my $row = $triangle->[$i];
foreach my $j (0 .. $#{$row}) {
my $num = $row->[$j];
if (is_prime($num)) {
$img->setpixel(x => $rows - $i + $j, y => $i, color => $blue);
}
else {
$img->setpixel(x => $rows - $i + $j, y => $i, color => $white);
}
}
}
return $img;
}
my $max = 4;
my $rows = 1000;
foreach my $i (1 .. $max) {
say "** Generating triangle $i of $max...";
my $triangle = triangle($rows, $i);
my $img = triangle2img($triangle);
$img->write(file => "reversed_triangle_$i.png");
}