Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

All responses must be orignal or will not be accepted as a valid answer. Primali

ID: 3885618 • Letter: A

Question

All responses must be orignal or will not be accepted as a valid answer.

Primality Test Program

Write a program to test for primality of any number N (that is, to test if N is a prime number). Start by writing an algorithm (a set of precise mathematical steps that can be written into a program) to test for primality. For full credit, run the program and use it to test for primality on several numbers. See what the largest number your program can handle is before you find it too slow to yield a result in a reasonable time (if it takes longer than 10 minutes to execute, you are done).

Use your own imagination. Remember that if the number N is even, it cannot be a prime number. You can write your program to simply try if it is a multiple of any numbers smaller than N. Also you can design your program to store all prime numbers less than N that your program has found, then take advantage of the fact that a number only needs to be tested to see if it is a multiple of those prime numbers smaller than N/2. If it is not, then it must be a prime number itself.

You may use any language you prefer, but it is recommended you use C# or C++ because these are the languages supported by DeVry in the programming courses you have taken.

Explanation / Answer


#include <iostream>
using namespace std;

int main()
{
int number, iteration;
bool isPrimeNumber = true;

cout << "Enter a positive integer: ";
cin >> number;

for(iteration = 2; iteration <= number / 2; ++iteration)
{
      if(number % iteration == 0)
      {
          isPrimeNumber = false;
          break;
      }
}
if (isPrimeNumber)
      cout << "This is a prime number";
else
      cout << "This is not a prime number";

return 0;
}