Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0a155b7

Browse filesBrowse files
sidvickramanabranhe
authored andcommitted
Added Sieve Of Eratosthenes to algorithms -> math
This is my first time adding committing anything to Open Source so do let me know if I'm doing anything wrong.
1 parent 4c9a434 commit 0a155b7
Copy full SHA for 0a155b7

File tree

Expand file treeCollapse file tree

1 file changed

+31
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+31
-0
lines changed
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import math
2+
3+
4+
def sieve(number): # taking a number as input and we'll find all prime values below it
5+
primes = [0] * number
6+
primes[0] = primes[1] = 1 # initialise 0 and 1 to not prime numbers
7+
for prime in range(2, int(math.sqrt(len(primes)))):
8+
if primes[prime] == 0:
9+
for y in range(prime * 2, len(primes), prime):
10+
primes[y] = 1
11+
else:
12+
pass
13+
14+
return primes
15+
16+
17+
while True: # take input and reject any non integer inputs
18+
try:
19+
num = int(input("Type in a number: "))
20+
break
21+
22+
except ValueError:
23+
print("Type in an integer!")
24+
25+
nums = sieve(num)
26+
27+
for num in range(len(nums)):
28+
if nums[num] == 0:
29+
print(num)
30+
else:
31+
pass

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.