-
Notifications
You must be signed in to change notification settings - Fork 34
/
pascal_special_triangle.pl
executable file
·96 lines (68 loc) · 2.25 KB
/
pascal_special_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 06 May 2019
# https://github.com/trizen
# Generate a visual representation of a special Pascal triangle, where all entries satisfy a certain condition.
# If the sum of the two numbers above in the triangle does not satisfy the condition, then we put a constant value in its place.
# OEIS sequences:
# https://oeis.org/A307116
# https://oeis.org/A307433
use 5.010;
use strict;
use warnings;
use Imager qw();
use ntheory qw(:all);
use Math::AnyNum;
use experimental qw(signatures);
my $VALUE = Math::AnyNum->new(2); # constant value
my $size = 1000;
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);
sub isok ($n) { # condition
kronecker($n - 1, $n) == 1;
}
sub map_value ($value, $in_min, $in_max, $out_min, $out_max) {
((($value - $in_min) * ($out_max - $out_min)) / ($in_max - $in_min)) + $out_min;
}
sub special_pascal_triangle ($rows) {
my @rows;
my @row = ($VALUE);
foreach my $n (1 .. $rows) {
push @rows, [@row];
if ($n <= 10) {
say join(' ', map { $_->round } @row);
}
#<<<
@row = ($VALUE, (map {
my $t = $row[$_] + $row[$_ + 1];
isok($t) ? $t : $VALUE;
} 0 .. $n - 2), $VALUE);
#>>>
}
foreach my $row (@rows) {
@$row = map { log($_) } @$row;
}
my $min_value = vecmin(map { @$_ } @rows);
my $max_value = vecmax(map { @$_ } @rows);
say "Min: $min_value";
say "Max: $max_value";
foreach my $n (1 .. @rows) {
my $i = 0;
my $offset = ($rows - $n) / 2;
my $row = $rows[$n - 1];
foreach my $elem (@$row) {
my $hue = map_value($elem, $min_value, $max_value, 1, 360);
$img->setpixel(
x => $offset + $i++,
y => $n,
color => {
hsv => [$hue, 1, ($elem == $min_value) ? 0 : 1]
}
);
}
}
}
special_pascal_triangle($size);
$img->write(file => "special_pascal_triangle.png");