Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 30 lines (25 sloc) 0.58 kB
e54b6c4 @jtpalmer Problem 45, Perl 5.
authored
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use 5.010;
5
6 sub triangle {
7 my ($n) = @_;
8 return $n * ( $n + 1 ) / 2;
9 }
10
11 sub pentagonal {
12 my ($n) = @_;
13 return $n * ( 3 * $n - 1 ) / 2;
14 }
15
16 sub hexagonal {
17 my ($n) = @_;
18 return $n * ( 2 * $n - 1 );
19 }
20
21 my ( $ti, $pi, $hi ) = ( 285, 165, 143 );
22 $ti++;
23 my ( $t, $p, $h ) = ( triangle($ti), pentagonal($pi), hexagonal($hi) );
24 while ( $t != $p || $t != $h ) {
25 $t = triangle( ++$ti ) until $t >= $p && $t >= $h;
26 $p = pentagonal( ++$pi ) until $p >= $t;
27 $h = hexagonal( ++$hi ) until $h >= $t;
28 }
29 say $t;
Something went wrong with that request. Please try again.