-
-
Notifications
You must be signed in to change notification settings - Fork 46.2k
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
Added solution to Project Euler 69 #2934
Conversation
Co-authored-by: Dhruv <dhruvmanila@gmail.com>
@dhruvmanila Implemented the requested changes, please review. |
project_euler/problem_69/sol1.py
Outdated
""" | ||
|
||
if n <= 0: | ||
raise ValueError("Please enter a natural number") |
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.
raise ValueError("Please enter a natural number") | |
raise ValueError("Please enter a natural number greater than 0") |
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.
Natural numbers are greater than 0 by definition. If you still want it, I'll commit :)
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.
Yes, please, or maybe write something like the input value must be greater than 0 and please put the if
statement back as that was a mistake on my part. Did you not realize that your code would become incorrect without the if
statement?
for number in range(2, n + 1): | ||
if phi[number] == number: | ||
phi[number] -= 1 | ||
for multiple in range(number * 2, n + 1, number): | ||
phi[multiple] = (phi[multiple] // number) * (number - 1) |
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.
for number in range(2, n + 1): | |
if phi[number] == number: | |
phi[number] -= 1 | |
for multiple in range(number * 2, n + 1, number): | |
phi[multiple] = (phi[multiple] // number) * (number - 1) | |
for number in range(2, n + 1): | |
phi[number] -= 1 | |
for multiple in range(number * 2, n + 1, number): | |
phi[multiple] = (phi[multiple] // number) * (number - 1) |
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.
That line always evaluates to True
so it's not needed.
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.
After I accepted this, the test failed. Are you sure of this?
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.
This was a mistake on my part. I did not see that you were changing the later elements of the list in the nested for
loop. Please change it back.
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.
I'm curious as to how did you not realize that your code would become incorrect without the if
statement?
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.
I did know what happened but gave you the benefit of doubt. Here's an explanation of what went wrong:
With the edit you made, the loop did not identify primes anymore and executed this for all numbers instead, therefore affecting the ratio.
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.
@dhruvmanila I have another open PR#2868 and would appreciate it if you could review that too. (Also, it uses the sieve creation similar to the phi constructed here)
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 mentioned, only submit one PR at a time. I have a lot to go through so that might take days. Thank you for your cooperation.
Co-authored-by: Dhruv <dhruvmanila@gmail.com>
Travis tests have failedHey @sarthaka1310, TravisBuddy Request Identifier: e9a2f7c0-0be5-11eb-a59b-f3f3774a9991 |
* Added solution to Project Euler 69 * Accept edits from code review Co-authored-by: Dhruv <dhruvmanila@gmail.com> * Added doctests * Renaming and exception handling * Apply suggestions from code review Co-authored-by: Dhruv <dhruvmanila@gmail.com> * Edited mistake. Co-authored-by: formal-acc <sarthak.agrawal@research.iiit.ac.in> Co-authored-by: Dhruv <dhruvmanila@gmail.com>
* Added solution to Project Euler 69 * Accept edits from code review Co-authored-by: Dhruv <dhruvmanila@gmail.com> * Added doctests * Renaming and exception handling * Apply suggestions from code review Co-authored-by: Dhruv <dhruvmanila@gmail.com> * Edited mistake. Co-authored-by: formal-acc <sarthak.agrawal@research.iiit.ac.in> Co-authored-by: Dhruv <dhruvmanila@gmail.com>
Describe your change:
For #hacktoberfest
Added solution to Project Euler 69 for issue #2695
Checklist:
Fixes: #{$ISSUE_NO}
.