2

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?

2
  • I advise you to read about decorators Commented Feb 11, 2021 at 20:40
  • Tangential comment: you can get the sum much more easily with sum(function(i) for i in range(3000001)). Commented Feb 11, 2021 at 21:32

4 Answers 4

1

You can pass the function that you want to use as parameter.

def counter(prime_checker):
    primeamount = 0
    for i in range(3000001):
        if prime_checker(i) == True:
            primeamount +=1
    return primeamount

counter(primsqr)

Sign up to request clarification or add additional context in comments.

Comments

1

You just need to take a function as parameter.

def counter(function):
    primeamount = 0
    for i in range(3000001):
        if function(i) == True:
            primeamount +=1
    return primeamount

print(counter(primsqr))
print(counter(primeferm))

Comments

0

You can pass functions as arguments

def counter(func):
    primeamount = 0
    for i in range(3000001):
        if func(i) == True:
            primeamount +=1
    return primeamount


counter(primsqr) #or counter(primeferm)

Comments

0

Functions in python are objects just like primitive types (integers, strings, lists, etc.). Like users have said above you can pass functions as parameters in Python functions.

def counter(function):
    primeamount = 0
    for i in range(3000001):
        if function(i) == True:
            primeamount +=1
    return primeamount

print(counter(primsqr))
print(counter(primeferm))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.