Skip to content

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

Merged
merged 4 commits into from
Jul 31, 2019
Merged

project_euler/problem_10 #1089

merged 4 commits into from
Jul 31, 2019

Conversation

itsvinayak
Copy link
Member

edit new solution using sieve-of-eratosthenes to solve problem 10 of project Euler

Copy link
Member

@cclauss cclauss left a 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

try:
xrange # Python 2
except NameError:
xrange = range # Python 3
Copy link
Member

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.

#import doctest
#doctest.testmod()

print(prime_sum(int(raw_input())))
Copy link
Member

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.


"""

list = [0 for i in xrange(n+1)]
Copy link
Member

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

>>> prime_sum(10000)
5736396
>>> prime_sum(7)
10
Copy link
Member

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.

Copy link
Member Author

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,

@cclauss cclauss merged commit 4a5589f into TheAlgorithms:master Jul 31, 2019
@cclauss
Copy link
Member

cclauss commented Jul 31, 2019

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) 

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* project_euler/problem_10

* update project_euler/problem_10

* update project_euler/problem_10

* Negative user tests added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants