Primality Testing–Part 1. Naïve Approaches
Primality refers to determining if a given input number is prime or not. This post covers two naive approaches for primality testing.
Naive Approach 1
This most straightforward approach is to check if a given input integer n is divisible by integers from 2 to n-1. If a number is prime, then it should not divisible by any numbers in the range [2, n-1]. This is implemented as below,
/**
a naive method of determining if n is prime or not
divide from 2 to n-1
**/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int prime(int n) {
int i;
for (i = 2; i <= n-1; i++) {
if (n%i == 0) {
printf("%d is divisble by %d\n", n, i);
return 0;
}
}
return 1;
}
int main(int argc, char **argv) {
int n = atoi(argv[1]);
struct timeval stTime, edTime;
gettimeofday(&stTime, NULL);
if (prime(n)) {
printf("%d is prime\n", n);
} else {
printf("%d is not 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));
}
Save the code to prim0.c. Compile the code with the command below,
gcc -o prim0 prim0.c –lm
Below are some sample testing results,
./prim0 2147483647
2147483647 is prime
time: 7:145569
./prim0 2147483640
2147483640 is divisble by 2
2147483640 is not prime
time: 0:46
./prim0 2
2 is prime
time: 0:51
./prim0 3
3 is prime
time: 0:49
Naive Approach 2: Improved Version of NA 1
The above algorithm can be improved. Firstly, if a number is divisible by an even integer, it must be divisible by 2. So we only need to check if the input number n is divisible by 2 and skip the rest of the even integers.
Secondly, if a number n is divisible by a number x, where x > sqrt(n). Then n = x*y, where y < sqrt(n), and y is also divisible by n. Therefore, we only need to check up to integer sqrt(n).
Below is the implementation with the two optimizations described,
/**
a naive method of determining if n is prime or not
divide from 2 to sqrt(n), skip the even numbers
**/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int prime(int n) {
int i, j;
if (n == 2) {
return 1;
}
if (!(n&1)) {
//n is even
printf("%d is divisible by 2\n", n);
return 0;
}
j = sqrt(n);
//to be safe, run to j+1
for (i = 3; i <= j + 1; i+=2) {
if (n%i == 0) {
printf("%d is divisible by %d\n", n, i);
return 0;
}
}
return 1;
}
int main(int argc, char **argv) {
int n = atoi(argv[1]);
struct timeval stTime, edTime;
gettimeofday(&stTime, NULL);
if (prime(n)) {
printf("%d is prime\n", n);
} else {
printf("%d is not 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 sqrt is a floating point operation. We check until sqrt(n) + 1, just to be safe.
Save the code to prim1.c, and compile the code using the command below,
gcc -o prim1 prim1.c –lm
Below are some testing results,
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim1 2
2 is prime
time: 0:50
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim1 3
3 is prime
time: 0:60
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim1 2147483647
2147483647 is prime
time: 0:308
roman10@ra-ubuntu-1:~/Desktop/integer/prim$ ./prim1 2147483641
2147483641 is divisible by 2699
2147483641 is not prime
time: 0:73
References:
1. Wikipedia Primality Test: http://en.wikipedia.org/wiki/Primality_test
2. Algorithms by Dasgupta, CH Papadimitriou and UV Vazirani
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 (26)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (5)
- Animation (1)
- 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 (1)
- 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)




