-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
project_euler/problem_10 #1089
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
project_euler/problem_10 #1089
Conversation
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.
Cool! Thanks for your contribution. A few minor requests...
- Longer variable names because l, i, j, s look kinda old school. Can you please expand at least some of these into words that help to explain your intent to the reader?
- Could you please return s instead of printing s at the end of prime_sum()? This would require your main to change to print(prime_sum(2000000)) but it allows others to use your function in larger scripts. It also allows us to do automated testing...
- Please also consider adding doctests with a large positive number, small positive number, zero, a negative number, a floating point number, and a string just to see how prime_sum() deals with bad data.
- Python type hints also help if you are so inclined... https://docs.python.org/3/library/typing.html
project_euler/problem_10/sol3.py
Outdated
try: | ||
xrange # Python 2 | ||
except NameError: | ||
xrange = range # Python 3 |
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.
Sorry to be a pain but... Let's simplify and just use input() and range() because this repo no longer supports legacy Python.
project_euler/problem_10/sol3.py
Outdated
#import doctest | ||
#doctest.testmod() | ||
|
||
print(prime_sum(int(raw_input()))) |
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.
print(prime_sum(int(input().strip()))) will ensure that your code will gracefully deal with leading and/or trailing whitespace in user input.
project_euler/problem_10/sol3.py
Outdated
|
||
""" | ||
|
||
list = [0 for i in xrange(n+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.
list should not be used as a variable name in Python because there is list is a builtin that we should avoid shadowing. https://docs.python.org/3/library/functions.html
project_euler/problem_10/sol3.py
Outdated
>>> prime_sum(10000) | ||
5736396 | ||
>>> prime_sum(7) | ||
10 |
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.
Tests for negative number, floating point number, string? This will ensure that your code is robust and predictable.
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.
it's not for a negative number, floating-point number, string. it only works for positive numbers,
it a question base problem if anyone uses it, should only enter positive numbers.
thank you,
Thanks for the contribution... An optimization was possible around: s = 0
for i in range(n):
if list_[i] == 0:
s += i
return s
# could be rewritten as
return sum(i for i in range(n) if list_[i] == 0) |
* project_euler/problem_10 * update project_euler/problem_10 * update project_euler/problem_10 * Negative user tests added.
edit new solution using sieve-of-eratosthenes to solve problem 10 of project Euler