aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-04-02 19:31:21 +0100
committerGitHub <noreply@github.com>2022-04-02 19:31:21 +0100
commit7cdfd8d8fcf26a8b77f655e355e2a6708a6bd044 (patch)
treec45d3896a70da0b70ab4750e856e530656f19bc7
parentd4623b0f8f6459704d5e71d3e09dd779ca07f05b (diff)
parent13a37ba59af22a3b2d285604cbb2fb4d5184e57c (diff)
downloadperlweeklychallenge-club-7cdfd8d8fcf26a8b77f655e355e2a6708a6bd044.tar.gz
perlweeklychallenge-club-7cdfd8d8fcf26a8b77f655e355e2a6708a6bd044.tar.bz2
perlweeklychallenge-club-7cdfd8d8fcf26a8b77f655e355e2a6708a6bd044.zip
Merge pull request #5871 from LubosKolouch/master
feat(challenge-158/lubos-kolouch/p[erl,ython]/ch-2.p[ly]): Challenge 158 Task 2 LK Perl Python
-rw-r--r--challenge-158/lubos-kolouch/perl/ch-2.pl45
-rw-r--r--challenge-158/lubos-kolouch/python/ch-2.py41
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-158/lubos-kolouch/perl/ch-2.pl b/challenge-158/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..1c584e73c2
--- /dev/null
+++ b/challenge-158/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,45 @@
+package main;
+use strict;
+use warnings;
+use Math::Prime::Util qw/is_prime/;
+
+sub get_eq {
+
+# warning - waste here, it is ready also for the second series, even though we were not asked for it
+# this is a bad practice and one of the 7 wastes, but I just accept that I am aware of it and leave it.
+ my ( $y, $incr ) = @_;
+
+ my $x = $y + $incr;
+
+ return ( $x**3 - $y**3 ) / ( $x - $y );
+}
+
+sub get_cuban_primes {
+ my $limit = shift;
+
+ my @primes;
+ my $result = 0;
+ my $y = 0;
+
+# it would be probabl safer to keep running for a while even after we get over the limit...
+# maybe next values from the function(s) could drop again below the limit
+# but it passes the test and I am a lazy programmer to check the actual outputs from the function,
+# so let's just leave it ;)
+
+ while ( $result <= $limit ) {
+ $y++;
+
+ $result = get_eq( $y, 1 );
+ push @primes, $result if is_prime($result);
+ }
+
+ return \@primes;
+}
+
+use Test::More;
+
+is_deeply( get_cuban_primes(1000),
+ [ 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919 ] );
+
+done_testing;
+1;
diff --git a/challenge-158/lubos-kolouch/python/ch-2.py b/challenge-158/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..4ed051489c
--- /dev/null
+++ b/challenge-158/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,41 @@
+""" Challenge 158 Task 2 """
+from sympy import isprime
+
+
+def get_eq(y: int, incr: int) -> float:
+ """Solve the Cuban prime equation"""
+
+ # warning - waste here, it is ready also for the second series, even though we were
+ # not asked for it. This is a bad practice and one of the 7 wastes, but I just
+ # accept that I am aware of it and leave it.
+
+ x = y + incr
+ return (x**3 - y**3) / (x - y)
+
+
+def get_cuban_primes(limit: int) -> list:
+ """Do the exercise"""
+
+ primes = []
+ result = 0.0
+ y = 0
+
+ # it would be probabl safer to keep running for a while even after we get over the limit...
+ # maybe next values from the function(s) could drop again below the limit
+ # but it passes the test and I am a lazy programmer to check the actual outputs from
+ # the function, so let's just leave it ;)
+
+ while result <= limit:
+ y += 1
+
+ result = get_eq(y, 1)
+ if result != int(result):
+ continue
+
+ if isprime(int(result)):
+ primes.append(int(result))
+
+ return primes
+
+
+assert get_cuban_primes(1000) == [7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919]