S. Chen
S. Chen

Reputation: 25

Python - function returns true when a given number is a prime number or else false

Hi I'm a beginner and I'm stuck on this question that wants me to use only while loop to solve. The question wants me to write a function that returns True when the given number is a prime number and it returns False if the given number is not a prime number.

My code so far:

def is_prime(n):
    i = 2
    while i <= n//2:
        if n%i != 0:
            return True
        else:
            return False
        i+=1

The problem I have is I think my code displays the correct output for numbers 4 and above and it returns 'None' for 1, 2, and 3. I've debugged it and I think the problem is the while loop condition. But I don't know how to fix it. I would appreciate it if any of you pros can help me out!

edit: I changed the while condition but 1 still returns None.. and 2 returns False when it's supposed to return True

def is_prime(n):
    i = 2
    while i <= n:
        if n%i != 0:
            return True
        else:
            return False
        i+=1

Upvotes: 1

Views: 3877

Answers (4)

oppressionslayer
oppressionslayer

Reputation: 7222

If you want to impress your lesson teacher you can show him a fast probabilistic prime number isprime for numbers larger than 2**50. I haven't found any errors in it after weeks of cpu time stress testing it on a 6 core AMD:

import random
import math

def lars_last_modulus_powers_of_two(hm):
   return math.gcd(hm, 1<<hm.bit_length())

def fast_probabilistic_isprime(hm):
    if hm < 2**50:
       return "This is to only be used on numbers greater than 2**50"
    if lars_last_modulus_powers_of_two(hm+hm) != 2:
       return False
    if pow(2, hm-1, hm) == 1:
       return True
    else:
       return False

def fast_probabilistic_next_prime(hm):
   if hm < 2**50:
       return "This is to only be used on numbers greater than 2**50"
   if hm % 2 == 0:
      hm = hm + 1
   hm += 2
   while fast_probabilistic_isprime(hm) == False:
      hm += 2
   return hm

""" hm here is bitlength, which must be larger than 50.
    usage is create_probabilistic_prime(1000)
"""
def create_probabilistic_prime(hm):
   if 2**hm < 2**50:
       return "This is to only be used on numbers greater than 2**50"
   num = random.randint(2**hm,2**(hm+1))
   return fast_probabilistic_next_prime(num)

Upvotes: 0

Jack
Jack

Reputation: 6198

Want to get really fancy? Make a Sieve of Eratosthenes:

def is_prime(n):
    a = list()
    # Assume all are prime
    a[0:n+1] = (n+1)*[1]

    # Start with removing even numbers
    i = 2
    while i*i <= n:
        print ("I: ", i)
        # Set all divisible by i to 0
        a[0:n+1:i] = len(a[0:n+1:i])*[0]

        # If a[n] is zero, return False
        if a[n] == 0:
            return False

        # Increment i until we have a prime number
        while a[i] == 0:
            i+=1

    if a[n] == 0:
        return False
    else:
        return True

Upvotes: 0

Maurice Meyer
Maurice Meyer

Reputation: 18136

You could hard-code these 3 cases, in case you dont want to use sqrt:

def is_prime(n):
    i = 2

    if n in (1,3):
        return True
    elif n == 2:
        return False

    while i <= n//2:
        if n%i != 0:
            return True
        else:
            return False
        i+=1

for x in range(1, 5):
    print(x, '=', is_prime(x))

Output:

(1, '=', True)
(2, '=', False)
(3, '=', True)
(4, '=', False)

Upvotes: 1

Jack
Jack

Reputation: 6198

import math;
def is_prime(n):
    i = 2
    while i < max(math.sqrt(n),2):
        if n%i != 0:
            return True
        else:
            return False
        if i == 2:
            i+=1
        else
            i+=2

Upvotes: 2

Related Questions