from itertools import chain
def prime(x):
'''Является ли x простым.'''
if x <= 1: return False
if x%2 == 0 or x%3 == 0: return x <= 3
return all(x%div and x%(div+2) for div in range(5, int(x**0.5) + 1, 6))
def primes(n):
'''Простые числа, меньшие n.'''
prime = [True] * n
for i in range(3, int(n**0.5) + 1, 2):
if prime[i]:
prime[i*i::2*i] = [False] * ((n-i*i-1) // (2*i)+1)
return chain([2] if n > 2 else [], (i for i in range(3, n, 2) if prime[i]))
def solve(x):
'''Представления x суммами двух простых чисел.'''
return ({'a': x - match, 'b': match} for match in primes(x//2 + 1) if prime(x - match))
def lang_count(n, eds):
'''Склонение счётного слова. Eds — строка вида один/два/много, например, "коробка/коробки/коробок".'''
return eds.split('/')[2 if n%10 == 0 or n%10 > 4 or n//10%10 == 1 else 0 if n%10 == 1 else 1]
def main():
x = None
try:
x = int(input("Введите чётное число, большее двух: "))
except ValueError:
print("Это не число.")
return
if x % 2 != 0 or x <= 2:
print("Число не подходит.")
return
n = 0
def note(item):
nonlocal n
n = n + 1
return item
pretty = "{x} = {solutions}".format(x = x, solutions = " = ".join("{a}+{b}".format(**r) for r in map(note, solve(x))))
if n > 1: pretty += " ({n} решени{ed})".format(n = n, ed = lang_count(n, "е/я/й"))
if not n: pretty = "Решений не найдено!"
print(pretty)
main()
ZnJvbSBpdGVydG9vbHMgaW1wb3J0IGNoYWluCgpkZWYgcHJpbWUoeCk6CgknJyfQr9Cy0LvRj9C10YLRgdGPINC70LggeCDQv9GA0L7RgdGC0YvQvC4nJycKCWlmIHggPD0gMTogcmV0dXJuIEZhbHNlCglpZiB4JTIgPT0gMCBvciB4JTMgPT0gMDogcmV0dXJuIHggPD0gMwoJcmV0dXJuIGFsbCh4JWRpdiBhbmQgeCUoZGl2KzIpIGZvciBkaXYgaW4gcmFuZ2UoNSwgaW50KHgqKjAuNSkgKyAxLCA2KSkKCmRlZiBwcmltZXMobik6CgknJyfQn9GA0L7RgdGC0YvQtSDRh9C40YHQu9CwLCDQvNC10L3RjNGI0L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