Duncan Alf Zhang
Duncan Alf Zhang

Reputation: 37

How to Find Prime Number

How to find prime numbers? Prime numbers are the numbers that are bigger than one, and are divisible only by themselves and one. One way to determine whether or not a number is a prime number is as follows:

->if the number < 2, then return False

->if the number is 2, then return True ->for each value of i, where i >=2 and i < number: if the number is divisible by i, then return False ->return True

My current code:

def is_prime(number):
    if number == 2:
        return True
    elif number < 2:
        return False
    else:
        for i in range(2, number):
            if number % i == 0:
                return False
            else:
                return True
def main():
    print(is_prmie(1))
    print(is_prmie(4))
    print(is_prmie(7))

Some syntax problems I don't know how to solve. Can someone please help? Thanks TA!

Upvotes: 2

Views: 458

Answers (4)

unpluggeDloop
unpluggeDloop

Reputation: 109

        <?php

        // PHP code to check wether a number is prime or Not 
        // function to check the number is Prime or Not 
        function primeCheck($number){ 

            if ($number == 1 || $number == 2 || $number % 2 !== 0) {
                 echo "Prime"; 
            }

            else if ($number % 2 == 0) 
            {
                echo "Not Prime"; 
            }
          } 

        echo primeCheck(5);

Upvotes: 0

David L. Walsh
David L. Walsh

Reputation: 24815

I suggest the following:

from math import sqrt

def is_prime(x):
  if x < 2:
    return False

  if x % 2 == 0:
    return x == 2

  i = 3
  while i <= sqrt(x):
    if x % i == 0:
      return False
    i += 2

  return True

Things to note:

  • The only even prime is 2. The loop is restricted to odd numbers.
  • Only factors up to and including the square root must be tested. Because if x = y * z and y ≥ sqrt(x) then z ≤ sqrt(x)
  • Only one return statement in the loop. You can exit early on a non prime, but you must complete the loop to be sure the number is prime.

Upvotes: 1

Akash Deep Sharma
Akash Deep Sharma

Reputation: 29

Spelling of is_prime at the time of method call is wrong. Also, you haven't mke a call to the method main at the end.

Small improvement:

for i in range(2, number):

this line can be as below as we don't need to check divisibility of the number after its half:

for i in range(2, (number/2)):

e.g. for checking 24 is prime or not, we need to check only upto 12 because it will never be divisible by number greater than 12.

Upvotes: 0

Rifat Bin Reza
Rifat Bin Reza

Reputation: 2761

You need to call the main() and write is_prime() correctly.

def is_prime(number):
if number == 2:
    return True
elif number < 2:
    return False
else:
    for i in range(2, number):
        if number % i == 0:
            return False
        else:
            return True
def main():
    print(is_prime(1))
    print(is_prime(4))
    print(is_prime(7))
main()

Upvotes: 0

Related Questions