-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add first solution for Project Euler Problem 207 #3522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dhruvmanila
merged 16 commits into
TheAlgorithms:master
from
PetitNigaud:project-euler-207
Oct 29, 2020
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
53568cf
add solution to Project Euler problem 206
64e3d36
Add solution to Project Euler problem 205
3c9a9dc
Merge branch 'master' of https://github.com/TheAlgorithms/Python
42d3142
updating DIRECTORY.md
25ca898
Merge branch 'master' of https://github.com/TheAlgorithms/Python
05853d8
updating DIRECTORY.md
f566df1
Merge branch 'master' of https://github.com/TheAlgorithms/Python
c60eacd
Revert "Add solution to Project Euler problem 205"
5f10606
Revert "add solution to Project Euler problem 206"
7ae0d6f
add solution for project euler problem 207
e916ab0
updating DIRECTORY.md
4fb4a1c
add type hint for output of helper function
5231e04
Merge branch 'project-euler-207' of https://github.com/PetitNigaud/Py…
4a842ee
Correct default parameter value in solution
PetitNigaud fce5a5c
use descriptive variable names and remove problem solution from docte…
c018f58
Merge branch 'project-euler-207' of https://github.com/PetitNigaud/Py…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
|
||
Project Euler Problem 207: https://projecteuler.net/problem=207 | ||
|
||
Problem Statement: | ||
For some positive integers k, there exists an integer partition of the form | ||
4**t = 2**t + k, where 4**t, 2**t, and k are all positive integers and t is a real | ||
number. The first two such partitions are 4**1 = 2**1 + 2 and | ||
4**1.5849625... = 2**1.5849625... + 6. | ||
Partitions where t is also an integer are called perfect. | ||
For any m ≥ 1 let P(m) be the proportion of such partitions that are perfect with | ||
k ≤ m. | ||
Thus P(6) = 1/2. | ||
In the following table are listed some values of P(m) | ||
|
||
P(5) = 1/1 | ||
P(10) = 1/2 | ||
P(15) = 2/3 | ||
P(20) = 1/2 | ||
P(25) = 1/2 | ||
P(30) = 2/5 | ||
... | ||
P(180) = 1/4 | ||
P(185) = 3/13 | ||
|
||
Find the smallest m for which P(m) < 1/12345 | ||
|
||
Solution: | ||
Equation 4**t = 2**t + k solved for t gives: | ||
t = log2(sqrt(4*k+1)/2 + 1/2) | ||
For t to be real valued, sqrt(4*k+1) must be an integer which is implemented in | ||
function check_t_real(k). For a perfect partition t must be an integer. | ||
To speed up significantly the search for partitions, instead of incrementing k by one | ||
per iteration, the next valid k is found by k = (i**2 - 1) / 4 with an integer i and | ||
k has to be a positive integer. If this is the case a partition is found. The partition | ||
is perfect if t os an integer. The integer i is increased with increment 1 until the | ||
proportion perfect partitions / total partitions drops under the given value. | ||
|
||
""" | ||
|
||
import math | ||
|
||
|
||
def check_partition_perfect(k) -> bool: | ||
""" | ||
|
||
Check if t = f(k) = log2(sqrt(4*k+1)/2 + 1/2) is a real number. | ||
|
||
>>> check_partition_perfect(2) | ||
True | ||
|
||
>>> check_partition_perfect(6) | ||
False | ||
|
||
""" | ||
|
||
t = math.log2(math.sqrt(4 * k + 1) / 2 + 1 / 2) | ||
|
||
return t == int(t) | ||
|
||
|
||
def solution(max_proportion: float = 1 / 12345) -> int: | ||
""" | ||
Find m for which the proportion of perfect partitions to total partitions is lower | ||
than max_proportion | ||
|
||
>>> solution(1) > 5 | ||
True | ||
|
||
>>> solution(3 / 13) > 185 | ||
True | ||
|
||
>>> solution(1 / 12345) | ||
44043947822 | ||
PetitNigaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
|
||
total = 0 | ||
perfect = 0 | ||
|
||
i = 3 | ||
while True: | ||
k = (i ** 2 - 1) / 4 | ||
if k == int(k): # if k = f(i) is an integer, then there is a partition for k | ||
k = int(k) | ||
total += 1 | ||
if check_partition_perfect(k): | ||
perfect += 1 | ||
if perfect > 0: | ||
if perfect / total < max_proportion: | ||
return k | ||
i += 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.