Define a function is_prime that receives an integer argument and returns true if the argument is a prime number and otherwise returns 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.

Respuesta :

Answer:

This function (along with the main)is written using python progrmming language.

The function is written using for loop;

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

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

    if num == 1:

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

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)

         for i in range(2 , num):

              if num % i == 0:

This line prints if the number is not a prime number and why it's not a prime number

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

                   break;

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

         else:

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

The function ends here

The main begins here

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

is_prime(num) This line calls the defined function