-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
add : Project Euler 95 - solution #9169
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
Closed
kosuri-indu
wants to merge
12
commits into
TheAlgorithms:master
from
kosuri-indu:add/project-euler-95
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
36f4fb1
Project Euler 95 solution
kosuri-indu 4f58d3a
change in 95
kosuri-indu cec24b9
change in 95
kosuri-indu a0bc206
change in 95
kosuri-indu 11af4ce
change in 95
kosuri-indu ea35a71
change in 95
kosuri-indu 6b19990
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3427fba
change in 95
kosuri-indu f4bfb13
Merge branch 'add/project-euler-95' of https://github.com/kosuri-indu…
kosuri-indu 1642e3d
change in 95
kosuri-indu 3d21ced
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e26cc49
Merge branch 'add/project-euler-95' of https://github.com/kosuri-indu…
kosuri-indu 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Problem 95 | ||
Url: https://projecteuler.net/problem=95 | ||
Statement: | ||
The proper divisors of a number are all the divisors excluding the number itself. | ||
For example, the proper divisors of 28 is 1,2,4,7 and 14 and . | ||
As the sum of these divisors is equal to 28, we call it a perfect number. | ||
|
||
Interestingly the sum of the proper divisors of 220 is 284 and the sum of the proper divisors of 284 is 220 forming a chain of two numbers. | ||
For this reason, 220 and 284 are called an amicable pair. | ||
|
||
Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers: | ||
12496 -> 14288 -> 15472 -> 14536 -> 14264(->12496 -> ....) | ||
Since this chain returns to its starting point, it is called an amicable chain. | ||
|
||
Find the smallest member of the longest amicable chain with no element exceeding one million. | ||
""" | ||
|
||
|
||
def find_smallest_member(n: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: |
||
""" | ||
Returns the smallest member of the longest amicable chain with no element exceeding one million | ||
>> 14316 | ||
""" | ||
|
||
sum_of_div = [0] * (n + 1) | ||
for i in range(1, n // 2 + 1): | ||
for j in range(i * 2, n + 1, i): | ||
sum_of_div[j] += i | ||
|
||
checked = [False] * (n + 1) | ||
max_len_of_chain = 0 | ||
result = 0 | ||
for i in range(2, n + 1): | ||
possible_chain = [] | ||
j = i | ||
while not checked[j]: | ||
checked[j] = True | ||
possible_chain.append(j) | ||
j = sum_of_div[j] | ||
if j > n: | ||
break | ||
if j in possible_chain: | ||
len_of_chain = len(possible_chain) - possible_chain.index(j) | ||
if len_of_chain > max_len_of_chain: | ||
max_len_of_chain = len_of_chain | ||
result = min(possible_chain[-len_of_chain:]) | ||
break | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"Solution : {find_smallest_member(10**6)}") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
project_euler/sol1.py
, please provide doctest for the functionfind_smallest_member
Please provide descriptive name for the parameter:
n