-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcircular_prime_triangle.pl
More file actions
executable file
·53 lines (39 loc) · 970 Bytes
/
Copy pathcircular_prime_triangle.pl
File metadata and controls
executable file
·53 lines (39 loc) · 970 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
51
52
53
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 02 April 2016
# https://github.com/trizen
# Generate a triangle with highlighted numbers in the form of: floor(sqrt(prime(i)^2 + i^2))
use 5.010;
use strict;
use warnings;
use Imager;
use List::Util qw(max);
use ntheory qw(nth_prime);
my %data;
sub generate {
my ($n) = @_;
foreach my $i (1 .. $n) {
undef $data{int(sqrt(nth_prime($i)**2 + $i * $i))};
}
return 1;
}
generate(100000);
my $i = 1;
my $j = 1;
my $max = max(keys %data);
my $limit = int(sqrt($max)) - 1;
# Create a new image
my $img = Imager->new(xsize => $limit * 2, ysize => $limit + 1);
my $red = Imager::Color->new(255, 0, 0);
for my $m (0 .. $limit) {
my $x = $limit - $m;
for my $n ($j .. $m**2) {
if (exists $data{$j}) {
$img->setpixel(x => $x, y => $m, color => $red);
}
++$x;
++$j;
}
}
$img->write(file => 'prime_triangle.png');