Skip to content

Add doctest to maths/sieve_of_eratosthenes and remove other/finding_primes #1078

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 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions maths/sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
"""Sieve of Eratosthones."""
# -*- coding: utf-8 -*-

"""
Sieve of Eratosthones

The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value.
Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich)
Also thanks Dmitry (https://github.com/LizardWizzard) for finding the problem
"""


import math


def sieve(n):
"""Sieve of Eratosthones."""
"""
Returns a list with all prime numbers up to n.

>>> sieve(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
>>> sieve(25)
[2, 3, 5, 7, 11, 13, 17, 19, 23]
>>> sieve(10)
[2, 3, 5, 7]
>>> sieve(9)
[2, 3, 5, 7]
>>> sieve(2)
[2]
>>> sieve(1)
[]
"""

l = [True] * (n + 1)
prime = []
start = 2
end = int(math.sqrt(n))

while start <= end:
# If start is a prime
if l[start] is True:
prime.append(start)

# Set multiples of start be False
for i in range(start * start, n + 1, start):
if l[i] is True:
l[i] = False

start += 1

for j in range(end + 1, n + 1):
Expand Down
21 changes: 0 additions & 21 deletions other/finding_primes.py

This file was deleted.