-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_38.pl
More file actions
executable file
·45 lines (39 loc) · 873 Bytes
/
problem_38.pl
File metadata and controls
executable file
·45 lines (39 loc) · 873 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
41
42
43
44
45
#! /usr/bin/perl
use strict;
use warnings;
use List::Util qw(first);
sub is_pandigital {
my %t ;
foreach (@_) {
foreach (split "", $_) {
return 0 if $_ == 0;
$t{$_}++;
return 0 if $t{$_} > 1;
}
}
return -1 if( first { !defined $t{$_} } (1 .. 9) );
return 1;
}
my $res = 0;
#918273645 is known solution
foreach ( [ 9, 9 ], [ 98, 91 ],
[ 987, 918 ], [ 9876, 9182 ] ) {
my $a = $_->[0];
my $b = $_->[1];
foreach( $b .. $a ) {
my $i = 2;
my $t = $_;
my $r = $_;
while( 1 ) {
$r .= $i * $t;
my $pd = is_pandigital( $r );
last unless $pd;
if( $pd == 1 ) {
$res = $r if $r > $res;
last;
}
$i++;
}
}
}
print "Answer: ", $res, "\n" ;