0

I am beginner to python. I wrote the following code for identifying a prime number

import math

n = int(input("Enter any natural number:"))
for k in range (2, n):
    x = math.floor(n / k)
    if x == n / k:
        print("n is not a prime")
    else:
        print("n is a prime")

The output that I get is as follows when I took n=23:

Enter any natural number:23
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime
n is a prime

[Program finished]

Rather than getting all these lines, can't I get a single line giving me ok if none if k in the range divides n and vice versa?

wovano
  • 4,543
  • 5
  • 22
  • 49
RAHUL
  • 109
  • 2
  • 1
    Why do you print "prime" for every number you check? Finding a single divisor is enough for it to not be prime, but if it's prime you must go through your entire loop to decide – Pranav Hosangadi Jan 05 '23 at 06:42

4 Answers4

1

Use the code as follows.

n=int(input("Enter any natural number:"))
n_p = False
for k in range (2,n):
    if n % k == 0:
        n_p = True
        break

if n_p:
    print("n is not a prime")
else:
    print("n is a prime")

If n is divided by k, then n_p becomes True and it prints "n is not a prime". Otherwise, n_p is still False and it prints "n is a prime"

Gilseung Ahn
  • 2,598
  • 1
  • 4
  • 11
0

One way is to create a function to check if it is prime or not..!

import math
def check_prime_number(n):
    for k in range(2,n):
        x=math.floor(n/k)
        if(x==n/k):
            return "n is not a prime"  
    return "n is a prime"
    
n=int(input("Enter any natural number:"))
print(check_prime_number(n))

return is used to end the execution of the function call. we know that if any number b/w [2,n-1] can divide the number [n] than it is not a prime number so.. at any time if if condition satisfy it prints n is not prime and end the execution of the function. if if condition not satisfy till last we can say that it is a prime number so after the end of for loop we have write n is a prime and the function ends.

Output:-

Enter any natural number:23
n is a prime
Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
0

For example:

n=int(input("Enter any natural number:"))
for k in range (2,n):
    if not n % k:
        print("n is not a prime")
        break
else:
    print("n is a prime")
-1

You are getting this because your print statement is in for loop. To solve this you need to use if else statment outside of the for loop. So your final code should be:

import math

n=int(input("Enter any natural number:"))

for k in range (2,n):

    x=math.floor(n/k)
  
if(x==n/k):

     print("n is not a prime")

else:

     print("n is a prime")

Or, you can do it without math library

n = int(input("Check this number: "))
is_prime= True
for N in range(2,n):
  if n%N ==0:
    is_prime= False
if is_prime:
  print("It's a prime number.")
else:
  print("It's not a prime number.")
Shounak Das
  • 350
  • 12