From 77013470c114ea58e6a39775f4d36b88dd5a5620 Mon Sep 17 00:00:00 2001 From: andreoss Date: Mon, 7 Sep 2015 12:45:43 -0400 Subject: [PATCH] [euler] problem 60 --- categories/euler/prob060-andreoss.pl | 68 ++++++++++++++++++++++++++++ categories/euler/prob066-andreoss.pl | 2 +- categories/euler/prob080-andreoss.pl | 2 +- t/categories/euler.t | 11 +++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 categories/euler/prob060-andreoss.pl diff --git a/categories/euler/prob060-andreoss.pl b/categories/euler/prob060-andreoss.pl new file mode 100644 index 0000000..dc768f3 --- /dev/null +++ b/categories/euler/prob060-andreoss.pl @@ -0,0 +1,68 @@ +use v6; + +=begin pod + +=TITLE Prime pair sets + +=AUTHOR Andrei Osipov + +The primes 3, 7, 109, and 673, are quite remarkable. By taking any two +primes and concatenating them in any order the result will always be +prime. For example, taking 7 and 109, both 7109 and 1097 are +prime. The sum of these four primes, 792, represents the lowest sum +for a set of four primes with this property. + +Find the lowest sum for a set of five primes for which any two primes +concatenate to produce another prime. + +=end pod + +subset Prime of Int where *.is-prime; + + +sub infix:«R»($a, $b) { + +( $a ~ $b ) & +( $b ~ $a ) ~~ Prime +} + +multi are-remarkable() { True } +multi are-remarkable($a) { True } +multi are-remarkable($a, *@xs) { + $a R @xs.all +} + +sub get-remarkable( :@primes is copy + , *@sequence + ) { + gather while my $x = @primes.shift { + if are-remarkable $x, @sequence { + take(|@sequence , $x) if @sequence; + take $_ + for get-remarkable + :@primes + , @sequence, $x + } + } +} + +sub MAIN( Int :$limit = 10_000 + , Bool :$verbose = False + , Int :$size = 5 + ) { + + my @primes = grep Prime , 1 .. $limit; + + for get-remarkable :@primes -> @r { + + say @r.perl if $verbose ; + + if @r == $size { + $verbose + ?? say "The sequence is @r[], the sum is {[+] @r}" + !! say [+] @r + and last + } + } + + say "Done in {now - BEGIN now}." if $verbose; +} + diff --git a/categories/euler/prob066-andreoss.pl b/categories/euler/prob066-andreoss.pl index e7cadca..5b0ded2 100644 --- a/categories/euler/prob066-andreoss.pl +++ b/categories/euler/prob066-andreoss.pl @@ -34,7 +34,7 @@ Find the value of D ≤ 1000 in minimal solutions of x for which the largest value of x is obtained. -The following algoritm was used for the solution: +The following algorithm was used for the solution: L =end pod diff --git a/categories/euler/prob080-andreoss.pl b/categories/euler/prob080-andreoss.pl index 9a737ea..917003e 100644 --- a/categories/euler/prob080-andreoss.pl +++ b/categories/euler/prob080-andreoss.pl @@ -18,7 +18,7 @@ sums of the first one hundred decimal digits for all the irrational square roots. -The following algoritm was used for the solution: +The following algorithm was used for the solution: L =end pod diff --git a/t/categories/euler.t b/t/categories/euler.t index 1eab06b..7756fed 100644 --- a/t/categories/euler.t +++ b/t/categories/euler.t @@ -456,6 +456,17 @@ subtest { check-example-solutions($problem, $expected-output, @authors) }, "prob059"; +subtest { + plan 1; + + my $problem = "prob060"; + my @authors = ; + my $expected-output = 26033; + + check-example-solutions($problem, $expected-output, @authors) +}, "prob060"; + + subtest { plan 2;