-2

input a integer X and tell if it is a prime. If it is a prime, output 'Y' If not, output 'N' and the smallest prime factor.

Here is the program I have tried to write.

X = int(input('enter a integer X:')) for i in range(2, X): if X % i == 0: print('Y') else: print('N')

But I would like to print just one time 'Y' or 'N'. And I also do not know how to make the smallest prime factor show on my program result.

Thank you all for helping me

  • You may want to use the Miller-Rabin primality test to check if a number is prime. Here is a great resource and code for implementing it: https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture11.pdf – AVManerikar Oct 20 '22 at 04:34

2 Answers2

0

Use any and := walrus operator along with generator like this This will work for python >= 3.8.

n = int(input("Enter a number : "))
j = 2
if any(((k:=j) and (not n%j)) for j in range(2, int(n**0.5)+1)) :
    print(k)
else:
    print("prime")
Deepak Tripathi
  • 3,175
  • 1
  • 8
  • 21
-3
n = int(input("Enter Number:"))  # input from user
# ====================================PRIME CHECK==================================== #
flag = 0
for i in range(2,n):  # looping to check if number is divisible by other numbers
    if n % i == 0:
        flag = 1  # setting flag value to 1
        print(i)  # printing value of i for which n is divisible
        break  # to exit from the if clause
if flag == 1:  # checking if the value of flag is 1 then print it is not prime
    print(n, "is not a Prime")
else:  # else print prime and the value of n
    print(n, "is Prime")
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135