1

I'm new to python, and I can't see why my for loop is ignoring the if statement and saying every number is a prime number. My code is as such.


def prime_number(number):
    sqrt_prime = math.sqrt(number)
    for x in (2,sqrt_prime-1):
        if isinstance((number/x), int) and x <= sqrt_prime-1:
            con_num = str(number)
            print(con_num,end=' ');print('is not a prime number.')
            break
    else:
        con_num = str(number)
        print(con_num,end=' ');print('is a prime number.')

It could probably be a lot more streamlined, just want to know why it isn't either outputting true for the isinstance or that x is smaller or equal to the sqrt of the number - 1. Thanks in advance.

4
  • 1
    check type in isinstance it may be float is you're using python3 Commented Mar 8, 2021 at 4:15
  • number/x will always be a float in python3, so your condition will not be satisfied(The same opinion as @neilharia7) Commented Mar 8, 2021 at 4:16
  • I wonder if we can reduce the range of (2, sqrt_prime-1) to half. Commented Mar 8, 2021 at 5:24
  • You might consider checking a potential factor like this: if number % x == 0 instead of using / though personally I would be cautious about 0 vs 0.0 and getting incorrect answers from rounding. You might also try if number / x == number // x:. Commented Mar 8, 2021 at 5:26

2 Answers 2

1

If this is python 3.x then it makes sense why it ignores it.

number/x will always return a float and not an integer. Thus, isinstance((number/x), int) is always False.

Sign up to request clarification or add additional context in comments.

1 Comment

The else statement is attached to the for loop, to be executed if the loop does not break. So it is properly indented.
0

The problem is isinstance((number/x), int).

The type is always float if you check them by

print(type(number/x))

Correction: Change it from

        if isinstance((number/x), int) and x <= sqrt_prime-1:

to

        if number % x == 0 and x <= sqrt_prime-1:

Another problem is the coding:

def prime_number(number):
    sqrt_prime = math.sqrt(number)
    flg = 0
    for x in (2,sqrt_prime-1):
        if number % x == 0 and x <= sqrt_prime-1:
            con_num = str(number)
            print(con_num,end=' ');print('is not a prime number.')
            flg = 1
            break
    if flg==0:
            con_num = str(number)
            print(con_num,end=' ');print('is a prime number.')

I am not sure if you have finished the code, seems like this is algorithm is not running rightly.

2 Comments

Thanks, fixed the float issue. I also realised I forgot to make it range(2,int(sqrt_prime)-1), so it was only dividing by 2 and then moving on, rather than going up all the values from 2 to the sqrt -1
if not number % x and x < sqrt_prime, print(number, 'is not a prime number.') Check this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.