Quick Prime Number Test - Method: Basic

With prime numbers, we always get a headache. We will see 2 tutorials about quick prime number testing, this one at a basic level, the next one at an advanced level. The idea of our test is the following: We create an array of bool values, to represent numbers like this: 0 - FALSE 1 - FALSE 2 - TRUE 3 - TRUE 4 - FALSE 5 - TRUE 6 - FALSE 7 - TRUE 8 - FALSE 9 - FALSE 10 - FALSE 11 - TRUE And so forth! Where we have TRUE, it is a prime number, where we have FALSE, it is not a prime number. How to generate such an array? First of all, for N numbers we need N+1 elements, because array indexing in C++ goes from 0 to nr_of_elements-1. Then, we initialize 0, 1 and 2 manually. Obviously, for the beginning, we fill the array assuming every number is a prime number. The following declarations are required:
  1. #include <iostream>
  2.  
  3. #define N 1000001
  4.  
  5. using namespace std;
  6.  
  7. bool isPrime[N];
This is our bool array. Now let us see, how to fill the array with the right values. The initialization is simple, as all elements will be set to TRUE. Generating the prime number TRUE/FALSE values goes like this:
  1. void FindPrimesTo(int n) {
  2. isPrime[0] = isPrime[1] = false;
  3. isPrime[2] = true;
  4. int index1, index2;
  5. for (index1 = 2; index1 <= n; index1++) {
  6. if (isPrime[index1] == true) {
  7. for (index2 = index1 * 2; index2 <= n; index2 += index1) {
  8. isPrime[index2] = false;
  9. }
  10. }
  11. }
  12. }
The principle is simple, and uses the following steps: 1) 0, 1 are not primes, 2 is a prime 2) For each prime number up to n (we tested the value to be TRUE), we do the following: the prime number's multiples are not primes, so we set those to FALSE. As you can foresee, the result will be the array with TRUE for primes, FALSE for non-primes. For your amazement, we have created a test application, which reads numbers for prime testing, until 0 is entered. We simply ignore negative numbers in this test! The full source code for main.cpp is here:
  1. #include <iostream>
  2.  
  3. #define N 1000001
  4.  
  5. using namespace std;
  6.  
  7. bool isPrime[N];
  8.  
  9. void init(int n) {
  10. int i;
  11. for (i = 0; i <= n; i++) {
  12. isPrime[i] = true;
  13. }
  14. }
  15.  
  16. void FindPrimesTo(int n) {
  17. isPrime[0] = isPrime[1] = false;
  18. isPrime[2] = true;
  19. int index1, index2;
  20. for (index1 = 2; index1 <= n; index1++) {
  21. if (isPrime[index1] == true) {
  22. for (index2 = index1 * 2; index2 <= n; index2 += index1) {
  23. isPrime[index2] = false;
  24. }
  25. }
  26. }
  27. }
  28.  
  29. int main(int argc, char** argv) {
  30. init(N - 1);
  31. FindPrimesTo(N - 1);
  32.  
  33. cout << " PRIME TEST APPLICATION" << endl;
  34. cout << " ----------------------" << endl;
  35. cout << " Enter 0 to exit!" << endl << endl;
  36. int option = 1, nr;
  37. while (option != 0) {
  38. nr = -1;
  39. while ((nr < 0) || (nr > 1000000)) {
  40. cout << " nr = ";
  41. cin >> nr;
  42. }
  43. option = nr;
  44. cout << " ";
  45. if (isPrime[nr] == true) {
  46. cout << nr << " is a prime number!";
  47. } else {
  48. cout << nr << " is not a prime number!";
  49. }
  50. cout << endl;
  51. }
  52. cout << " Done! Now exiting ..." << endl;
  53. return 0;
  54. }
One of the outputs during our tests was this (and it is accurate - you can verify):
  1. PRIME TEST APPLICATION
  2. ----------------------
  3. Enter 0 to exit!
  4.  
  5. nr = 15
  6. 15 is not a prime number!
  7. nr = 7
  8. 7 is a prime number!
  9. nr = 100103
  10. 100103 is a prime number!
  11. nr = 1000000
  12. 1000000 is not a prime number!
  13. nr = -25
  14. nr = 66
  15. 66 is not a prime number!
  16. nr = 67
  17. 67 is a prime number!
  18. nr = 0
  19. 0 is not a prime number!
  20. Done! Now exiting ...
Enjoy and test any number of primes you wish!

Tags

Comments

Submitted byConstantine Adraktas (not verified)on Sat, 08/09/2014 - 01:11

SEE A BREAKTHROUGH ON THE PRIME NUMBERS https://www.academia.edu/7415061/Genesis_13_-_The_statements_by_Euler_and_Zagier_on_the_Prime_Numbers_are_wrong_-_The_Primes_DO_HAVE_a_Military_Precision_Order_of_Sequence_-_By_Constantine_Adraktas_Alma_Mater_MIT

Add new comment