0% found this document useful (0 votes)
159 views3 pages

Prime Number Generator - Py

This document contains code to generate prime numbers between a given range using different algorithms with varying time complexities. It defines functions to check for divisibility by numbers 2 through 8, as well as functions to generate prime numbers with basic iteration from 1 to n, iteration from 2 to sqrt(n), pre-checking for divisibility, and tracking execution time. The main loop prompts the user to select an algorithm to run, times its execution, and displays the results and processing time.

Uploaded by

Siva Virat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views3 pages

Prime Number Generator - Py

This document contains code to generate prime numbers between a given range using different algorithms with varying time complexities. It defines functions to check for divisibility by numbers 2 through 8, as well as functions to generate prime numbers with basic iteration from 1 to n, iteration from 2 to sqrt(n), pre-checking for divisibility, and tracking execution time. The main loop prompts the user to select an algorithm to run, times its execution, and displays the results and processing time.

Uploaded by

Siva Virat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import time

def divisible_by_2(string):
#checking divisibility of 2
return string[-1]=='0' or string[-1]=='2' or string[-1]=='4' or string[-1]=='6'
or string[-1]=='8'

def divisible_by_3(string):
#checking divisibility of 3
total=0
for char in string:
total+=int(char)
return total%3==0 #sum of digits of a number

def divisible_by_4(string):
#checking divisibility of 4
return int(string[-2:])%4==0 #last 2 digits of a number

def divisible_by_5(string):
#checking divisibility of 5
return string[-1]=='5' #unit digit of a number

def divisible_by_8(string):
#checking divisibility of 8
return int(string[-3:])%8==0 #last 3 digits of a number

def divisibility_check(string):
#checking divisibility of 2,3,4,5 and 8
return divisible_by_2(string) or divisible_by_3(string) or
divisible_by_4(string) or divisible_by_5(string) or divisible_by_8(string)

def prime_number_geneartor_4(start_range,end_range):
for i in range(start_range,end_range+1):
stringified_num = str(i)
if i<=1: #eliminating negative numbers,0 and 1 as
they are not primes
continue
elif len(stringified_num)>1 and divisibility_check(stringified_num):
#performing divisibility_checks for numbers having length>=2
continue
is_prime=True
for j in range(2,i//2+1): #Prime number check by reducing no.of
iterations to half
if i%j==0:
is_prime=False
break
if is_prime:
global result
result.append(i)
return result

def prime_number_geneartor_3(start_range,end_range):
for i in range(start_range,end_range+1):
if i<=1: #eliminating negative numbers,0 and 1 as
they are not primes
continue
is_prime=True
for j in range(2,i//2+1): #Prime number check by reducing no.of
iterations to half
if i%j==0:
is_prime=False
break
if is_prime:
global result
result.append(i)
return result

def prime_number_geneartor_2(start_range,end_range):
for i in range(start_range,end_range+1):
if i<=1: #eliminating negative numbers,0 and 1 as
they are not primes
continue
is_prime=True
for j in range(2,i): #Prime number check excluding 1 and itself
if i%j==0:
is_prime=False
break
if is_prime:
global result
result.append(i)
return result

def prime_number_geneartor_1(start_range,end_range):
for i in range(start_range,end_range+1):
if i<=1: #eliminating negative numbers,0 and 1 as
they are not primes
continue
count=0
for j in range(1,i+1): #Prime number check including 1 and itself
if i%j==0:
count+=1
if count==2:
global result
result.append(i)
return result

#This loop runs unit user selects '0' to exit


choice=None
while choice!=0:
msg = "\nThe algorithms below are listed on the basis of their optimization
level(time complexity) in ascending order\n1. Prime Number Generator-1\n2. Prime
Number Generator-2\n3. Prime Number Generator-3\n4.Prime Number Generator-4 (more
optimized)\n0.Press 0 to exit\n"
print(msg)
choice = int(input("Select your preferred Algorithm as listed above: "))
if choice==0:
print("Exit Successful......")
break
start_range, end_range = int(input("Enter the start range: ")),int(input("Enter
the end range: "))
result = []

#selecting strategies
if choice==1:
start_time = time.time() #time just before performing prime
Check
result = prime_number_geneartor_1(start_range,end_range) #calling
Generator-1
end_time = time.time()-start_time #time taken to perform prime check
elif choice==2:
start_time = time.time() #time just before performing prime
Check
result = prime_number_geneartor_2(start_range,end_range) #calling
Generator-1
end_time = time.time()-start_time #time taken to perform prime check
elif choice==3:
start_time = time.time() #time just before performing prime
Check
result = prime_number_geneartor_3(start_range,end_range) #calling
Generator-1
end_time = time.time()-start_time #time taken to perform prime check
elif choice==4:
start_time = time.time() #time just before performing prime
Check
result = prime_number_geneartor_4(start_range,end_range) #calling
Generator-1
end_time = time.time()-start_time #time taken to perform prime check
else:
print("Enter valid Selection")

print(result)
print("count,",len(result))
print("processing time:",end_time)

You might also like