Link to home
Start Free TrialLog in
Avatar of kimos123
kimos123

asked on

Prime Numbers

hi there,
im trying to make a prog  that generate the first  100 prime numbers
here is my code:

#include <stdio.h>
int main(void)
{
int x,a,f,p,teller;
a=2;
f=0;
//p=a-1;
  for(x=1 ; x<1000;x++){

//for(p=1; p<a; p++){

     
if ((a%p !=0) &&  f<100){
     
 for(p=1; p<a; p++){

//    for(p=a; p>0; p--) {

// print("Priem
    f++;

    printf("Priemgetal %i: %i \n",f,a );

}    
// p++;
 a++;

}
//p++;
 }

}
----------------------

and its not working   here is how i want to make it i dont know if its good
prime number is    
primenumber % primenumber -1   << and it have to loop intil 1          

anyone can help me with it ?  

other possible way is      
primenumber % (1 to  primenumber -1 )  

plz help me im stuck
thanks in advance
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Easiest way: make an array in which you store the primes found. Check if the current number is divisable by any of them, if not it's another prime. See Eratosthenes' sieve (Zeef van Eratosthenes).
Avatar of kimos123
kimos123

ASKER

i want to make it without area's  it should be very easy   if we have 2 loops
one to loop the numbers and one to loop the % number from 1 to the primenumber   or from the primenumber to 1  

but im stuck with the loops     im not so good in loops
good illustration of the sieve is here: http://www.faust.fr.bw.schule.de/mhb/eratosiv.htm
The array only contains true/false flags, the indexes into the array are the numbers that are prime or not...

other than that, simple way is to test against every odd number between 2 and sqrt( number ), or even better (well... arguable) against all primes between 2 and sqrt( number )...

// dodgy recursive function....
function int isprime( int number ) {
    int i;
    // Special cases...
    if( number <= 2 ) {
        return 0;
    }

    for( i = 3; i <= sqrt( number ); i+=2 ) {
        if( isprime( i ) ) {
            if( number % i == 0 ) {
                return 0;
            }
        }
     }
     return 1;
}
sorry... special cases above are wrong...

code snippet should be:

// Special cases
if( number < 2 )  { return 0; }
if( number == 2 )  { return 1; }
If you start with p==1, a%p will always be zero.
kut.c:3: syntax error before `int'
kut.c: In function `isprime':
kut.c:11: warning: type mismatch in implicit declaration for built-in function `sqrt'

i have this until now and i get error compiling

------------------------------------
#include <stdio.h>

function int isprime( int number ) {
    int i;
    // Special cases...
    if( number < 2 )  { return 0; }
if( number == 2 )  { return 1; }

    for( i = 3; i <= sqrt( number ); i+=2 ) {
        if( isprime( i ) ) {
            if( number % i == 0 ) {
                return 0;
            }
        }
     }
     return 1;
}

int main(void)
{
int x,a,f,p,teller;
a=2;
f=0;

  for(x=1 ; x<1000;x++){

if (isprime(a) &&  f<100){
         f++;

    printf("Primenumber %i: %i \n",f,a );

}    

 a++;

}

}
------------------------
Interesting filename... ;)

Remove the word "function".
lol sjef   yea i been trying it for 2 days now...  i coudnt hold it so i used this name :)

i still got the error    i removed the function  word  and i dont have the first syntax error but i still got:

kut.c: In function `isprime':
kut.c:11: warning: type mismatch in implicit declaration for built-in function `sqrt'
starting with p == 1 is incorrect as sjef_bosman points out... definition of prime is an integer not devisible by any integer between 2 and itself (inclusive). Testing 2, and then every prime upto sqrt(number) is sufficient, although, this is obviously recursive (unless using a sieve as described above). For small numeric sets, simply testing 2 and all odd numbers upto sqrt(number) is sufficient, but how useful are small prime numbers?
The sieve mechanism is simple to implement, even for ranges in the 1000s...

   unsigned char a[SIEVE_SIZE];
    int i,j;
    memset( a, 1, SIEVE_SIZE );

    a[0] = a[1] = 0;

    for( i = 2; i < SIEVE_SIZE; ) {
        for( j = 2*i; j < SIEVE_SIZE; j+= i ) {
            a[j] = 0;
        }
        do {
            i++;
        } while( !a[i] );
    }
    for( i = 0; i != SIEVE_SIZE; i++ ) {
        if( a[i] ) {
            printf( "Prime: %d\n", i );
        }
    }
yup, not sure why I put "function" in there...

also, #include <math.h> to get sqrt().

Also, sqrt() expects double, not int, but the implicit cast should be ok, once the #include is there...
obvious possible overflow bug with my sieve impl. but that can be an exercise (the do { } while() loop...)
cjjclifford thanks alot  it worked  
but is my idea wronge?  about looping and testing the mod (%) ?  

and how can i put a loop in it so it display        
prime 1: firstprimenumber
prime 2: secondprimenumber
prime 3: etc...

and could you please explane what you did exactly becouse i dont understand it.

thanks in advance
Of course the idea is not wrong, it's just very time-consuming: if the value to be tested is 10000, you have to divide 999 times whereas witha sieve you divide max 100 time (i.e. sqrt(10000)), and in practice only some 20 times. So you solved the problem in an O(n*n) algorithm, but it can be done in O(n).
nevermind about the loop i made it
i just want an explanation for  



unsigned char a[SIEVE_SIZE];
    int i,j;
    memset( a, 1, SIEVE_SIZE );

    a[0] = a[1] = 0;

    for( i = 2; i < SIEVE_SIZE; ) {
        for( j = 2*i; j < SIEVE_SIZE; j+= i ) {
            a[j] = 0;
        }

thanks
        do {
            i++;
        } while( !a[i] );
    }
    for( i = 0; i != SIEVE_SIZE; i++ ) {
        if( a[i] ) {
            printf( "Prime: %d\n", i );
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks  cjjclifford  & sjef_bosman for the help.  
im glade experts like you guys are in this forum :)
Next time, try a question with 500 points: they'll be all over you!
you mean it wasn't! oh, darn :-)
That'll teach you to supply the whole solution!

Sjef (LOL)
ironically, the slow, inefficient solution was accepted, rather than the sieve impl....
But that's what he asked for (without "area") :(
Ah say, you were so well on your way, now how could I barge in?

Sjef ;)
an real interesting exercise to write the function with zero variable "area" at all... i.e. without any variables, just the argument to play with... reminds me of my college days, getting LISP practicals to generate poetry without using any variables at all...
I somehow managed to slip through LISP... I'll think about this one, tomorrow. Maybe something for the riddles TA?
indeed maybe... its an interesting idea though, pure functional programming using C, which is traditionally not used for that...