-
Notifications
You must be signed in to change notification settings - Fork 33
/
right_triangle_primes.pl
executable file
·62 lines (50 loc) · 1.15 KB
/
right_triangle_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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 11 April 2015
# https://github.com/trizen
# A number triangle, with the primes highlighted in blue
# (there are some lines that have more primes than others)
use 5.010;
use strict;
use warnings;
use GD::Simple;
use ntheory qw(is_prime);
my $n = 1000000; # duration: about 5 seconds
sub limit {
my ($n) = @_;
(sqrt(8 * $n + 1) - 1) / 2;
}
sub round {
my ($n) = @_;
($n**2 + $n) / 2;
}
my $lim = int(limit($n));
my $num = round($lim);
# create a new image
my $img = GD::Simple->new($lim, $lim);
my $counter = 1;
my $white = 1;
$img->fgcolor('white');
foreach my $i (0 .. $lim - 1) {
$img->moveTo(0, $i);
foreach my $j (0 .. $i) {
##print $counter, ' ';
if (is_prime($counter)) {
if ($white) {
$img->fgcolor('blue');
$white = 0;
}
}
elsif (not $white) {
$img->fgcolor('white');
$white = 1;
}
$img->line(1);
++$counter;
}
##print "\n";
}
open my $fh, '>:raw', 'right_triangle_primes.png';
print $fh $img->png;
close $fh;