# Am I stupid? 6. is_prime

#1

I couldn't understand the instructions, so I've made completly another code, which works too, but us a lot larger

Should I leave this code liek this or should I do this way as the codecademy wants me to?

``````def is_prime(x):
if x<2:
return False
elif x%2 == 0:
if x==2:
return True
else:
return False
elif x%3 == 0:
if x==3:
return True
else:
return False
elif x%5==0:
if x==5:
return True
else:
return False
elif x%7 == 0:
if x==7:
return True
else:
return False
else:
return True``````

#2

It is a right way, too, but making the program lighter (not lighter, just lighter) like this is always recommended.

def is_prime():
if number == 2 or number > 1:
return True
if number % 2 == 0:
return False
for current in range(3, int(sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False

as you see it does all the things that your code does, but it's less heavy

#3

Yeah, but I don;t understand most of this code

#4

Which one?
(Why that 20 characters is required)

#5

By the Way Codecademy doesn't say, that you have to do it by the shorter way.

#6

(what 20 characters?)

#7

Codecademy requires 20 charcacters to a post, so I just can't post "which one"

#8

So you won't say me what did u do?

#9

The code can be as long as your one is, but with a few if statements we can do the same thing.
so first we have this:

if number % 2 == 0:
return False

if it divided by two is zero, it is false.

then we have this for- loop.
for current in range(3, int(sqrt(number) + 1), 2):
if number % current == 0:
return False

for current integer numbers in range 3 - 4, if the number divided by 'current' is zero it is false.
that's what your code does, too but this is shorter.

#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.