-
Notifications
You must be signed in to change notification settings - Fork 33
/
divisor_triangle.pl
executable file
·56 lines (41 loc) · 1.27 KB
/
divisor_triangle.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 14 September 2016
# Website: https://github.com/trizen
# Generates a triangle with non-prime and non-power numbers,
# each number connected through a line to its divisors.
use strict;
use warnings;
use Imager;
use ntheory qw(is_prime is_power divisors);
use POSIX qw(ceil);
use Memoize qw(memoize);
memoize('get_point');
my $limit = 10;
my $scale = 1000;
my $red = Imager::Color->new('#ff0000');
my $img = Imager->new(xsize => 2 * $limit * $scale,
ysize => $limit * $scale);
sub get_point {
my ($n) = @_;
my $row = ceil(sqrt($n));
my $cell = 2 * $row - 1 - $row**2 + $n;
($scale * $cell, $scale * $row);
}
foreach my $n (1 .. $scale) {
if (not is_prime($n) and not is_power($n)) {
my ($x1, $y1) = get_point($n);
foreach my $divisor (divisors($n)) {
my ($x2, $y2) = get_point($divisor);
$img->line(
x1 => ($limit * $scale - $y1 - 1) + $x1,
y1 => $y1,
x2 => ($limit * $scale - $y2 - 1) + $x2,
y2 => $y2,
color => $red
);
}
}
}
$img->write(file => 'divisor_triangle.png');