Skip to content

Speed improvements for prime finder - especially for large ranges #13

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
Oct 29, 2017
Merged
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
64 changes: 29 additions & 35 deletions Prime/code.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
def SieveOfEratosthenes(range_to):

# creating a boolean array first
prime = [True for i in range(range_to + 1)]
p = 2
while (p * p <= range_to):

# If prime[p] is not changed, then it is a prime
if (prime[p] == True):

# Update all multiples of p
for i in range(p * 2, range_to + 1, p):
prime[i] = False
p += 1
print([p for p in range(2, range_to) if prime[p]])
# creating a boolean array first
prime = [True for i in range(range_to + 1)]
p = 2
while (p * p <= range_to):

def prime(range_from, range_to):

allList = [x for x in range(2, range_to + 1)]
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):

# Update all multiples of p
for i in range(p * 2, range_to + 1, p):
prime[i] = False

p += 1

ptr = 0
while True:
i = 2
while i * allList[ptr] <= range_to:
if i*allList[ptr] in allList:
allList.remove(i*allList[ptr])
i += 1
ptr += 1
if allList[ptr] ** 2 > range_to:
break
print([p for p in range(2, range_to) if prime[p]])


primeList = [x for x in allList if x > range_from]
print("Prime List Between ", range_from, " and ", range_to, " is")
print(primeList)
def is_prime(num):
for i in range(2, int(num / 2) + 1):
if num % i == 0:
return False
return True


def prime(range_from, range_to):
allList = [num for num in range(range_from, range_to + 1) if is_prime(num)]

print("Prime List Between", range_from, "and", range_to, "is")
print(allList)


if __name__ == "__main__":
print("Enter the range of number")
range_from = int(input("From: "))
range_to = int(input("To: "))

prime(range_from, range_to)
SieveOfEratosthenes(range_to)
print("Enter the range of number")
range_from = int(input("From: "))
range_to = int(input("To: "))

prime(range_from, range_to)
SieveOfEratosthenes(range_to)