Flux rss

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

Published by deri58, last update on Sunday November 23, 2008 04:29:23 AM 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;
}

Prime numbers Hello object oriented programmers, I wanna a program that return a list of prime numbers up to 100. The point is you are not allowed to use arithmetic operations such as add,subtract,divide and multiply!! You can use only "++" for increasing the... en.kioskea.net/forum/affich-37145-prime-numbers
Viruses - Hoaxes A hoax is an email which disseminates misinformation and encourages the recipient to send this false report to his or her friends, relatives, or colleagues. More and more people are forwarding information received by email without checking whether or... en.kioskea.net/virus/hoax.php3
Access problem Hello, Hi i would like to access to my database source i am getting each time a message error can someone check whether i have made a mistake somewhere JAR files has already been added Construct an access object. */ public access () { try { Context... en.kioskea.net/forum/affich-13120-access-problem
[C language]Handling 64-bit integers[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... en.kioskea.net/faq/sujet-978-c-language-handling-64-bit-integers
Converting a 32-bit integer into IPConverting 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... en.kioskea.net/faq/sujet-945-converting-a-32-bit-integer-into-ip
How to detect the number of packages free/non-free installedHow to detect the number of packages free/non-free installed Under Debian –based system, with the help of a tool GNU, the tools VRMS is an application program that reports all of the packages whether it is free is non free which is... en.kioskea.net/faq/sujet-850-how-to-detect-the-number-of-packages-free-non-free-installed
Confirmation whether my computer is infectedHI There is something very strange happening on my computer , it works perflectly till i am not connected to internet. But when i try to get online and while opening IE the application gets close - i am not even able to check my mail. It is quite... en.kioskea.net/forum/affich-17693-confirmation-whether-my-computer-is-infected
Check series of numbers in a given data baseHello, I want t check the presence of several numbers ( example pages numbers) from a list of numbers given as a data base. How can i find the list contains these numbers . en.kioskea.net/forum/affich-43796-check-series-of-numbers-in-a-given-data-base
Convert value in the cell into integerHello, I am working on creating a macro. I want to go through the sheet and take the quantity that enter in the cell and copy and insert the row as many times as the value that enter in the cell. How do I convert the number in the cell into integer so... en.kioskea.net/forum/affich-17925-convert-value-in-the-cell-into-integer
Download MP3-CheckFormat MP3 is audio format par excellence for audio compression. But it is really possible that there are errors in formats, notably graffiti there. MP3-Check is an application which allows to prove your music collection for quality and possible... en.kioskea.net/telecharger/telecharger-696-mp3-check
Download Primitive Disk IndexerTo stock and/or to safeguard data on his hard disk or of detachable disks is good means to lose nothing. But when they pile up, it is rather difficult to meet there. Primitive Disk Indexer is a program allowing to safeguard the structure of your... en.kioskea.net/telecharger/telecharger-626-primitive-disk-indexer
Download SFV CheckerSFV Checker is an integrity controller which ensures that your multi-volumes file archives are complete and up to date. SFV Checker uses the CRC-32 technology to verify every file and notifies you if one or several files are corrupted, possess an... en.kioskea.net/telecharger/telecharger-1659-sfv-checker
Bangladesh makes Internet use free for primary schoolsThis file photo shows a Bangladeshi saleswoman (R) talking with children at a computer fair in Dhaka. Bangladesh on Tuesday made the use of broadband Internet services free for nearly 40,000 state-run primary schools to boost the number of users in... en.kioskea.net/actualites/bangladesh-makes-internet-use-free-for-primary-schools-10450-actualite.php3
China set to launch record number of spacecraft in 2008: reportA man talks on a mobile phone next to a billboard showing Chinese made Chang'e I, the country's first lunar orbiter, in Beijing, in November 2007. China will launch a record number of spacecraft this year, state media reported Tuesday, amid... en.kioskea.net/actualites/china-set-to-launch-record-number-of-spacecraft-in-2008-report-10126-actualite.php3
Representation of real numbers and integers 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 mathematical number can be infinite (as... en.kioskea.net/base/representation.php3
Spreadsheets - Mathematical Functions Function Description ABS() This function returns the absolute value of a number. It therefore returns the number, if it is positive, or the opposite (positive) if it is negative ODD(value) Rounds a number up to the nearest odd integer MOD(value1;... en.kioskea.net/tableur/tabmath.php3
Email - Using email An email client, a software program for writing, checking and sending email, is generally made up of a number of windows. The main windows of this software are: Incoming, In, Inbox: This represents the main mailbox for receiving email, Sent, Outbox,... en.kioskea.net/courrier-electronique/usages-messagerie.php3