#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jun 14 13:42:48 2023 @author: richardson """ from math import * from time import time # code to input a number: n = int(input("Give me an integer here: ")) #nn = 186711 # this assigns nn to be an integer. print("Do you think "+str(n)+" is prime?") # these are strings; print displays it as output. def isitprime1(nn): lastj = int(sqrt(nn)); # only need to # check divisibility of numbers up to the # square root of nn for j in range(2,lastj+1): # range(2,nn) means the integers 2,3,4,...,lastj-1 # each time through the loop, j becomes 2, # then 3, then 4, ... # and the loop is run lastj-2 times. if nn%j==0: # need two = signs if you are #checking if an equation is true are not. # a==b means True if a=b, False # if a!=b. '!=' means not equal. # nn%j means nn mod j = remainder # when nn is divided by j. return False return True t1 = time() # number of seconds since a specific date. bubba = isitprime1(n) t2 = time() print("Time elapsed for 1st method = "+str(t2-t1)+" seconds.") if bubba: print("Yes, "+str(n)+" is prime.") else: print("No, "+str(n)+" is composite.") def isitprime2(nn): for j in range(2,nn): # range(2,nn) means the integers 2,3,4,...,lastj-1 # each time through the loop, j becomes 2, # then 3, then 4, ... # and the loop is run lastj-2 times. if nn%j==0: # need two = signs if you are #checking if an equation is true are not. # a==b means True if a=b, False # if a!=b. '!=' means not equal. # nn%j means nn mod j = remainder # when nn is divided by j. return False return True t1 = time() # number of seconds since a specific date. bubba = isitprime2(n) t2 = time() print("Time elapsed for second method = "+str(t2-t1)+" seconds.") if bubba: print("Yes, "+str(n)+" is prime.") else: print("No, "+str(n)+" is composite.") def isitprime3(nn): lastj = int(sqrt(nn)); # only need to # check divisibility of numbers up to the # square root of nn for j in range(2,lastj+1): # range(2,nn) means the integers 2,3,4,...,lastj-1 # each time through the loop, j becomes 2, # then 3, then 4, ... # and the loop is run lastj-2 times. if isitprime3(j) and nn%j==0: # recursive programming # i.e. calling the function from within itself # need two = signs if you are #checking if an equation is true are not. # a==b means True if a=b, False # if a!=b. '!=' means not equal. # nn%j means nn mod j = remainder # when nn is divided by j. return False return True t1 = time() # number of seconds since a specific date. bubba = isitprime3(n) t2 = time() print("Time elapsed for 3rd method = "+str(t2-t1)+" seconds.") if bubba: print("Yes, "+str(n)+" is prime.") else: print("No, "+str(n)+" is composite.")