Skip to content

Commit ae702b0

Browse files
authored
Merge pull request #10 from bhansa/master
Added new method for sieve of Eratosthenes
2 parents 96c3aea + ee181c8 commit ae702b0

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

Diff for: Prime/code.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
1+
def SieveOfEratosthenes(range_to):
12

3+
# creating a boolean array first
4+
prime = [True for i in range(range_to + 1)]
5+
p = 2
6+
while (p * p <= range_to):
7+
8+
# If prime[p] is not changed, then it is a prime
9+
if (prime[p] == True):
10+
11+
# Update all multiples of p
12+
for i in range(p * 2, range_to + 1, p):
13+
prime[i] = False
14+
p += 1
15+
print([p for p in range(2, range_to) if prime[p]])
216

317
def prime(range_from, range_to):
418

5-
allList = [x for x in range(2, int(range_to)+1)]
19+
allList = [x for x in range(2, range_to + 1)]
620

721
ptr = 0
822
while True:
923
i = 2
10-
while i * allList[ptr] <= int(range_to):
24+
while i * allList[ptr] <= range_to:
1125
if i*allList[ptr] in allList:
1226
allList.remove(i*allList[ptr])
1327
i += 1
1428
ptr += 1
15-
if allList[ptr] ** 2 > int(range_to):
29+
if allList[ptr] ** 2 > range_to:
1630
break
1731

1832

19-
primeList = [x for x in allList if x > int(range_from)]
33+
primeList = [x for x in allList if x > range_from]
2034
print("Prime List Between ", range_from, " and ", range_to, " is")
2135
print(primeList)
2236

2337

2438

2539
if __name__ == "__main__":
2640
print("Enter the range of number")
27-
range_from = input("From: ")
28-
range_to = input("To: ")
41+
range_from = int(input("From: "))
42+
range_to = int(input("To: "))
2943

3044
prime(range_from, range_to)
45+
SieveOfEratosthenes(range_to)
46+

0 commit comments

Comments
 (0)