Prime Number!! Emergency

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Budiman
    New Member
    • Nov 2006
    • 21

    Prime Number!! Emergency

    I need help from you all to solve my assignment. I want to display only prime number start from 1 to 100.I hope you guys solve this post fast, its emergency.
    Thanks for your help
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Budiman
    I need help from you all to solve my assignment. I want to display only prime number start from 1 to 100.I hope you guys solve this post fast, its emergency.
    Thanks for your help
    If you are not willing to get help on doing it yourself, then search this forum and the java forum. It has been done many times in those locations

    Comment

    • ashishjigar
      New Member
      • Nov 2006
      • 3

      #3
      Originally posted by Budiman
      I need help from you all to solve my assignment. I want to display only prime number start from 1 to 100.I hope you guys solve this post fast, its emergency.
      Thanks for your help
      i will help u out

      #include<stdio. h>
      #include<conio. h>
      main()
      {
      int i,p=0,j;
      for(i=1;i<=100; i++)
      for(j=1;j<=i;j+ +)
      {
      if(i%j==0)
      p++;
      }
      if(p==2)
      printf("%d",i);
      p=0;
      }
      getch();
      }

      Comment

      • backpacking
        New Member
        • Nov 2006
        • 2

        #4
        Originally posted by ashishjigar
        i will help u out
        for(i=1;i<=100; i++)
        for(j=1;j<=i;j+ +)
        {
        if(i%j==0)
        p++;
        }
        if(p==2)
        printf("%d",i);
        p=0;
        }
        this code definitely works. a prime number is one which only has two divisors - 1 and itself. when p==2 which is the minimum that p will equal, then i is a prime number.

        to speed up the code though you may want your second "for" statement to be
        Code:
        for(j=2;j<i;j++)...
        
        if(p==0)...
        this only gets rid of a couple of rounds. if you really want to start saving some time, you need to set a dynamic limit to how high j can go. this is because you will find all divisors of a number by the time that you reach the square root. for example. numbers that divide 100 are:

        1,2,4,5,10,20,2 5,50,100

        or in their pairs they are:

        1*100
        2*50
        4*25
        5*20
        10*10

        notice that by the time that you divide 100 by 10 you have found every divisor.
        this means that instead of 100 divides to find a prime you only need 10 or 9 because we will start at 2.

        you can actually save even more time, but its really not worth it unless you are looking for a lot of prime numbers. i have only one other post on here and it is in answer to this same question. i implemented a vector to keep track of the prime numbers as it found them and then only used those numbers to divide and find other primes - after all why divide a number by 6 when you have already divided it by 2 and 3. if it is not divisable by 2 or 3 then it is not by 6. implementing this method would reduce the amount of divides for the number 97(which is prime) down to about 4. it only need be divided by 2,3,5,7. the numbers 8,9,10 are all themselves divisable by these 4 numbers and therefore don't need to be used, and no need to go above 10 because it is about the square root. you can look at the code at prime number code

        i hope this makes sense

        Comment

        • Budiman
          New Member
          • Nov 2006
          • 21

          #5
          Thx for all your help.

          Comment

          • palani12kumar
            New Member
            • Oct 2006
            • 60

            #6
            In this problem, there's no need to check whether the number is divisible by all number upto the given number. that is to check whether n is prime number or not, its enough to check whether it is divisible by a number between 2 to n/2. This will speed up the program a lot;

            void main()
            {
            int i,j,n,b=1;
            printf("\n Enter the range:");
            scanf("%d",&n);
            for(i=2;i<=n;i+ +)
            {
            for(j=2;j<i/2;j++)
            {
            if(i%j==0)
            {
            b+=1;
            }
            }
            if(b)
            printf("\n %d",i);
            }
            getch();
            }

            Comment

            • macklin01
              New Member
              • Aug 2005
              • 145

              #7
              Originally posted by palani12kumar
              In this problem, there's no need to check whether the number is divisible by all number upto the given number. that is to check whether n is prime number or not, its enough to check whether it is divisible by a number between 2 to n/2. This will speed up the program a lot;
              Actually, it's sufficient to only check known primes between 2 and sqrt(n). If you're generating a list of all prime numbers between 2 and N, starting at 2, then to check any given number n <= N, you only have to check for prime divisors up to an including sqrt(n), which you will have recorded in a list of known prime numbers.

              Better still, just use the Sieve of Erastothenes algorithm. (Google). It's pretty much the fastest technique to determine all the primes in a range [2,N].

              It's too bad the original poster wasn't willing to do some of the work on this, and that others chose to do it for him. Prime number testing is a fun and rich topic, and always relevant to things like encryption. Many programmers cut their teeth on writing prime number-related programs. Very good logic exercises, and lots of fun. -- Paul

              Comment

              • rpapaiof
                New Member
                • Oct 2006
                • 27

                #8
                #include <stdio.h>


                int main(void){
                int j,Lim,i,flag;
                printf("Please enter a limit number : ");
                scanf("%d",&Lim );

                for(i=2;i<=Lim; i++){
                for(j=2;j<=i-1;j++){
                if(i%j==0){
                flag=0;
                break;}
                flag=1;
                }
                if(flag==1)
                printf("%5d\t", i, "%5d",i);
                }

                return 0;
                }

                Comment

                Working...