|
| 1 | +""" |
| 2 | +
|
| 3 | +Project Euler Problem 207: https://projecteuler.net/problem=207 |
| 4 | +
|
| 5 | +Problem Statement: |
| 6 | +For some positive integers k, there exists an integer partition of the form |
| 7 | +4**t = 2**t + k, where 4**t, 2**t, and k are all positive integers and t is a real |
| 8 | +number. The first two such partitions are 4**1 = 2**1 + 2 and |
| 9 | +4**1.5849625... = 2**1.5849625... + 6. |
| 10 | +Partitions where t is also an integer are called perfect. |
| 11 | +For any m ≥ 1 let P(m) be the proportion of such partitions that are perfect with |
| 12 | +k ≤ m. |
| 13 | +Thus P(6) = 1/2. |
| 14 | +In the following table are listed some values of P(m) |
| 15 | +
|
| 16 | + P(5) = 1/1 |
| 17 | + P(10) = 1/2 |
| 18 | + P(15) = 2/3 |
| 19 | + P(20) = 1/2 |
| 20 | + P(25) = 1/2 |
| 21 | + P(30) = 2/5 |
| 22 | + ... |
| 23 | + P(180) = 1/4 |
| 24 | + P(185) = 3/13 |
| 25 | +
|
| 26 | +Find the smallest m for which P(m) < 1/12345 |
| 27 | +
|
| 28 | +Solution: |
| 29 | +Equation 4**t = 2**t + k solved for t gives: |
| 30 | + t = log2(sqrt(4*k+1)/2 + 1/2) |
| 31 | +For t to be real valued, sqrt(4*k+1) must be an integer which is implemented in |
| 32 | +function check_t_real(k). For a perfect partition t must be an integer. |
| 33 | +To speed up significantly the search for partitions, instead of incrementing k by one |
| 34 | +per iteration, the next valid k is found by k = (i**2 - 1) / 4 with an integer i and |
| 35 | +k has to be a positive integer. If this is the case a partition is found. The partition |
| 36 | +is perfect if t os an integer. The integer i is increased with increment 1 until the |
| 37 | +proportion perfect partitions / total partitions drops under the given value. |
| 38 | +
|
| 39 | +""" |
| 40 | + |
| 41 | +import math |
| 42 | + |
| 43 | + |
| 44 | +def check_partition_perfect(positive_integer: int) -> bool: |
| 45 | + """ |
| 46 | +
|
| 47 | + Check if t = f(positive_integer) = log2(sqrt(4*positive_integer+1)/2 + 1/2) is a |
| 48 | + real number. |
| 49 | +
|
| 50 | + >>> check_partition_perfect(2) |
| 51 | + True |
| 52 | +
|
| 53 | + >>> check_partition_perfect(6) |
| 54 | + False |
| 55 | +
|
| 56 | + """ |
| 57 | + |
| 58 | + exponent = math.log2(math.sqrt(4 * positive_integer + 1) / 2 + 1 / 2) |
| 59 | + |
| 60 | + return exponent == int(exponent) |
| 61 | + |
| 62 | + |
| 63 | +def solution(max_proportion: float = 1 / 12345) -> int: |
| 64 | + """ |
| 65 | + Find m for which the proportion of perfect partitions to total partitions is lower |
| 66 | + than max_proportion |
| 67 | +
|
| 68 | + >>> solution(1) > 5 |
| 69 | + True |
| 70 | +
|
| 71 | + >>> solution(1/2) > 10 |
| 72 | + True |
| 73 | +
|
| 74 | + >>> solution(3 / 13) > 185 |
| 75 | + True |
| 76 | +
|
| 77 | + """ |
| 78 | + |
| 79 | + total_partitions = 0 |
| 80 | + perfect_partitions = 0 |
| 81 | + |
| 82 | + integer = 3 |
| 83 | + while True: |
| 84 | + partition_candidate = (integer ** 2 - 1) / 4 |
| 85 | + # if candidate is an integer, then there is a partition for k |
| 86 | + if partition_candidate == int(partition_candidate): |
| 87 | + partition_candidate = int(partition_candidate) |
| 88 | + total_partitions += 1 |
| 89 | + if check_partition_perfect(partition_candidate): |
| 90 | + perfect_partitions += 1 |
| 91 | + if perfect_partitions > 0: |
| 92 | + if perfect_partitions / total_partitions < max_proportion: |
| 93 | + return partition_candidate |
| 94 | + integer += 1 |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + print(f"{solution() = }") |
0 commit comments