ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
heiko@hko:~/dummy$ ./isprime 2
2 is NOT a prime number.
heiko@hko:~/dummy$ ./isprime 3
3 is NOT a prime number.
heiko@hko:~/dummy$ ./isprime 7907
7907 is a prime number. /* which is correct */
...using this code:
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int isprime(int n)
{
int i = 1;
if(!(n%2)) return 0;
while(((i+=2) <= sqrt(n))&&(n%i));
return (n%i);
}
int main(int argc, char *argv[])
{
if (argc == 2) {
if (isprime(atoi(argv[1]))) {
printf("%s is a prime number.\n", argv[1]);
} else {
printf("%s is NOT a prime number.\n", argv[1]);
}
} else {
fprintf(stderr, "Use: %s <number>\n", argv[0]);
}
return 0;
}
Just include a special case that checks to see if the number given is 2, and if so automatically output that it is a prime without going through all the other stuff.
Table driven math functions are a LOT faster, in this case 20 times faster. Assuming you prime function is used a lot and the numbers are sometimes bigger than 2000 - 3000 I did a short test.
The"long code" refers to the table driven long version.
Short code is your version. This is a test on the same random number set, run two times:
kcsdev:/home/jmcnama> time ./test 1
choice:1
Processing using short code... Done
real 0m6.88s
user 0m6.79s <--------------------------
sys 0m0.01s
kcsdev:/home/jmcnama> time ./test 0
choice:0
Processing using long code... Done
real 0m0.39s
user 0m0.38s <--------------------------
sys 0m0.02s
kcsdev:/home/jmcnama>
kcsdev:/home/jmcnama> ./test 1
choice:1
Processing using short code... Done
kcsdev:/home/jmcnama> time ./test 0
choice:0
Processing using long code... Done
real 0m0.40s
user 0m0.38s <--------------------------
sys 0m0.01s
kcsdev:/home/jmcnama> time ./test 1
choice:1
Processing using short code... Done
real 0m6.79s
user 0m6.76s <--------------------------
sys 0m0.02s
kcsdev:/home/jmcnama>
The arrows point to the cpu usage in user mode, most of the real time the code used up doing the calculations...
And this is the test code driver, with your function added.
Not that I changed datatype to long, since the "long" algorithm was set to use a long datatype. On my system Long and int are the same thing, except that the compiler doesn't know that.
Code:
/* test.c */
#include <stdlib.h>
#include <stdio.h>
#include "values.h"
extern isprime(long);
int isprime1(long);
int main(int argc, char *argv[]){
int i,j;
int choice;
choice=atoi(argv[1]);
printf("choice:%d\n",choice);
if (choice!=0) {/*plan B */
printf("Processing using short code... ");
for(i=0;i<SIZE;i++) j=isprime1(values[i]);
}
if(choice==0){
printf("Processing using long code... ");
for(i=0;i<SIZE;i++) j=isprime(values[i]);
}
printf("Done\n");
return 0;
}
/* your function renamed to isprime1 */
/* I made the dataypes long to match the algorithm I used.
on 32 bit systems, C usually makes long and int the same thing */
int
isprime1(long n)
{
long i = 1;
if ((n>1)&&(n<4)) return 1;
if(!(n%2)) return 0;
while(((i+=2) <= sqrt(n))&&(n%i));
return (n%i);
}
But my question is... What happens to bigger numbers?
you need to create the table for biggest numbers, if needed... dont?
Primes will always be primes, that's right... but i dont know ... limitations on the size of the prime number? with the table, you'll have limitatios always... am i right?
I'm asking.. maybe you have more experiece than me. this conversation has become very interesant
a year or so ago three indian mathmaticians formulated a proof of primality
this is the only absoulte proof that i know of...
i wouldn't think it as fast as a table based system but certainly faster than
a brute force method and its claimed to be certain of correctness for any number...
I threw that stuff together - it has a bug or two.
Our production code uses a table that handles all positve long values - the first 4797 primes - up to 2.7 billion.
It can find 100000 primes in 8 seconds on our HP multiuser box.
We use it in production because we evaluate primality of numbers about 300,000 times an hour.
bsearch is okay for small primes, but the best application is to use the pointer stomp thru the prime list.
The reason is that 98%+ of all integers are factored by the first 201 prime numbers. Therefore the search returns on average after about 100 iterations for really large numbers. Using a midpoint break based on sqrt speeds up exit on medium sized primes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.