Question

(In Python) Complete the isPrime() function to take an int and return True if it is...

(In Python) Complete the isPrime() function to take an int and return True if it is a prime number and False if it is not.

Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2). For example, if num is 7, then you could test:

  • if 7 remainder 6 is not 0, then

  • if 7 remainder 5 is not 0, then

  • if 7 remainder 4 is not 0, then

  • if 7 remainder 3 is not 0 and finally

  • if 7 remainder 2 is not 0.

If it fails any one of these tests, then num is not prime. Otherwise, it is prime.

Examples:

isPrime( 2 ) returns True

isPrime( 3 ) returns True

isPrime( 4 ) returns False (4 is divisible by 2)

isPrime( 7 ) returns True

isPrime( 9 ) returns False (9 is evenly divisible by 3)

isPrime( 101 ) returns True

Homework Answers

Answer #1

Source Code:

def isPrime(num):
for i in range(2,num-1):
if(num%i==0):
return False
return True

print(isPrime(2))
print(isPrime(3))
print(isPrime(4))
print(isPrime(7))
print(isPrime(9))
print(isPrime(101))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
Write the following in C: 2. An integer is said to be prime if it is...
Write the following in C: 2. An integer is said to be prime if it is divisible by only 1 and itself. Write a function called prime() that takes in one integer number, and returns 1 (true) if the number is prime and 0 (false) otherwise. Write a program to generate six random numbers between 1 to 100 and calls function prime() on each one to determine if it is prime or not.
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing...
Write a method which takes as input an integer, returns true if the integer is prime,...
Write a method which takes as input an integer, returns true if the integer is prime, and returns false otherwise. Do not import anything. If the input is negative, 0 or 1, false should be returned. If the input x is greater than 1, you can test if it is prime (inefficiently) by checking if it is divisible by any integer from 2 up to half of x. If it is not divisible by any of these numbers, then it...
A Harshad number (or a Niven number) is a number that is evenly divisible by the...
A Harshad number (or a Niven number) is a number that is evenly divisible by the sum of its digits. An example is 18 (1+8=9, 18%9 = 0). Write a function called isHarshad(num) that takes an integer as an argument and returns True if the number is a Harshad number and False if it is not. Then, use this function to create a list of all of the Harshad numbers in the first 500 natural numbers.
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
1.Implement a function which will -       + Accept a number, num, and a list, my_list...
1.Implement a function which will -       + Accept a number, num, and a list, my_list as arguments       + return True if num exists in my_list       + return False otherwise 2. Write Python code to implement the following function - def find_in_list(item, my_list):     """     Look for an item from my_list (of items.)     If the item is found, return the index position of the item;     otherwise, return -1.     """       #add your code here...
def is_even(value: int) -> bool: """Return True if and only if value is an even number....
def is_even(value: int) -> bool: """Return True if and only if value is an even number.    >>> is_even(108) True >>> is_even(135) False """ def convert_time(hour: int) -> int: """Return the given time in hours hour from the 24 hour clock to a time in hours for the 12 hour clock. Precondition: 0 <= hour <= 23 >>> convert_time(0) 12 >>> convert_time(4) 4 >>> convert_time(15) 3 """
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...