Python: To Check For Prime And Increment
i have exactly 5 days of practise, an hour daily so kindly forgive if my questions are very low level. No prior coding exp My objective of the below code is 1- check if entered num
Solution 1:
The way you have done has a lot of errors not efficient. By making some modification to your code i have made it much simpler. Read the comments to understand:
defprimetest (num): # check if number is a primereturn(all(num % i for i inrange(2, num)))
num = int (input ("enter a number:")) # main code:whileTrue: #loop continues until prime number found(True)if primetest(num):
print(num,"is a prime number.")
break#(stops loop if prime found)else: #otherwise repeat by incrementing to the next num until foundprint(num,"is not a prime number.")
num += 1
Output:
enteranumber:4545isnotaprimenumber.
46isnotaprimenumber.
47isaprimenumber.
Post a Comment for "Python: To Check For Prime And Increment"