Write an is_prime function in Python

In summary, the conversation discusses the creation of a function called is_prime, which takes in an integer argument and returns True if the argument is a prime number and False if it is not. The speaker suggests adding doctests as the function is developed and also provides some tips for debugging the code.
  • #1
brushman
113
1

Homework Statement



Write a function, is_prime, which takes a single integral argument and returns True when the argument is a prime number and False otherwise. Add doctests to your function as you develop it.

2. The attempt at a solution

Code:
def is_prime(n):
    x = 1
    while x<=n:
        x+=1
        if n%x==0:
            return False
        elif x==n:
            return True

if __name__ == '__main__':
    import doctest
    doctest.testmod()


I figure x goes from 2 to n, and if it finds a factor it returns false; otherwise, true. Why doesn't this work?
 
Technology news on Phys.org
  • #2
a) are you sure that you're calling the function? I tried to run your code but couldn't, so instead I just called the function to test it:
Code:
for i in range(10):
    print is_prime(i)

b) try to print out x, n, true, and false at every iteration of the loop

c) returns throw you out of a function, so think about your exit conditions for the while loop

I got your code working with a little tweak, so just step through the logic (which is what print statements-the most basic debugging tool-are for).
 
Last edited:

1. What is an "is_prime" function in Python?

An "is_prime" function in Python is a user-defined function that takes in a number as input and checks whether it is a prime number or not. A prime number is a number that is only divisible by itself and 1. The function returns a boolean value, True if the number is prime and False if it is not.

2. How do you write an "is_prime" function in Python?

To write an "is_prime" function in Python, you can use a for loop to iterate through all the numbers from 2 to the input number-1, and check if the input number is divisible by any of those numbers. If it is divisible, then the number is not prime. If the loop finishes without finding any divisors, then the number is prime. You can also optimize the function by only checking the numbers up to the square root of the input number, as any factors beyond the square root would have been already checked.

3. Can you provide an example of an "is_prime" function in Python?

Yes, here is an example of an "is_prime" function in Python:

def is_prime(num):
     for i in range(2, int(num**0.5)+1):
         if num % i == 0:
             return False
     return True

This function checks all the numbers from 2 to the square root of the input number and returns False if any number is found to be a divisor. If the loop finishes without finding any divisors, then the function returns True, indicating that the number is prime.

4. What is the time complexity of an "is_prime" function in Python?

The time complexity of an "is_prime" function in Python is O(sqrt(n)), where n is the input number. This is because the function only iterates up to the square root of the input number, making it more efficient than checking all numbers up to the input number-1.

5. How can an "is_prime" function be used in a larger program?

An "is_prime" function can be used in a larger program to check if a given number is prime before performing any operations on it. This can be useful in many scenarios, such as finding prime numbers within a range, checking if a number is a factor of another number, or implementing prime number algorithms like the Sieve of Eratosthenes. It can also be used in applications involving cryptography or generating random numbers.

Similar threads

  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
11
Views
807
  • Programming and Computer Science
Replies
4
Views
862
  • Programming and Computer Science
Replies
8
Views
786
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
33
Views
2K
Back
Top