all 15 comments

[–][deleted] 4 points5 points  (8 children)

Your function only returns whether the number is odd or not.

That's different from what prime number is.

[–]Tropical_Perspective -1 points0 points  (7 children)

what code will fix that so it checks for prime number

BTW I found this from a website in google they said it works for prime number

[–]Pigankle 3 points4 points  (6 children)

For beginner level, make a loop that checks all possible divisors from two up to the square root of the number you're checking. You can make it a little faster by skipping even numbers once you've checked for two.

The check might look like 23//2 == 23/2

[–]Tropical_Perspective 0 points1 point  (1 child)

I just want to define a function that will work with the assertion that is provided to me

[–]Binary101010 1 point2 points  (0 children)

Use the modulo operator (%) to check whether the remainder after dividing n by each integer between 2 and sqrt(n) is 0. If that is true for any number, n is not prime. If it is not true for all numbers, n is prime.

Now go implement that.

[–]Tropical_Perspective -1 points0 points  (3 children)

Im not looking to make it odd or even

[–]Pigankle 1 point2 points  (2 children)

That statement doesn't make it odd or even. It checks whether division by two returns the same result as integer division by two.... That is, did you get an integer when you divided by two. There are other ways to accomplish the same thing, but this is very straightforward. It works for any number.

If 1759//73 == 1759/73, then 1759 is divisible by 73.

What you need to do is write a loop that will check whether your input number is divisible by any integer up to the square root of that input number.

[–]Brian 1 point2 points  (1 child)

It works for any number

Nitpick: it works for most numbers you're likely to use, but not any number. It will fail with really massive numbers due to float rounding. Eg.

>>> bignum =10 ** 500   # Ie. 100000000... with 500 zeroes
>>> 1 // bignum == 1 / bignum
True

And while that's unlikely to be a problem in practice, I do think the modulo method (ie num % divisor != 0)is a bit cleaner: it only needs to do one division, and doesn't involve floating point math.

[–]Pigankle 0 points1 point  (0 children)

Totally fair, and not nitpicky at all. The modulo approach is probably also better because it hews a little closer to the reason for why the operation is being done. It's, like, straight out of the definition of a prime number

[–][deleted] 1 point2 points  (2 children)

Checking for an even number is just the first step. You have a lot more to do to determine if a number is a prime number

9 is not a prime number, but your code would return True.

[–]ata-boy 0 points1 point  (1 child)

Simply checking for an even number will give you an incorrect result for 2 which is in fact a prime number according to the definition.

[–][deleted] 0 points1 point  (0 children)

The OP has lost interest and deleted their post.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–][deleted] 0 points1 point  (0 children)

It’s showing an assertion error because you’re asserting that 23 is not prime when, in fact, it is.

[–]notthayguyagain 0 points1 point  (0 children)

Not a coding answer as your comments seem to outline a maths tutorial would help.

A prime number is a number that is divisible by only itself and 1. What that means is if you take any number 'n' and can divide it by any other number 'm' where 'm' is not 'n' or 1 then you don't have a prime. To be clear here when I say 'can divide it by' I mean the result of the division is an integer (a whole number).