Question

In: Computer Science

5. A prime number is a number that is only evenly divisible by itself and 1....

5. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, how‐ ever, is not prime because it can be divided evenly by 1, 2, 3, and 6.   Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Use the function in a program that prompts the user to enter a number then displays a message indicating whether the number is prime.

6. In another program, use the function you wrote in question 5 to print the prime numbers between 1 and 100 using for loop.

Solutions

Expert Solution

5. SOURCE CODE

def isPrime(n):
if n <= 1 or n % 1 > 0:
return False
for i in range(2, n//2):
if n % i == 0:
return False
return True

6. SOURCE CODE

def isPrime(n):
if n <= 1 or n % 1 > 0:
return False
for i in range(2, n//2):
if n % i == 0:
return False
return True
for i in range(1,101):
print(isPrime(i))

OUTPUT

False
True
True
True
True
False
True
False
False
False
True
False
True
False
False
False
True
False
True
False
False
False
True
False
False
False
False
False
True
False
True
False
False
False
False
False
True
False
False
False
True
False
True
False
False
False
True
False
False
False
False
False
True
False
False
False
False
False
True
False
True
False
False
False
False
False
True
False
False
False
True
False
True
False
False
False
False
False
True
False
False
False
True
False
False
False
False
False
True
False
False
False
False
False
False
False
True
False
False
False


OUTPUT SCREENSHOT

Comment bellow for any doubts


Related Solutions

isPrime Function. A prime number is a number that is only evenly divisible by itself and...
isPrime Function. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. The...
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
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. b) Modify the...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume that we are given a list of all the prime numbers from 1 to 10,000, in sorted order in a text file called primes.txt: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,...
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
A number is prime if it can be divided exactly only by 1 and itself. Here...
A number is prime if it can be divided exactly only by 1 and itself. Here is an algorithm for checking if an input number is prime: function out = isprime(m)             for j:=2 to m-1             if mod(m,j) ==0 then             return “input is not a prime”             endif             endfor             return ”input is a prime” Let n denote the size of the input m, i.e. the number of bits in the binary expression for m. What is...
All years that are evenly divisible by 400 or are evenly divisible by four and not...
All years that are evenly divisible by 400 or are evenly divisible by four and not evenly divisible 100 are leap years. For example, since 1600 is evenly divisible by 400, the year 1600 was a leap year. Similarly, since 1988 is evenly divisible by four but not 100, the year 1988 was also a leap year. Using this information, write a C++ program that accepts the year as user input, determines if the year is a leap year, and...
10. Write a for loop which will print every number evenly divisible by 13 between 1...
10. Write a for loop which will print every number evenly divisible by 13 between 1 and 100, inclusive. Your loop will only increment by 1 each loop so you need an if test to see if each number should be printed. Put each number output on the same line, 1 space apart. 11. Write a while loop which will prompt the user for a number and accept their input and then display this message (assuming number input is 3)...
A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT