-
Notifications
You must be signed in to change notification settings - Fork 0
/
u.pl
82 lines (59 loc) · 2.02 KB
/
u.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 25 March 2019
# https://github.com/trizen
# Generate a visual representation of the Pascal-Fibonacci triangle.
# Definition by Elliott Line, Mar 22 2019:
# Consider a version of Pascal's Triangle: a triangular array with a single 1 on row 0,
# with numbers below equal to the sum of the two numbers above it if and only if that sum
# appears in the Fibonacci sequence. If the sum is not a Fibonacci number, `1` is put in its place.
# OEIS sequence:
# https://oeis.org/A307069
use 5.010;
use strict;
use warnings;
use Imager qw();
use ntheory qw(is_square :all);
use experimental qw(signatures);
use Math::GMPz;
sub is_fibonacci($n) {
my $m = 5 * $n * $n;
is_square($m - 4) or is_square($m + 4);
}
my $size = 500; # the size of the triangle
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 pascal_prime_power_triangle {
my ($rows) = @_;
my $ONE = Math::GMPz->new(1);
my $TWO = Math::GMPz->new(2);
my @row = ($ONE);
foreach my $n (1 .. $rows - 1) {
my $i = 0;
my $offset = ($rows - $n) / 2;
foreach my $elem (@row) {
my $ln = (1+log(sprintf('%s', 1+abs($elem))));
$img->setpixel(
x => $offset + $i++,
y => $n,
color => {
hsv => [$elem <= 1 ? 0 : (360 / $ln), 1, 1 - 1 / $ln]
}
);
}
last if grep{length("$_") > 70} @row;
if ($n < 40) {
say "@row";
}
#<<<
@row = ($ONE, (map {
my $t = $row[$_] + $row[$_ + 1];
(!is_square_free($t) ) ? $t : $TWO;
} 0 .. $n - 2), $ONE);
#>>>
}
}
pascal_prime_power_triangle($size);
$img->write(file => "squarefree2.png");