Thread: prime number prog. need help

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    6

    prime number prog. need help

    heres the question

    You can use an array of bit-field structures as the problem states, or just a simple array of int instead. Either way, in the loop which will go i=2..31999, use div() from stdlib.h to produce the array index and bit number, e.g. r = div(i,32); then r.quot is the array index and r.rem is the bit position (0...31).
    heres my code

    PHP Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    int p[1000]; 
    int main(void

    int i,j
    int m,k
    div_t d
    for (
    =2i<300i++) 

        
    divi300); 
        
    d.rem
        
    d.quot
         
        if( (
    p[k] & (1<<m)) != 0) continue; 
        for( 
    2*i300+= i); p[k] |= ( 1<<); 
         
      } 
      for( 
    2300; ++i
      { 
        if( (
    p[k] && (1<<i)) == 0printf"%d\n"i); 
      } 
      return 
    0

    it compiles but its not printing anything?
    anyone know why?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    if( (p[k] && (1<<i)) == 0) printf( "%d\n", i);
    Should that be a bitwise AND instead of a logical AND?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    wow thanks.. that did it!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > (1<<i)
    This isn't going to do anything meaningful for values of i past 31

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Why Errors in Prime Number Loop
    By wco5002 in forum C++ Programming
    Replies: 15
    Last Post: 03-22-2008, 10:49 PM
  3. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 08:03 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. Prime Number?
    By cprogramnewbie in forum C Programming
    Replies: 8
    Last Post: 03-16-2002, 12:18 AM