-
Notifications
You must be signed in to change notification settings - Fork 33
/
pythagorean_triples.pl
executable file
·69 lines (50 loc) · 1.47 KB
/
pythagorean_triples.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 18 August 2016
# Website: https://github.com/trizen
# Generate Pythagorean triples whose sum goes up to a certain limit.
# See also: https://projecteuler.net/problem=75
# https://en.wikipedia.org/wiki/Pythagorean_triple
use 5.010;
use strict;
use warnings;
use ntheory qw(gcd);
sub pythagorean_triples {
my ($limit) = @_;
my @triples;
my $end = int(sqrt($limit));
foreach my $n (1 .. $end - 1) {
for (my $m = $n + 1 ; $m <= $end ; $m += 2) {
my $x = ($m**2 - $n**2);
my $y = (2 * $m * $n);
my $z = ($m**2 + $n**2);
last if $x + $y + $z > $limit;
if (gcd($n, $m) == 1) { # n and m coprime
my $k = 1;
while (1) {
my $x = $k * $x;
my $y = $k * $y;
my $z = $k * $z;
last if $x + $y + $z > $limit;
push @triples, [$x, $y, $z];
++$k;
}
}
}
}
map { $_->[1] } sort { $a->[0] <=> $b->[0] } map {
[$_->[0] + $_->[1] + $_->[2], [sort { $a <=> $b } @{$_}]]
} @triples;
}
my @triples = pythagorean_triples(50);
foreach my $triple (@triples) {
say "P(@$triple) = ", $triple->[0] + $triple->[1] + $triple->[2];
}
__END__
P(3 4 5) = 12
P(6 8 10) = 24
P(5 12 13) = 30
P(9 12 15) = 36
P(8 15 17) = 40
P(12 16 20) = 48