-
Notifications
You must be signed in to change notification settings - Fork 34
/
pascal_powers_of_two_triangle.pl
executable file
·79 lines (57 loc) · 1.76 KB
/
pascal_powers_of_two_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 06 May 2019
# https://github.com/trizen
# Generate a visual representation of the Pascal powers of two triangle.
# OEIS sequence:
# https://oeis.org/A307433
use 5.010;
use strict;
use warnings;
use Imager qw();
use Math::GMPz;
use experimental qw(signatures);
sub is_power_of_two ($n) {
(($n) & ($n - 1)) == 0;
}
my $two_power = 10;
my $size = 1 << $two_power;
my $img = Imager->new(xsize => $size, ysize => $size);
my $black = Imager::Color->new('#000000');
my $red = Imager::Color->new('#ff00000');
$img->box(filled => 1, color => $black);
my $ONE = Math::GMPz->new(1);
sub map_value {
my ($value, $in_min, $in_max, $out_min, $out_max) = @_;
((($value - $in_min) * ($out_max - $out_min)) / ($in_max - $in_min)) + $out_min;
}
sub pascal_powers_of_two {
my ($rows) = @_;
my @row = ($ONE);
foreach my $n (1 .. $rows) {
my $i = 0;
my $offset = ($rows - $n) / 2;
foreach my $elem (@row) {
my $t = Math::GMPz::Rmpz_sizeinbase($elem, 2);
my $hue = ($elem == 1) ? 0 : map_value($t, 0, 1 << ($two_power - 1), 1, 360);
$img->setpixel(
x => $offset + $i++,
y => $n,
color => {
hsv => [$hue, 1, ($elem == 1) ? 0 : 1]
}
);
}
if ($n <= 11) {
say "@row";
}
#<<<
@row = ($ONE, (map {
my $t = $row[$_] + $row[$_ + 1];
is_power_of_two($t) ? $t : $ONE;
} 0 .. $n - 2), $ONE);
#>>>
}
}
pascal_powers_of_two($size);
$img->write(file => "pascal_powers_of_two_triangle.png");