From 46bc6738d78de3a05901881c919e8d91a28b8ef4 Mon Sep 17 00:00:00 2001 From: obelisk0114 Date: Fri, 26 Jul 2019 03:25:38 -0700 Subject: [PATCH] Add doctest to maths/sieve_of_eratosthenes.py and remove other/finding_primes.py (#1078) Both of the two files implemented sieve of eratosthenes. However, there was a bug in other/finding_primes.py, and the time complexity was larger than the other. Therefore, remove other/finding_primes.py and add doctest tomaths/sieve_of_eratosthenes.py. --- maths/sieve_of_eratosthenes.py | 38 ++++++++++++++++++++++++++++++++-- other/finding_primes.py | 21 ------------------- 2 files changed, 36 insertions(+), 23 deletions(-) delete mode 100644 other/finding_primes.py diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index cedd04f92..44c7f8a02 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -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): diff --git a/other/finding_primes.py b/other/finding_primes.py deleted file mode 100644 index 035a14f4a..000000000 --- a/other/finding_primes.py +++ /dev/null @@ -1,21 +0,0 @@ -''' --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 -''' -from __future__ import print_function - - -from math import sqrt -def SOE(n): - check = round(sqrt(n)) #Need not check for multiples past the square root of n - - sieve = [False if i <2 else True for i in range(n+1)] #Set every index to False except for index 0 and 1 - - for i in range(2, check): - if(sieve[i] == True): #If i is a prime - for j in range(i+i, n+1, i): #Step through the list in increments of i(the multiples of the prime) - sieve[j] = False #Sets every multiple of i to False - - for i in range(n+1): - if(sieve[i] == True): - print(i, end=" ")