Aprime number is a number that is only evenly divisble by itself and 1. for example,

Aprime number is a number that is only evenly divisble by itself and 1. for example, the number 5 is prime because it can only be evenlly divided by 1 and 5. the number 6, however, is not prime because it can be divided evenly by 2 and 3. 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 and then prints whether the number is prime.

Related Posts

This Post Has 7 Comments

  1. Let's be honest, there are plenty of solutions on the internet. Here's one where I added a program to use it. Note that this one is optimized to not consider even numbers, and to only iterate to the squareroot of the number you're testing.

    def is_prime(n):

       if n == 2 or n == 3: return True

       if n % 2 == 0 or n < 2: return False

       for i in range(3, int(n**0.5)+1, 2):  

           if n % i == 0:

               return False

       return True

    x = int(input("Enter a number: "))

    print("{} is {}a prime.".format(x, "" if is_prime(x) else "not "))

  2. Java program given below

    Explanation:

    Java Code: Save this file as Main.java and keep This file and GenericQueue.java in same folder

    class Main {

                   public static boolean isPrime(int n){

                                   for (int i=2; i<= n/2; i++){

                                                   if (n % i== 0)

                                                                   return false;

                                   }

                                   return true;

                   }

                   public static void main(String[] args) {

                                   GenericQueue<Integer> queue = new GenericQueue<Integer>();

                                   int n = 0, p = 2;

                                   while(n <= 50)

                                   {

                                                   if(isPrime(p))

                                                   {

                                                                   queue.enqueue(p);

                                                                   n++;

                                                   }

                                                   p++;

                                   }

                                   System.out.println(queue);

                   }

    }

  3. // here is code in C++.

    #include <bits/stdc++.h>

    using namespace std;

    // function to check prime

    bool isPrime(int n)

    {

       // if input is less than 1

      if(n<1)

          return false;

       // if input is 1,2 or 3

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

      {

          return true;

      }

      // if input is greater than 3

      else

      {

          for(int i=2; i<n; i++)

          {

              if(n%i==0)

                  return false;

          }

          return true;

      }

    }

    // driver function

    int main()

    {

       // variable

       int num;

      cout << "Enter a number: ";

      // read the input number

      cin >> num;

       // call function to check prime

      if (isPrime(num)==true)

      // if prime

          cout << num << " is prime.";

      else

      // if not prime

          cout << num << " is not prime.";

    return 0;

    }

    Explanation:

    Read a number from user and assign it to "num".Then call the function isPrime() with "num" as parameter.In this function, if the input is less than 1 then it will return "false".If input is 1 or 2 or 3 then it will return "true".For an input greater than 3, if the number is divisible by only 1 and itself then function will return "true" otherwise it will return "false".

    Output:

    Enter a number: 17

    17 is prime.

  4. Or mine means that itself and 1 can only go into it. Conposit means that multiple numbers can go into it like 24. 12,6,8,2 can all go into it

  5. public class Main {    public static void main (String [] args) {        for(int i = 2; i < 10000; i++){            if(isPrime1(i)){               System.out.print(i + " ");            }        }        System.out.println();        for(int i = 2; i < 10000; i++){            if(isPrime2(i)){                System.out.print(i + " ");            }        }    }    public static boolean isPrime1(int n){              for(int i=2; i <= n/2; i++){            if(n % i  == 0){                return false;            }        }        return true;    }    public static boolean isPrime2(int n){        for(int i=2; i <= Math.sqrt(n); i++){            if(n % i  == 0){                return false;            }        }        return true;    } }

    Explanation:

    Firstly, create the first version of method to identify a prime number, isPrime1. This version set the limit of the for loop as n/2. The for loop will iterate through the number from 2 till input n / 2 and check if n is divisible by current value of i. If so, return false to show this is not a prime number (Line 22 - 26). Otherwise it return true to indicate this is a prime number.

    In the main program, we call the isPrime1 method by passing the i-index value as an argument within a for-loop that will iterate through the number 2 - 10000 (exclusive).  If the method return true, print the current i value). (Line 5 - 9)

    The most direct way to ensure all the prime numbers below 10000 are found, is to check the prime status from number 2 - 9999 which is amount to 9998 of numbers.

    Next we create a second version of method to check prime, isPrime2 (Line 31 - 40). This version differs from the first version by only changing the for loop condition to i <= square root of n (Line 33).  In the main program, we create another for loop and  repeatedly call the second version of method (Line 13 - 17). We also get the same output as in the previous version.

Leave a Reply

Your email address will not be published. Required fields are marked *