Skip to content

Commit ef0f149

Browse files
committed
[euler] problem 38
1 parent a115e2f commit ef0f149

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

categories/euler/prob038-andreoss.pl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use v6;
2+
3+
=begin pod
4+
5+
=TITLE Pandigital multiples
6+
7+
=AUTHOR Andrei Osipov
8+
9+
L<https://projecteuler.net/problem=38>
10+
11+
Take the number 192 and multiply it by each of 1, 2, and 3:
12+
13+
192 × 1 = 192
14+
192 × 2 = 384
15+
192 × 3 = 576
16+
17+
By concatenating each product we get the 1 to 9 pandigital,
18+
192384576. We will call 192384576 the concatenated product of 192 and
19+
(1,2,3)
20+
21+
The same can be achieved by starting with 9 and multiplying by 1, 2,
22+
3, 4, and 5, giving the pandigital, 918273645, which is the
23+
concatenated product of 9 and (1,2,3,4,5).
24+
25+
What is the largest 1 to 9 pandigital 9-digit number that can be
26+
formed as the concatenated product of an integer with (1,2, ... , n)
27+
where n > 1?
28+
29+
=end pod
30+
31+
sub concat-product($x, $n) {
32+
+ [~] do for 1...$n { $x * $_ }
33+
}
34+
35+
sub is-pandigital(Int $n is copy) {
36+
return unless 123456789 <= $n <= 987654321;
37+
my $x = 0;
38+
loop ( ; $n != 0 ; $n div=10) {
39+
my $d = $n mod 10;
40+
$x += $d * 10 ** (9 - $d);
41+
}
42+
$x == 123456789;
43+
}
44+
45+
say max gather for 1 .. 9999 -> $x {
46+
next if $x !~~ /^^9/;
47+
for 2 .. 5 -> $n {
48+
my $l = concat-product $x, $n;
49+
take $l if is-pandigital $l;
50+
}
51+
}

t/categories/euler.t

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use v6;
22

33
use Test;
44

5-
plan 57;
5+
plan 58;
66

77
subtest {
88
plan 5;
@@ -361,6 +361,16 @@ subtest {
361361
check-example-solutions($problem, $expected-output, @authors)
362362
}, "prob036";
363363

364+
subtest {
365+
plan 1;
366+
367+
my $problem = "prob038";
368+
my @authors = <andreoss>;
369+
my $expected-output = 932718654;
370+
371+
check-example-solutions($problem, $expected-output, @authors)
372+
}, "prob038";
373+
364374
subtest {
365375
plan 1;
366376

0 commit comments

Comments
 (0)