Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import reduce
- def part(n):
- products = sorted(product(p) for p in partitions(n))
- values = products[-1] - 1, sum(products) / len(products), sorted_median(products)
- return "Range: {} Average: {:.2f} Median {:.2f}".format(*values)
- def product(arr):
- return reduce(int.__mul__, arr)
- def is_prime(n):
- d = 2
- while d * d <= n:
- if n % d == 0: return False
- d += 1
- return True
- def sorted_median(arr):
- return (arr[(len(arr) - 1) // 2] + arr[len(arr) // 2]) / 2
- def prime_at_least(n):
- while not is_prime(n): n += 1
- return n
- def partitions(n):
- current = [1] * n
- while current:
- yield current
- current = next_partition(current)
- def next_partition(partition):
- if len(partition) < 2: return None
- suffix_total = sum(partition[-2:])
- new_repeated_value = prime_at_least(partition[-2] + 1)
- repetitions = suffix_total // new_repeated_value - 1
- result = partition[:-2] + [new_repeated_value] * repetitions
- result.append(suffix_total - repetitions * new_repeated_value)
- return result if is_prime(result[-1]) else next_partition(result)
- n = int(input())
- print(part(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement