project_euler/problem_10 - #1089
Conversation
cclauss
left a comment
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Sorry to be a pain but... Let's simplify and just use input() and range() because this repo no longer supports legacy Python.
| #import doctest | ||
| #doctest.testmod() | ||
|
|
||
| print(prime_sum(int(raw_input()))) |
There was a problem hiding this comment.
print(prime_sum(int(input().strip()))) will ensure that your code will gracefully deal with leading and/or trailing whitespace in user input.
|
|
||
| """ | ||
|
|
||
| list = [0 for i in xrange(n+1)] |
There was a problem hiding this comment.
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
| >>> prime_sum(10000) | ||
| 5736396 | ||
| >>> prime_sum(7) | ||
| 10 |
There was a problem hiding this comment.
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.
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,
* 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