Click here to Skip to main content
11,648,785 members (76,762 online)
Click here to Skip to main content

Tagged as

Quicker Prime Number Test

, 11 Feb 2012 CPOL 26.5K 109 7
Rate this:
Please Sign up or sign in to vote.
Prime Number Testing


I've tried to harness C's speed and an efficient algorithm for prime number testing.


What you need to know is 2 facts.

  • If the number can be factored into two numbers, at least one of them should be less than or equal to its square root.
  • Every prime number can be represented by the form 6k+1 or 6k-1.


The two facts I just mentioned are converted to code here.

#include <stdio.h>
#include <math.h>
  unsigned long int testno;
  unsigned long int i=0;
  unsigned long int testsqrt;
  printf("Enter number for checking : \n");
        printf("\nNot Prime");
    printf("\nNot Prime");

First, it checks if the number is 2 or 3 and if it is one of them, it prints that the number is prime.

The line:


filters out a majority of the non-prime numbers.

First, it tests if the number can be represented by the form 6k+1 or 6k-1 and then if it is a multiple of 2 or 3.

If it can be represented in either of the forms(6k+1 or 6k-1), then it proceeds to the checking loop.

If the condition fails, the program exits after printing "Not Prime".

Then, testsqrt stores the square root of testno(the number the user inputs).

The loop counter i starts from 5 (2 and 3 have been already checked. Every multiple of 4 is divisible by 2 which has already been checked too).

Then, the counter is checked if it is even or if it is a multiple of 3. If it is either, then the program proceeds to the next iteration.

Then, the program checks if testno is a multiple of the current number(counter).

If it is, then the program prints "Not Prime".

If no number divides testno, finally, the program prints that the number is prime and exits.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anshul R
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy 5 Pin
Manish K. Agarwal2-May-13 22:49
memberManish K. Agarwal2-May-13 22:49 
QuestionValidity of formula Pin
LWessels21-Aug-12 4:59
memberLWessels21-Aug-12 4:59 
AnswerRe: Validity of formula Pin
pranav9525-Sep-12 3:24
memberpranav9525-Sep-12 3:24 
GeneralMy vote of 5 Pin
pasztorpisti12-Aug-12 22:59
memberpasztorpisti12-Aug-12 22:59 
QuestionGood One Pin
Chandrasekharan P7-Mar-12 22:11
memberChandrasekharan P7-Mar-12 22:11 
Generalawesome... Pin
kr_harsha16-Feb-12 4:53
memberkr_harsha16-Feb-12 4:53 
GeneralReason for my vote of 5 I did not knew the 6k 1 or 6k-1 rule... Pin
frobertrti23-Jan-12 14:01
memberfrobertrti23-Jan-12 14:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.4 | Last Updated 11 Feb 2012
Article Copyright 2010 by Anshul R
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid