diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2022-04-01 19:25:47 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2022-04-01 19:25:47 +0200 |
| commit | ec2ba23f4b8a618f077f7310c77832631e57dd36 (patch) | |
| tree | 4449aec0d38d53cd18a06149973c78efa82991d1 | |
| parent | 94c76f89337d76da7cb27601d2b02a5553462bf9 (diff) | |
| download | perlweeklychallenge-club-ec2ba23f4b8a618f077f7310c77832631e57dd36.tar.gz perlweeklychallenge-club-ec2ba23f4b8a618f077f7310c77832631e57dd36.tar.bz2 perlweeklychallenge-club-ec2ba23f4b8a618f077f7310c77832631e57dd36.zip | |
feat(challenge-158/lubos-kolouch/[perl,python]/ch-1.p[ly]): Challenge 158 Task 1 LK Perl Python
| -rw-r--r-- | challenge-158/lubos-kolouch/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-158/lubos-kolouch/python/ch-1.py | 21 |
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-158/lubos-kolouch/perl/ch-1.pl b/challenge-158/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..bb4d6a431b --- /dev/null +++ b/challenge-158/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,29 @@ +package main; +use strict; +use warnings; +use Math::Prime::Util qw/is_prime next_prime/; +use List::Util qw/sum/; + +sub get_additive_primes { + my $limit = shift; + + my $pos = 1; + my @primes; + while ( $pos <= $limit ) { + + push @primes, $pos + if is_prime($pos) + and is_prime( sum( split //, $pos ) ); + $pos = next_prime($pos); + } + + return \@primes; +} + +use Test::More; + +is_deeply( get_additive_primes(100), + [ 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 ] ); + +done_testing; +1; diff --git a/challenge-158/lubos-kolouch/python/ch-1.py b/challenge-158/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..5e32ad5748 --- /dev/null +++ b/challenge-158/lubos-kolouch/python/ch-1.py @@ -0,0 +1,21 @@ +# Challenge 158 Task 1 +from sympy import isprime, nextprime + + +def get_additive_primes(limit: int) -> list: + """Get the list of primes""" + + pos = 1 + primes = [] + + while pos <= limit: + + if isprime(pos) and isprime(sum(list(map(int, str(pos))))): + primes.append(pos) + + pos = nextprime(pos) + + return primes + + +assert get_additive_primes(100) == [2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89] |
