aboutsummaryrefslogtreecommitdiff
path: root/challenge-076/perlboy1967
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2020-09-06 09:16:39 +0000
committerNiels van Dijke <perlboy@cpan.org>2020-09-06 09:16:39 +0000
commitd344f7c9bff76b5b8864fb38d813611aef729ef8 (patch)
tree6a94740ee71193ed01172e13c1d993c7b3b39804 /challenge-076/perlboy1967
parent13803a7feacd492a37743417b8405054dc241a4e (diff)
downloadperlweeklychallenge-club-d344f7c9bff76b5b8864fb38d813611aef729ef8.tar.gz
perlweeklychallenge-club-d344f7c9bff76b5b8864fb38d813611aef729ef8.tar.bz2
perlweeklychallenge-club-d344f7c9bff76b5b8864fb38d813611aef729ef8.zip
Task 1
Diffstat (limited to 'challenge-076/perlboy1967')
-rwxr-xr-xchallenge-076/perlboy1967/perl/ch-1.pl63
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-076/perlboy1967/perl/ch-1.pl b/challenge-076/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..efbcc00fee
--- /dev/null
+++ b/challenge-076/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 076
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/
+#
+# Task 1 - Prime Sum
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+# Unbuffered STDOUT
+$|++;
+
+# Sort of cheating... using some CPAN modules doing all the hard work
+use Math::Prime::XS qw(primes);
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw(sum);
+
+my ($N) = @ARGV;
+
+die "Input must be integer value and >= 2"
+ unless (defined $N and $N =~ m#^[1-9][0-9]*$# and $N >= 2);
+
+my @primes = primes($N);
+my %primes = +map { $_ => 1 } @primes;
+
+my @solution;
+
+printf "%d Prime number candidates found to use for a solution\n",
+ scalar(@primes);
+
+print "Searching for a solution (be patient... this can take a while...\n";
+
+my $k = 1;
+
+WHILE:
+while ($k < scalar @primes) {
+ printf "Using %d primes...\n", $k + 1;
+ my $c = combinations(\@primes, $k);
+ while (my $a = $c->next) {
+ my $sum = sum(@$a);
+ my $left = $N - $sum;
+ if (exists $primes{$left} and !grep { $_ == $left } @$a) {
+ @solution = (@$a, $N -$sum);
+ last WHILE;
+ }
+ }
+ $k++;
+}
+
+print "Input:\n";
+printf "\t%s = %d\n\n", '$N', $N;
+
+print "Output:\n";
+if (scalar @solution) {
+ printf "\t%d as the sum of prime numbers (%s) is same as input number.\n",
+ scalar(@solution), join(', ', @solution);
+} else {
+ print "\tNo solution can be found.\n";
+}
+