6. is_prime


#1

Why isnt this working?

def is_prime(x):
  impossible = []
  if x > 1:
      impossible.append(range(2,x)) 
  for i in impossible:
      for y in i:
          if x % int(i[y]) == 0:
              return False
          else:
              return True
  else:
      return False

#2

1) If you think about how you would do this manually, how many loops do you need?

2) In your loop you check if there's a remainder after division. If there isn't, then you say it isn't a prime, but if it is, you say it IS prime, after only having checked the remainder with a single divisor. You need to repeat this test many times before you can say that it is a prime, so you can't return that early - and besides, is it even a loop if you always return on the first iteration?


#3

My solution:
def is_prime(x):
if x==2:
return True
elif x>2:
j=0
for q in range(2,x):
if x%q==0:
j+=1
if j>=1:
return False
else:
return True
else:
return False


#4

You are treating "i" as a range when it is a single variable defined against the list "impossible". Thats one issue you have getting working code to determine a prime number. The other is approach. Using a list is not a n efficient way to go.

  for i in impossible:
  for y in i:
      if x % int(i[y]) == 0:

I have awful trouble figuring this out and it turns out that indentation key stroke stumped me for a day! <>

My approach in figuring it out was based on the use of range() to determine a prime number greater then 2. Using a for loop efficiently was the way way to go but BEWARE of your whitespace. Indent properly or you will get trapped like I did.

Here is my solution to the prime number issue:

x = int(raw_input("please enter your prime number for checking now"))

def is_prime(x):
    if x == 2:
        return True
        print "X equals two, True printed" #TEST CODE
    elif x < 2:
        return False
        print "Program recognises x < 2" #TEST CODE
    elif x == 3:
        return True
        print "X equals three, True printed" #TEST CODE
    elif x > 3:
        print "just before for loop" #TEST CODE
        for n in range(2, x - 1):
            if x % n == 0:
                return False
        return True
print is_prime(x)

#5

A few problems here:

  1. First off: I don't understand your solution.

  2. If I copy it, it won't work. The error is: "IndentationError: expected an indented block" on line 7 (if x % q == 0:)

In line 7, it starts getting weird for me. I understand that you check "if x / q has no rest". But then, what is and why would you += it? Can you explain your solution? Or can anyone else?


#6

This is my solution:

def is_prime(x):
    if x > 0 and x <=2 and x != 1:
        return True
    elif x > 0 and x > 2 :
        for n in range(2,x-1) :
            print n
            if x%n == 0 :
                return False
        return True
    else:
        return False

#8

Hi,
Does your code work with value 0?
I have a similar code as below but I am getting error that - “Your function fails on is_prime(0). It returns True when it should return False.”

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


#9

This solution is similar to tacoasian’s, but with simpler coding.It determines if a value is less than 2, it is not prime and returns false. If it is greater than 2 and cannot be easily divided by a number between 2 and x-1, than it is true.
Example: x == 10
10 < 2 is false
n = (x-1) or n = (10 - 1)
if 10 is equally divisible by any number within range 2 to 9, it is not prime. 10 is divisible by numbers within that range, so it is not prime and returns false.

def is_prime(x):
    if x < 2:
        return False
    elif x >=2:
        for n in range(2,x-1) :
            if x%n == 0 :
                return False
        return True
    else:
        return False

#10

This is really clear to me - thanks! I swapped your return and print statements though eg. for x = 1 it wasn’t printing.


#11

Hi, I don’t understand why the return True is indented with the loop. Maybe you can help me understand.
Thanks


#12

Yeah, why is the return True before the last else indented? Is that like saying else and which loop is it for?
Thanks