Alright so I have coded a program that checks prime numbers with two different methods. I also made a program that counts the number of prime numbers that the functions find under a given number. I also compare these later and find % difference. However, I am curious if there's a way for the prime counter code to take on the two different prime checkers as variables so I don't have to write two separate counters for each method. How could I make something like that?
The code is something like this
def primsqr(x):
if x>1:
for i in range(2, int(x ** 0.5) + 1):
if x % i == 0:
break
return False
else:
return True
def primeferm(x):
if x>1:
if pow(2, (x-1), mod=x) == 1:
return True
else:
return False
def counter():
primeamount = 0
for i in range(3000001):
if primsqr(i) == True:
primeamount +=1
return primeamount
#And then an identical one for the other prime checker and the comparison blablabla
#This is also pseudocode
Any chance I could just make a general function for counting prime amounts under a number instead of writing the last one two times? Maybe something that makes the prime checker as a parameter and makes a general function?
sum(function(i) for i in range(3000001)).