Previous posts on primality testing are all trying to factor the input number. For a very large number with few hundreds of bits, it is difficult to compute in that manner.
This post introduce a probabilistic approach based on Fermat’s primality test.
Fermat’s Little Theorem
Fermat’s primality test is based on Fermat’s Little Theorem,
If p is prime, then for every 1 <= a < p,
[ a ^ ( p - 1) ] mod p = 1
Fermat’s Little Theorem suggests a method to test if a number is prime or not. For a big input number p, it is not desirable to test every a. Normally, we randomly pick up an a, and if it satisfy the equation above, we claim it is prime, otherwise, it is not.
However, Fermat’s little theorem is a necessary condition for a number to be prime, but not sufficient. In other words, every prime number satisfy fermat’s little theorem, but it is also possible to find an a for a composite number n where [ a ^ (p - 1) ] mod n = 1.
It can be proven that most values of a will fail the test for a composite number. And if we pick several random a and they all pass the test, the possibility of making a wrong decision is rare. This algorithm cannot guarantee that positive primality test is 100% correct, but it is fast the possibility of wrong is low enough for many practical use cases.
Implementation
Below is a simple C implementation based on the idea above,
#include <stdio.h>
#include <stdlib.h>
unsigned long modexp(int x, int y, int n) {
unsigned long i;
if (y == 0) {
return 1%n;
}
if (y == 1) {
return x%n;
}
i = modexp(x, y/2, n);
if (y&0x01) { //odd
//return (x*i*i)%n;
// printf("%d:%lu:%lu\n", x, i, (x%n*i*i));
return x%n*i%n*i%n;
} else { //even
return i*i%n;
}
}
int main(int argc, char **argv) {
int n;
int a;
int i;
n = atoi(argv[1]);
srand(time(NULL));
struct timeval stTime, edTime;
gettimeofday(&stTime, NULL);
for (i = 0; i < 20 && i < n; ++i) {
a = rand()%100;
if (a == 0) {
a = 1;
}
//printf("a=%d\n", a);
if (modexp(a, n-1, n) != 1) {
printf("%d is not prime, %d\n", n, a);
break;
}
}
if (i == 20 || i == n) {
printf("%d is prime\n", n);
}
gettimeofday(&edTime, NULL);
printf("time: %u:%u\n", (unsigned int)(edTime.tv_sec - stTime.tv_sec), (unsigned int)(edTime.tv_usec - stTime.tv_usec));
}
Note that the code uses the modular exponentiation algorithm. Due to the integer overflow, the largest input the program can accept is 65536 = 2^16.
Save the code to prim5.c and compile it using the command below,
gcc -o prim5 prim5.c
Below are some simpe tests,
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim5 65521
65521 is prime
time: 0:69
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim5 2
2 is prime
time: 0:53
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim5 65536
65536 is not prime, 8
time: 0:53
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim5 63703
63703 is prime
time: 0:69
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim5 61493
61493 is prime
time: 0:67
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim5 13
13 is prime
time: 0:54
References:
1. Wikipedia Primality test: http://en.wikipedia.org/wiki/Primality_test
2. Algorithms
One comment on “Primality Testing — Part 4. Fermat’s Little Theorem and A Probabilistic Approach”
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (27)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (6)
- Animation (2)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- May 2013 (2)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)





sir i want a program to check the primality condition if the given number is even