[C language]Checking whether an integer is a prime number

Last update on November 23, 2008 04:29 AM by deri58
Published by deri58

[C language]Checking whether an integer is a prime number




Definition of a prime number


A prime number is an integer, which is divided only by 1 and itself.

Algorithm 1: dividers between 2 and N-1 will be tested


/**************************\

 *  prime_number1.c     *
\**************************/

/* algorithm : test all the dividers */
#include <stdio.h>


int main (void)
{
  int i, nb, count, test;
  test = count = 0;
  printf ("enter integer: ");
  if (scanf ("%d", &nb) != 1)
    return -1;

  for (i = 2; i < nb; i++, count++)
    if (nb % i == 0)
      test = 1;
  if (!test)
    printf ("%d prime number, number of iterations = %d\n", nb, count);
  else
    printf ("%d is not a prime number, number of iterations = %d\n", nb,count);
  return 0;
}

Algorithm 2: Even dividers will not be tested, research is limited to odd dividers


/**************************\

 *    prime_number2.c   *
\**************************/

/* algorithm : exclude even numbers and

 *    test all the dividers */
#include <stdio.h>


int main (void)
{
  int i, nb, count, test;
  test = count = 0;
  printf ("enter integer: ");
  if (scanf ("%d", &nb) != 1)
    return -1;

  if (nb % 2 == 0)
          test = 1;
  else{
      for (i = 3 ; i < nb; i+=2, count++)
        if (nb % i == 0)
          test = 1;
  }
  if (!test)
          printf ("%d prime number, number of iterations = %d\n",
                          nb, count);
  else
          printf ("%d is not a prime number, number of iterations = %d\n",nb, count);
  return 0;
}


Algorithm 3: All odd dividers up to the square root of N will be tested


/**************************\

 *    prime_number3.c   *
\**************************/

/* algorithm : exclude all even numbers and

 * test all the dividers up to square root */
#include <stdio.h>
#include <math.h>

int main (void)
{
  int i, nb, count, test,limit;
  test = count = 0;
  printf ("enter integer : ");
  if (scanf ("%d", &nb) != 1)
    return -1;
  limit = sqrt(nb) + 1;

  if (nb % 2 == 0)
          test = 1;
  else{
      for (i = 3 ; i < limit; i+=2, count++)
        if (nb % i == 0)
          test = 1;
  }
  if (!test)
          printf ("%d prime number, number of iterations = %d\n", nb, count);
  else
          printf ("%d not a prime number, number of iterations = %d\n",nb, count);
  return 0;
}


Algorithme4: stop the program when a divider is found


/**************************\

 *    prime_number4.c   *
\**************************/

/* algorithm : exclude all even numbers and

 * test all dividers up to the square root
 * exit loop when first divider is found */
#include <stdio.h>
#include <math.h>

int main (void)
{
  int i, nb, count, test,limit;
  test = count = 0;
  printf ("Enter integer: ");
  if (scanf ("%d", &nb) != 1)
    return -1;
  limit = sqrt(nb) + 1;

  if (nb % 2 == 0)
          test = 1;
  else{
      for (i = 3 ; i < limit && ! test; i+=2, count++)
        if (nb % i == 0)
          test = 1;
  }
  if (!test)
          printf ("%d prime number, number of iterations = %d\n", nb, count);
  else
          printf ("%d not prime number, number of iterations = %d\n", nb, count);
  return 0;
}

Best answers for « Checking whether an integer is a prime number » in :
Download Prime Number Spiral Show Description The application is designed by Hermetic Systems. Prime Number Spiral is a tool that is able to display prime numbers in a rectangular grid. Simple and easy to use, the application has been awarded from various popular places. Offering an...
Representation of real numbers and integers Show Representing a number in a computer Representing (or encoding) a number means to express it in binary form. Representing numbers in a computer is necessary in order for it to be able to store and manipulate them. However, the problem is that a...
[C language]Handling 64-bit integers Show[C language]Handling 64-bit integers Unsigned 64-bit integer Example: Signed 64-bit integer Example: Basically in C language, an unsigned number over 32 bit can not exceed the value of 4 294 967 295. It may happen that you are...
[PHP]Last day of the month/Number of days in the month ShowLast day of the month and number of days in a month Using $m as the number of the month i and $y as year. Function date() can display directly the number of days in the month using the "t" character:
Converting a 32-bit integer into IP ShowConverting a 32-bit integer into IP Number to convert: 3265917058 Binary representation 11000010 10101001 11110000 10000010 - 3265917058 00000000 00000000 00000000 11000010 - 3265917058 >> 24 ( 194 ) 11000010 10101001 11110000...
Download Random Number Generator Pro ShowRandom Number Generator Pro is a tool that generates a list of random numbers based on customizable criteria. You can choose the minimum and maximum limits and incrementing numbers. Limits can be positive or negative values. Advantage The...
Download Source Code Spell Checker ShowSource Code Spell Checker is a program that offers to check your lines of code, highlight the errors and to report errors detected. You save valuable time by limiting the reading tedious programming codes. Advantage Via the context menu, you can...
Download A-PDF Number ShowDescription The application is designed by A-PDF.com. Simple and easy to use, the application is completely FREE! A-PDF Number is tool that allows you to add or create the page numbers in PDF files. The application allows you to create documents...
Error checking ShowError checking Binary encoding is very practical for use in electronic devices such as computers, in which information can be encoded based on whether an electrical signal is present or not. However, this electrical signal may suffer disturbances...
Server integrity check ShowIntegrity check When a server has been compromised, the hacker usually covers his/her tracks by deleting all records of his/her activity from the logs. Additionally, he/she installs some tools to enable him/her to create a backdoor, in order to...