blob: e3ad8abce60e98887d17d36e9678cab1296b25ef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
|