-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_52.pl
More file actions
executable file
·49 lines (43 loc) · 870 Bytes
/
problem_52.pl
File metadata and controls
executable file
·49 lines (43 loc) · 870 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
46
47
48
49
#! /usr/bin/perl
sub make_iter {
my $n = 1;
my $a = 10;
my @p;
sub {
while( $n += 3 ) {
if( $n > $a / 6 ) {
$n = $a + (3 - $a % 3) % 3;
$a *= 10;
}
my @d = split "", $n;
next if !grep { $_ == 0 or $_ == 5 } @d;
return $n;
}
}
}
sub compare {
my @a = @{shift()};
my @b = @{shift()};
my %am;
my %bm;
foreach (@a) { $am{$_}++; }
foreach (@b) { $bm{$_}++; }
foreach (keys %am, keys %bm) {
return 0 if $am{$_} != $bm{$_};
}
return 1;
}
my $iter = make_iter;
my $n;
while( $n = $iter->() ) {
my @n = split "", $n;
foreach (2 .. 6) {
my @t = split "", $_ * $n;
if( !compare( \@n, \@t ) ) {
goto NEXT;
}
}
last;
NEXT:
}
print "Answer: $n\n";