Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime.
Question
Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime.
Solution
Sure, here is a simple Python program that checks if a number is prime:
def is_prime(n):
if n <= 1:
return False
elif n <= 3:
return True
elif n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
n = int(input("Enter a positive integer: "))
if is_prime(n):
print(n, "is a prime number")
else:
print(n, "is not a prime number")
This program works by checking divisibility of n starting from 2 up to the square root of n. If n is divisible by any of these numbers, then it is not a prime number. If n is not divisible by any of these numbers, then it is a prime number.
Similar Questions
prime number in python
Create a Python function that takes a list of integers as input andreturns a new list containing only the prime numbers from the input list.Provide a clear explanation of your implementation and include a test case.
Complete the code to find if a given number is a prime number? The program will take a positive integer greater than 1 as input and indicate if it is a prime number by saying "prime", and if it is not a prime number saying "not a prime". Note there are 3 places in the given code that you need to fix for this code to work properly and give the expected output.
write easies code for me : Write a program in C to find if a number is prime or not.
Problem Statement:Find whether the given number is prime or not.Input Format:Accept an integer as inputOutput Format:Print the output as "Prime Number" or "Not a Prime Number"Constraints:1 <= INPUT <=10^15Sample Input 1:31Sample Output 1:Prime NumberSample Input 2:2Sample Output 2:Prime NumberSample Input 3:15Sample Output 3:Not a Prime Number
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.