Thread: how do i calculate prime number

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by pimp View Post
    there is a part that i don't understand can explain more?

    }while(--top > 3);

    //print up the arrray of primes
    for(i = 0; i < numberOfPrimes; i++)
    print array of primes[i];


    system("pause");
    return 0;


    }
    The variable top needs to be decremented by 1, every time the program loops through the outer do while loop.

    I had it stop at 4, because I already know lower (positive integers) are prime. If you want 2 and 3 in your prime number array, just change the 3 to 1.

    The print out of the prime numbers array should be straight forward to understand, yes?

    You'll need to add the code to "sum" up the array numbers, whatever you're supposed to do with that.

  2. #32
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i show u the code that i have edit.it about the same as yours..u try and run it..

    Code:
    #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int arrayofprimes[100];// create space for array
         int top,remainder,numberofprimes,value,prime,i;  
         int minusONE;//pointer for array
    
    
     
    //Has to be moved out of the two nested loops for finding primes.
       printf("pls enter top limit:");
       scanf("%d",&top);
    
       prime = 1; 
       numberofprimes = 0;
    
    
       do
       {
           for(minusONE = (top / 2); minusONE > 1; minusONE--) //Top / 2 is not the best, sqrt top is, but it's way better than top - 1
           {
                remainder = top % minusONE;
                printf("\n%d / %d remainder is %d", top, minusONE, remainder);
                if(remainder==0)  //it's not a prime number, so stop processing it.
                {
                    prime = 0;
                    break;  //break out of the for loop.
                }
            } //end of for 
            // if prime == 1 at this point, you have a prime number. Add this if statement, and put the prime number, into the array.
           
            if(remainder==1) 
            {
                arrayofprimes[numberofprimes++] = top;
                }
                
                                       
            
           }while(--top > 3);
         
       //print up the arrray of primes
       for(i = 0; i < numberofprimes; i++)//why did u declare i ? what is the function of i ?
       {
          printf("array[%02d]",arrayofprimes[i]);
          }
    
    
         system("pause");
         return 0;
       
         
     }
    i'm really a C program idiot.

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i manage to get prime number.. even though i change the 3 to 1 i still can't count number smaller than 4..

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The variable i is used by convention, as a loop counter, by programmers.

    I can't try the code, Pimp. My computer is involved in a very complex protein modeling simulation in Linux, and my compilers are all in Windows.

    You're not going to want the word "array[] printed out before every single prime, are you?

    Just print one line like "This is the primes that were found: " one time, before the for loop to print out all the actual prime numbers.

    Good idea to make them all line up, I like that!

  5. #35
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    adak i show u the code that i was expected to do so u will have a idea about my project.which i can actually ask u for more help(=

    summation of prime number
    ===================
    please enter top limit:10

    the prime number in primearray[00] is 7
    the prime number in primearray[01] is 5
    the prime number in primearray[02] is 3
    the prime number in primearray[03] is 2
    the prime number in primearray[04] is 1

    the sum of all prime number within the top limit is:
    1+2+3+5+7=18

    this is the actual output that i need to summit.
    another question.. will i be able to change the i variable to other variable?

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Replace this:
    Code:
           for(minusONE = (top / 2); minusONE > 1; minusONE--) //Top / 2 is not the best, sqrt top is, but it's way better than top - 1
    with this:
    Code:
      if(top > 5) 
         minusONE = top / 2;
      else
         minusONE = top - 1;
    
    
           for(; minusONE > 1; minusONE--) //note the two semi-colons!
     
    
    and add
    prime = 1;
    
    just before the while(--top > 1) line.
    Edit: Note that there is no primearray[00], primearray[01], etc. There is just primearray[0], primearray[1], etc.

    You can call the i variable anything you want, but get used to using it and seeing it - everybody uses it. It's as much a part of C
    (and other programming languages), as logic, itself. If you needed a second loop variable of no particular name, j would be used,
    as well.
    Last edited by Adak; 01-12-2008 at 07:45 AM.

  7. #37
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    can't i use &#37;02d to output a double digit out ? just a question.. i go try the code out and update .

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, but you can't use a format number for printing, as an index number in an array!

  9. #39
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i'm back. i try the code and it is able to give me prime number..however it was at the opp side

    array[07] is 1
    array[05] is 2
    array[03] is 3
    array[02] is 4
    array[01] is 5 // this is the code that i printe out

    but the code that i need to print is

    array[00] is 7
    array[01] is 5
    array[02] is 3
    array[03] is 2
    array[04] is 1

    sorry for asking so much. i'm really sux in C program

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Replace: If(remainder == 1) with: if(prime == 1).

    We don't care WHAT the remainder is. Also, the variable "value" is never used and can be deleted.

  11. #41
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    can't work they print out the same output

  12. #42
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please post up the whole current program, so I can see just where we're at.


    Please replace this:
    printf("array[&#37;02d]",arrayofprimes[i]);

    with this:
    printf("\n array[%2d]: %2d", i, arrayofprimes[i]);
    Last edited by Adak; 01-12-2008 at 08:20 AM.

  13. #43
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Code:
    #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int arrayofprimes[100];// create space for array
         int top,remainder,numberofprimes,prime,i;  
         int minusONE;//pointer for array
    
    
     
    //Has to be moved out of the two nested loops for finding primes.
       printf("pls enter top limit:");
       scanf("%d",&top);
    
       prime = 1; 
       numberofprimes = 0;
    
    
       do
       {
          if(top > 5) 
          {
          minusONE = top / 2;
          }
          else
         { 
           minusONE = top - 1;
         }
    
    
           for(minusONE; minusONE > 1; minusONE--) //note the two semi-colons!
     
    
    
    
    
    
          // for(minusONE = (top / 2); minusONE > 1; minusONE--) //Top / 2 is not the best, sqrt top is, but it's way better than top - 1
           {
                remainder = top % minusONE;
                printf("\n%d / %d remainder is %d", top, minusONE, remainder);
                if(remainder==0)  //it's not a prime number, so stop processing it.
                {
                    prime = 0;
                    break;  //break out of the for loop.
                }
            } //end of for 
            // if prime == 1 at this point, you have a prime number. Add this if statement, and put the prime number, into the array.
           
            if(prime==1) 
            {
                arrayofprimes[numberofprimes++] = top;
                }
                
                                       
            prime=1;
           }while(top-- >1);
         
       //print up the arrray of primes
       for(i = 0; i < numberofprimes; i++)//why did u declare i ? what is the function of i ?
       {
          printf("\narray[%02d]is %d ",arrayofprimes[i],i);
          }
          printf("\n");
    
    
         system("pause");
         return 0;
       
         
     }

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Swap positions of i and arrayofprimes[i], in the next to last print statement.

    like this: printf("\n array[&#37;2d]: %2d", i, arrayofprimes[i]);

    How's the printout?
    Last edited by Adak; 01-12-2008 at 08:26 AM.

  15. #45
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    the print out is the same .. however it still print out the remainder which i don want

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help, trying to find prime number
    By vampirekid.13 in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2009, 03:29 PM
  2. Prime number algorithm
    By Akkernight in forum C Programming
    Replies: 9
    Last Post: 04-19-2009, 01:50 PM
  3. how to calculate billion number
    By mrmamon in forum C++ Programming
    Replies: 9
    Last Post: 10-16-2005, 06:34 AM
  4. prime number finder.
    By Aalmaron in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2004, 04:56 PM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM