diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-09-04 21:39:50 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-09-04 21:39:50 +0200 |
| commit | 71b570df0971b3194a6b5e76a110ad866ab0267f (patch) | |
| tree | 1ed17f05fe8745ea5a2c0dc485ae000a91d04c14 | |
| parent | 96359f89a3f6ff89b93965600fc879769188a6cd (diff) | |
| download | perlweeklychallenge-club-71b570df0971b3194a6b5e76a110ad866ab0267f.tar.gz perlweeklychallenge-club-71b570df0971b3194a6b5e76a110ad866ab0267f.tar.bz2 perlweeklychallenge-club-71b570df0971b3194a6b5e76a110ad866ab0267f.zip | |
Solution 076 LK Task 1 Python
| -rw-r--r-- | challenge-076/lubos-kolouch/python/ch-1.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-076/lubos-kolouch/python/ch-1.py b/challenge-076/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..e3ad8abce6 --- /dev/null +++ b/challenge-076/lubos-kolouch/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +""" Perl weekly challenge 076 """ +# =============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/ +# +# Task 1 - Prime Sum +# +# AUTHOR: Lubos Kolouch +# =============================================================================== +from sympy import isprime + + +def get_nr_primes(what): + """ Calculate and return the number of primes needed """ + + # if the number is prime, we need just 1 number to represent it + + if isprime(what): + return 1 + + # if the number is even, we need 2 primes thanks to Goldbach's conjecture + if what % 2 == 1: + return 2 + + # if the number - 2 is prime, return 2 + if isprime(what - 2): + return 2 + + # if the number -3 is prime, return 2 (3 and the prime) + if isprime(what - 3): + return 2 + + # otherwise return 3 - it is 3 and 2 primes forming $what - 3 thanks to + # Goldbach's conjecture + return 3 + + +assert get_nr_primes(2) == 1 +assert get_nr_primes(9) == 2 +assert get_nr_primes(10) == 2 +assert get_nr_primes(12) == 3 |
