Find Prime numbers using Python
Posted by Samath
Last Updated: March 02, 2015

An integer greater than 1 is said to be prime if it divisible by only 1 and itself. For example 2,3,5,7 are prime numbers, but 4, 6, 8 and 9 are not. Write a function isPrime that determines whether a number is prime or not. The function takes a parameter n and checks if there exists a number from 2 to n that n is divisible by [Hint: you can use for loops]. If n is divisible by such a number then it is not a prime number and false must be returned. Return true if the number is a prime number. Remember that 1 is not a prime number.
e.g.
isPrime(5) -> True

Code:

def isPrime(n):
        for x in range(2,n):
            if(n%x==0):
                return False
        return True

Use isPrime function in a function primes that take two numbers as parameters and prints all the prime numbers between those two numbers (e.g. 2 and 10). Ensure that isPrime is local function and can only be accessed by function primes.
e.g.
primes(2,10) -> 2,3,5,7
isPrime(3) -> syntax error

Code:

def primes(a,n):
    def isPrime(n):
        for x in range(2,n):
            if(n%x==0):
                return False
        return True
    for num in range(a,n):
        if(isPrime(num) == True):
            print(num)