1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
KonstantinChe [14]
3 years ago
9

Define a function is_prime that receives an integer argument and returns true if the argument is a prime number and otherwise re

turns false. (An integer is prime if it is greater than 1 and cannot be divided evenly [with no remainder] other than by itself and one. For example, 15 is not prime because it can be divided by 3 or 5 with no remainder. 13 is prime because only 1 and 13 divide it with no remainder.) This function may be written with a for loop, a while loop or using recursion. Use the up or down arrow keys to change the height.
Computers and Technology
1 answer:
qaws [65]3 years ago
5 0

Answer:

<em>This function (along with the main)is written using python progrmming language.</em>

<em>The function is written using for loop; </em>

<em>This program does not make use comments (See explanation section for detailed line by line explanation)</em>

<em></em>

def is_prime(num):

    if num == 1:

         print(str(num)+" is not prime")

    else:

         for i in range(2 , num):

              if num % i == 0:

                   print(str(num)+" is not prime because it can b divided by "+str(i)+" and "+str(int(num/i))+" with no remainder")

                   break;

         else:

              print(str(num) + " is prime because only 1 and "+str(num)+" divide it with no remainder")

num = int(input("Number: "))

is_prime(num)

Explanation:

This line defines the function is_prime

def is_prime(num):

The italicize line checks if the user input is 1; If yes it prints 1 is not prime

<em>     if num == 1: </em>

<em>          print(str(num)+" is not prime") </em>

If the above conditional statement is not true (i.e. if user input is greater than 1), the following is executed

    else:

The italicized checks if user input is primer (by using for loop iteration which starts from 2 and ends at the input number)

         <em>for i in range(2 , num): </em>

<em>               if num % i == 0: </em>

<em>This line prints if the number is not a prime number and why it's not a prime number</em>

<em>                    print(str(num)+" is not prime because it can b divided by "+str(i)+" and "+str(int(num/i))+" with no remainder") </em>

<em>                    break; </em>

If otherwise; i.e if user input is prime, the following italicized statements are executed

         <em>else: </em>

<em>               print(str(num) + " is prime because only 1 and "+str(num)+" divide it with no remainder") </em>

<em>The function ends here</em>

The main begins here

num = int(input("Number: "))  This line prompts user for input

is_prime(num) This line calls the defined function

You might be interested in
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 be
Gemiola [76]

Answer:

The c++ program to check prime numbers is shown below.

#include <iostream>

using namespace std;

bool isPrime(int n);

bool isPrime(int n)

{

   bool prime;

   int p=0;

   

   if(n==2 || n==3)

       prime = true;

   else if(n%2 == 0)

       prime = false;

   else

   {

       for(int k=3; k<n/2; k++)

       {

           if(n%k == 0)

               p++;

       }

   

   if(p>1)

       prime = false;

   else

       prime = true;

   }

   

   return prime;

}

int main() {

   int num;

   do

   {

       cout<<"Enter a positive number."<<endl;

       cin>>num;

       if(num<1)

       {

           cout<<"Invalid number. Enter a positive number"<<endl;

           cin>>num;

       }

   }while(num<2);

   

   cout<<"The "<<num<<" is prime. "<<isPrime(num)<<endl;

   

   

}

 

OUTPUT

Enter a positive number.

-4

Invalid number. Enter a positive number

0

Enter a positive number.

101

The 101 is prime. 1

Explanation:

The user input is validated for positivity. A do while loop along with if statement is implemented for verification.

do

   {

       cout<<"Enter a positive number."<<endl;

       cin>>num;

       if(num<1)

       {

           cout<<"Invalid number. Enter a positive number"<<endl;

           cin>>num;

       }

   }while(num<1);

The test for prime number is done by using multiple if else statements and a Boolean variable prime is used.

If user inputs 2 or 3, variable prime is set to true.

Else If user inputs an even number, variable prime is set to false. This is done by taking modulo of the number upon division by 2.

Else if user inputs neither an even number nor a number less than 3, the modulus of the number is taken with divisors beginning from 3 up to half of the input number. Here, an integer variable p is used and based on its value, variable prime is set to true or false.

For this, an integer variable p is initialized to 0. A number can be completely divisible by itself or by its factors.

If the number is divisible by any of the divisors, value of variable p is increased by 1. If value of p is greater than 1, this means that the user input is divisible by more than one divisor. Hence, the given number is not a prime number and the variable prime is set to false. Otherwise prime will be set to true.

The value 1 indicates true and 0 indicates false.

4 0
3 years ago
Prove that any amount of postage greater than or equal to 64 cents can be obtained using only 5-cent and 17-cent stamps?
elixir [45]
Let P(n) be "a postage of n cents can be formed using 5-cent and 17-cent stamps if n is greater than 63".Basis step: P(64) is true since 64 cents postage can be formed with one 5-cent and one 17-cent stamp.Inductive step: Assume that P(n) is true, that is, postage of n cents can be formed using 5-cent and 17-cent stamps. We will show how to form postage of n + 1 cents. By the inductive hypothesis postage of n cents can be formed using 5-cent and 17-cent stamps. If this included a 17-cent stamp, replace this 17-cent stamp with two 5-cent stamps to obtain n + 1 cents postage. Otherwise, only 5-cent stamps were used and n  65. Hence there are at least three 5-cent stamps forming n cents. Remove three of these 5-cent stamps and replace them with two 17-cent stamps to obtain n + 1 cents postage.Hence P(n + 1) is true.
6 0
4 years ago
Assume that the ciphertext C is computed from the plaintext P by C= P ⊕ K1 ⊕ K2 , where K1 and K2 are the encryption key and ⊕ d
Sedbober [7]

Answer:

K1 ⊕ K2 = 9

Explanation:

Since 5 ⊕ K = 12, K must be 5 ⊕ 12 = 9.

But there are supposedly two encryptionkeys, we cannot know their individual value, only that K1 ⊕ K2 = 9.

That makes this a bit weird question.

4 0
4 years ago
How do u type faster
eduard

Answer:

by learning the right home keys and how to place the fingers

Explanation:

the left hand side : A,S,D,F

the right hand side: :, L, K, J

the two little fingers are guide keys

guide keys: letter A and column

5 0
4 years ago
Hannah decides to burn her video portfolio to a DVD. She wants to hand it over to a movie producer she will meet at a social eve
Gwar [14]
C maybe I am not sure this
7 0
3 years ago
Other questions:
  • Many people view drug and alcohol abuse as a social problem.<br> A. TRUE<br> B. FALSE
    9·1 answer
  • Write a static method named textcount that takes a scanner representing a file as a parameter and that reports various statistic
    11·1 answer
  • If you could make any blog, what would it be about and why?
    9·1 answer
  • After you save a table, the new table name appears ____.
    12·2 answers
  • You have just replaced the motherboard in your computer. now your computer will not start. you press the power button on the sys
    6·1 answer
  • What option from the format tab should you use to remove unwanted parts of a picture
    8·1 answer
  • A potentiometer a three wire variable resistor is used in which type of sensor ?
    13·1 answer
  • private members of a class are accessible only from _____________ of the same class or from their friends
    12·1 answer
  • Ict quiz I attached a picture
    11·2 answers
  • Date... Page No I write program to the check whether the supplied number is prime or not using a function procedor​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!