Define the function is_prime(num) to check if given positive integer is a prime number or not. The function return a bool True if a number is divisible only by 1 and itself and a bool False if it is divisible by any other number than 1 or itself. Consider 1 be a prime number. 2 is also prime.Examplesis_prime(7) -> Trueis_prime(100) -> Falseis_prime(1) -> Trueis_prime(2) -> True
Question
Define the function is_prime(num) to check if given positive integer is a prime number or not. The function return a bool True if a number is divisible only by 1 and itself and a bool False if it is divisible by any other number than 1 or itself. Consider 1 be a prime number. 2 is also prime.Examplesis_prime(7) -> Trueis_prime(100) -> Falseis_prime(1) -> Trueis_prime(2) -> True
Solution
To define the function is_prime(num), follow these steps:
- Start by defining the function
is_primewith a parameternum. - Inside the function, check if
numis equal to 1. If it is, returnTruesince 1 is considered a prime number. - Check if
numis equal to 2. If it is, returnTruesince 2 is also considered a prime number. - If
numis not equal to 1 or 2, proceed to the next step. - Create a loop that iterates from 2 to the square root of
num(inclusive). You can use themath.sqrt()function to calculate the square root. - Inside the loop, check if
numis divisible by the current iteration value. If it is, returnFalsesince it is divisible by a number other than 1 or itself. - If the loop completes without finding any divisors, return
Truesincenumis only divisible by 1 and itself.
Here is the code implementation:
import math
def is_prime(num):
if num == 1:
return True
if num == 2:
return True
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
return False
return True
Now, you can test the function with the given examples:
print(is_prime(7)) # Output: True
print(is_prime(100)) # Output: False
print(is_prime(1)) # Output: True
print(is_prime(2)) # Output: True
This function will return True if the number is only divisible by 1 and itself, and False if it is divisible by any other number.
Similar Questions
Please create a pseudocode function that can check if a given number n is prime or not, where n > 1.
A prime number is an integer greater or equal to 2 that is only divisible by 1 and by itself. The first few primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 …N is a prime if and only if it is not divisible evenly by any of the numbers from 2 to N−1. Let’s implement this decision as a function.
Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime.A prime number is a positive integer that is:greater than 1, andcan be divided exactly (without leaving a remainder) only by 1 or itself.
A prime number is an integer greater or equal to 2 that is only divisible by 1 and by itself. The first few primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 …N is a prime if and only if it is not divisible evenly by any of the numbers from 2 to N−1. Let’s implement this decision as a function.In the same program numbers.cpp, add a functionbool isPrime(int n);The function should return true if n is a prime, otherwise return false. Change the main function to test your new code.
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.
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.