how to print first 30 twin primes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • confusedcmsc
    New Member
    • May 2010
    • 1

    how to print first 30 twin primes?

    hey!

    there's something i would like to ask you guys...

    i am having a problem on how to print the first 30 twin primes...

    for those who don't know what twin primes are...those are pair of prime numbers that have a difference of two...

    i am aiming for the result of:
    (3,5)
    (5,7)
    (11,13)
    (17,19) and so on...

    but i dont have any idea on how to make a program of it...

    there was someone who helped me make it but we only managed to print pairs of odd numbers upto 97...

    i would really appreciate your help...
    thanks
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Calculate 1st prime
    Calculate 2nd prime

    See the difference

    Calculate 3rd prime

    See the difference between 2nd prime and 3rd prime. And so on.

    Regards
    Dheeraj Joshi

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      I suggest first writing an isPrime(i) function, that tests whether the number i is prime.

      ===
      After 3 and 5, all (twin) primes have the form 6n-1, 6n+1. (Use the sieve of erasothenes to figure out why.)

      Pseudo:
      Code:
      Print out (3, 5)
      for (n = 1; there are less than 30 twin primes so far; n++)
        Is 6n-1 prime and is 6n+1 prime?
          Print out 6n-1 and 6n+1
      Your isPrime function need not check for 2 and 3 as divisors.

      ====
      Aside for better speed: (IGNORE IF THIS CONFUSES YOU).
      If you want to improve the searching further after (5,7), multiply by 5:
      30n-1, 30n+1
      30n+5, 30n+7 X
      30n+11, 30n+13
      30n+17, 30n+19
      30n+23, 30n+25 X

      So all twin primes after (5,7) are in one of the 3 forms:
      30n-1, 30n+1
      30n+11, 30n+13
      30n+17, 30n+19

      Then you isPrime function need not check for 2,3, or 5 as divisors.

      Comment

      Working...