-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtriangle_primes_2.pl
More file actions
executable file
·50 lines (37 loc) · 949 Bytes
/
triangle_primes_2.pl
File metadata and controls
executable file
·50 lines (37 loc) · 949 Bytes
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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 18 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)
# Inspired by: https://www.youtube.com/watch?v=iFuR97YcSLM
use 5.010;
use strict;
use warnings;
use GD::Simple;
use ntheory qw(is_prime);
my $i = 1;
my $max = 2000; # duration: about 11 seconds
# create a new image
my $img = GD::Simple->new($max, $max);
my $white = 0;
$img->fgcolor('blue');
foreach my $x (1 .. $max) {
$img->moveTo(0, $x - 1);
foreach my $y (1 .. $x) {
if (is_prime($i)) {
$white = 0;
$img->fgcolor('blue');
}
elsif (not $white) {
$white = 1;
$img->fgcolor('white');
}
$img->line(1);
++$i;
}
}
open my $fh, '>:raw', 'triangle_primes_2.png';
print $fh $img->png;
close $fh;