-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.py
More file actions
executable file
·37 lines (32 loc) · 1.09 KB
/
Copy pathprimes.py
File metadata and controls
executable file
·37 lines (32 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python3
import math
number = 1
maxnumber = 100
number_of_primes = 0
# Need to start on an odd number
if number % 2 == 0:
number +=1
index = number
while index <= maxnumber:
# Start with 3, as we make sure we start on odd numbers
# We are incrimenting by 2 to ensure odd numbers
count = 3
is_prime = 0
while count <= math.sqrt(maxnumber):
if index % count == 0:
if count != 1 and count != index:
is_prime = 1
# print(str(index) + " is divisible by: " + str(count))
# If number is divisible by count, skip it and test the next one
break
# Check the next number count
count += 1
if is_prime == 0:
# print(number, end=', ')
number_of_primes += 1
print(str(number_of_primes) + ") Number " + str(index) + " is prime")
# If last digit is 0,2,4,6,8, then it's even and divisable by 2, so lets skip it
index += 2
print("")
percentage = 100 * number_of_primes / maxnumber
print("Toal number of Primes from " + str(number) +" to " + str(maxnumber) + " is " + str(number_of_primes) + ", which is " + str(percentage) + " %")