aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2020-09-04 21:24:50 +0200
committerLubos Kolouch <lubos@kolouch.net>2020-09-04 21:24:50 +0200
commit96359f89a3f6ff89b93965600fc879769188a6cd (patch)
tree8bf1f78ef29c8ed1fe9247966b96a9eb3212cf78
parent585b92ddc6f2fea7740005f36c933b62f1161111 (diff)
downloadperlweeklychallenge-club-96359f89a3f6ff89b93965600fc879769188a6cd.tar.gz
perlweeklychallenge-club-96359f89a3f6ff89b93965600fc879769188a6cd.tar.bz2
perlweeklychallenge-club-96359f89a3f6ff89b93965600fc879769188a6cd.zip
Solution 076 Task 1 Perl LK
-rw-r--r--challenge-076/lubos-kolouch/perl/ch-1.pl48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-076/lubos-kolouch/perl/ch-1.pl b/challenge-076/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..f4f1c54b7c
--- /dev/null
+++ b/challenge-076/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/
+#
+# Task 1 - Prime Sum
+#
+# AUTHOR: Lubos Kolouch
+#===============================================================================
+
+use strict;
+use warnings;
+use Math::Prime::Util qw/is_prime/;
+
+sub get_nr_primes {
+ my $what = shift;
+
+ # if the number is prime, we need just 1 number to represent it
+
+ return 1 if is_prime($what);
+
+ # if the number is even, we need 2 primes thanks to Goldbach's conjecture
+ return 2 if $what % 2 == 1;
+
+ # if the number - 2 is prime, return 2
+ return 2 if is_prime($what - 2);
+
+ # if the number -3 is prime, return 2 (3 and the prime)
+ return 2 if is_prime($what - 3);
+
+ # otherwise return 3 - it is 3 and 2 primes forming $what - 3 thanks to
+ # Goldbach's conjecture
+
+ return 3
+}
+
+
+use Test::More;
+
+is(get_nr_primes(9), 2, 'test 9');
+is(get_nr_primes(10), 2, 'test 10');
+is(get_nr_primes(2), 1, 'test 2');
+
+done_testing;