Printing Prime numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stone
    New Member
    • Jul 2006
    • 11

    Printing Prime numbers

    Hello.
    I need a simple programme to print prime numbers from 1 to 100.
    I need it urgently.
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Well, if it's simple, I guess you had better stay away from Quadratic Field Sieve or some of the faster methods.

    First test if the number is even, or divisible by two. Second, for all odd numbers from 3 to floor(square root(n)), n being tested for primality, see if it is divisible. For those, use the modulo operation, '%'. It returns the remainder. If (a % b) = 0, then b divides a, a is divisible by b.

    Examples:
    11 % 5 = 4 ( 11 = 2*5 + 1)
    11 % 3 = 2 ( 11 = 3*3 + 2)

    Is 67 prime? sqrt(67) = 8.xx, floor(sqrt(67)) = 8. Test 2, 3, 5, 7
    67 % 2 = 1
    67 % 3 = 1
    67 % 5 = 2
    67 % 7 = 4

    Therefore, 67 is prime since no possible options divide it. Of course, for a larger number, testing whether it's divisble by 9 may be redundant, but it's faster, I think just to check it, rather than check that 9 is prime (recursion, ugly).

    Comment

    • dennischikwayi
      New Member
      • Aug 2006
      • 2

      #3
      Originally posted by D_C
      Well, if it's simple, I guess you had better stay away from Quadratic Field Sieve or some of the faster methods.

      First test if the number is even, or divisible by two. Second, for all odd numbers from 3 to floor(square root(n)), n being tested for primality, see if it is divisible. For those, use the modulo operation, '%'. It returns the remainder. If (a % b) = 0, then b divides a, a is divisible by b.

      Examples:
      11 % 5 = 4 ( 11 = 2*5 + 1)
      11 % 3 = 2 ( 11 = 3*3 + 2)

      Is 67 prime? sqrt(67) = 8.xx, floor(sqrt(67)) = 8. Test 2, 3, 5, 7
      67 % 2 = 1
      67 % 3 = 1
      67 % 5 = 2
      67 % 7 = 4

      Therefore, 67 is prime since no possible options divide it. Of course, for a larger number, testing whether it's divisble by 9 may be redundant, but it's faster, I think just to check it, rather than check that 9 is prime (recursion, ugly).
      Try this code

      for(int pp=2;pp<100;pp+ +) //pp=possible prime
      {
      for(int pd=2;pd<pp;pd++ ) //pd=possible divisor
      {
      if(pp%pd==0)
      break;
      }
      if(pp==pd)
      System.out.prin tln(pp);
      }
      u may declare out of the for loops

      Comment

      • suresh_punniyakkodi
        New Member
        • Aug 2006
        • 20

        #4
        Hi,
        Please try this code,

        int i,j,p;
        for(i=2;i<=100; i++)
        {
        p = 0;
        for(j=2;j<=i/2;j++)
        {
        if(i%j == 0)
        {
        p = 1;
        break;
        }
        }
        if(p == 0)
        System.out.prin tln(i);
        }




        This is exact way.
        ex:- no:40, in this number divisible only between 1 to 20, in any case, not divisibly by 21 to 39. So, you must to check the number is prime in off of that number only.

        Bye...

        Regards,
        Suresh

        Comment

        • kaviarul
          New Member
          • Sep 2006
          • 1

          #5
          how do i put input during execution in prime number progarm

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by kaviarul
            how do i put input during execution in prime number progarm
            What do you want to input?
            Normal methods of input are the Scanner, BufferedReader and swing.JOptionPa ne.inputDialog

            Comment

            Working...