Debian Codegraph @

tests/inputs/prob060-andreoss.p6

raw
1 use v6;
2
3 # https://github.com/Raku/examples/raw/master/categories/euler/prob060-andreoss.pl
4
5 =begin pod
6
7 =TITLE Prime pair sets
8
9 =AUTHOR Andrei Osipov
10
11 The primes 3, 7, 109, and 673, are quite remarkable. By taking any two
12 primes and concatenating them in any order the result will always be
13 prime. For example, taking 7 and 109, both 7109 and 1097 are
14 prime. The sum of these four primes, 792, represents the lowest sum
15 for a set of four primes with this property.
16
17 Find the lowest sum for a set of five primes for which any two primes
18 concatenate to produce another prime.
19
20 =end pod
21
22 subset Prime of Int where *.is-prime;
23
24
25 sub infix:«R»($a, $b) {
26 +( $a ~ $b ) & +( $b ~ $a ) ~~ Prime
27 }
28
29 multi are-remarkable() { True }
30 multi are-remarkable($a) { True }
31 multi are-remarkable($a, *@xs) {
32 $a R @xs.all
33 }
34
35 sub get-remarkable( :@primes is copy
36 , *@sequence
37 ) {
38 gather while my $x = @primes.shift {
39 if are-remarkable $x, @sequence {
40 take(|@sequence , $x) if @sequence;
41 take $_
42 for get-remarkable
43 :@primes
44 , @sequence, $x
45 }
46 }
47 }
48
49 sub MAIN( Int :$limit = 10_000
50 , Bool :$verbose = False
51 , Int :$size = 5
52 ) {
53
54 my @primes = grep Prime , 1 .. $limit;
55
56 for get-remarkable :@primes -> @r {
57
58 say @r.perl if $verbose ;
59
60 if @r == $size {
61 $verbose
62 ?? say "The sequence is @r[], the sum is {[+] @r}"
63 !! say [+] @r
64 and last
65 }
66 }
67
68 say "Done in {now - BEGIN now}." if $verbose;
69 }
70