/
ch-1.pl
executable file
·39 lines (31 loc) · 869 Bytes
/
ch-1.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
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
sub main {
my $length = shift;
# Sanity check
die "You must specify a number\n" if not defined $length;
die "The number is not a positive integer\n" unless $length =~ /^[1-9][0-9]*$/;
# Build a hash table with all the possible squares
my %squares = ();
my $counter = 0;
while ( $counter**2 < 2 * 10**$length ) {
++$counter;
$squares{ $counter**2 } = $counter;
}
my @solutions = ();
my $r = 10**( $length - 1 );
while ( $r < 10**$length ) {
my $r1 = reverse($r);
push @solutions, $r if ( $r >= $r1 and $squares{ $r - $r1 } and $squares{ $r + $r1 } );
++$r;
}
if ( scalar(@solutions) ) {
say join ' ', @solutions;
}
else {
say 'No solutions found';
}
}
main(@ARGV);