-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler36.pl
More file actions
executable file
·40 lines (39 loc) · 872 Bytes
/
euler36.pl
File metadata and controls
executable file
·40 lines (39 loc) · 872 Bytes
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
#!/usr/bin/perl -w
use strict;
sub ispal {
my $num = sprintf("%b", shift);
@_ = split //, $num;
foreach (1..int(@_/2)) {
return 0 unless ((shift @_)==(pop @_));
}
1;
}
my $length;
my $sum=0;
for (my $k=1; $k<1000; $k++) {
$length = length($k);
if ($length == 1) {
next unless ($k%2);
$_ = $k;
$sum += $_ if (ispal($_));
$_ = $k.$k;
$sum += $_ if (ispal($_));
}
elsif ($length == 2) {
$k =~ /(\d)(\d)/;
next unless ($1%2);
$_ = $1.$2.$1;
$sum += $_ if (ispal($_));
$_ = $1.$2.$2.$1;
$sum += $_ if (ispal($_));
}
else {
$k =~ /(\d)(\d)(\d)/;
next unless ($1%2);
$_ = $1.$2.$3.$2.$1;
$sum += $_ if (ispal($_));
$_ = $1.$2.$3.$3.$2.$1;
$sum += $_ if (ispal($_));
}
}
print "$sum\n";