how to print first n primes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janutovianca
    New Member
    • May 2010
    • 2

    how to print first n primes?

    hey there!!!

    we were asked to make a code for printing prime numbers from 3 until the [I]n[I]th prime number but i can't seem to find the right solution for this one.

    when i print my work it prints some other numbers that has the factor of another prime number

    it prints the numbers:
    3, 5, 7, 9 ,11, 13, 15...

    i'm a beginner here, so please help me.
    i really would appreciate your helping me.
    thanks.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Does it print
    Code:
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
    this?

    These are prime numbers.
    Can we have a look at your code?

    Regards
    Dheeraj Joshi

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Well from the look of your output you are just printing all the odd numbers rather than checking if a number is prime before printing it.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        The basic logic of your program is
        Code:
           number_of_primes = 0;
           candidate = 1;
           Repeat until number_of_primes == threshold {
              if candidate is a prime {
                 print the candidate
                 increment number of primes
              }
           }
        The challenge is determining if the candidate is a prime. Forget about computer programs for a moment. Let's talk mathematics first: how can you tell if a number is prime? There is no point writing code until you have a firm grasp on this.

        Comment

        • janutovianca
          New Member
          • May 2010
          • 2

          #5
          actually...i've already got how to print prime numbers but the next problem i'm having now is how to print the first n primes, not the prime numbers from 0 to n.

          i really did appreaciate your replies.

          this is my code:
          Code:
          #include<stdio.h> 
          
          main() 
          { 
          int x,y,n; 
          printf(" Enter n: "); 
          scanf("%d",&n); 
          printf("\n"); 
          
          
          for(x=3;x<=n;x++) 
          {
          for(y=2;y<=x-1;y++) 
          if(x%y==0) 
          break; 
          if(x==y) 
          printf("\t%d\n",x);
          }
          
          
          }

          i really would like your help...thanks

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            In your for loop instead of the end condition being a test against x, the current number you are checking, you need to use a separate variable to keep a count of the number of primes you have found and test against that instead.

            Also your loop at line 13 works but has a lot of scope for optimisation.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Both loops involve comparisons against x. The one Banfa wants you to change is the outermost loop at line 11.

              By the way, "2' is a prime number too.

              Comment

              Working...