Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Prime and Twin Prime Numbers in C...anybody?

Posted on 2003-02-25
3
Medium Priority
?
462 Views
Last Modified: 2009-12-16
Hi,

     I'm trying to write a program that calculates all of the pairs of numbers ranging from 1 through 400 where the first number is any prime number, and the second is its twin prime, which is equal to the 2X the first number +1, then prints each pair.

     I'm able to get programs to run that calculate and print all prime numbers in a range just fine, but this just doesn't seem to be working.  Does anybody know how this might be done?

Thanks!
   
    J.Pryce.  
0
Comment
Question by:JohnPryce
3 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8015469
show ur code /attempt .. then we can be more precise
0
 
LVL 1

Accepted Solution

by:
SimesA earned 200 total points
ID: 8016374
This has been written here and so probably won't actually compile, but it should be easy enough to fix it. (I'm assuming you already have a test for "primeness" since you can print primes within a range)

// not sure whether you want both numbers to be less
// than 400, it you do, change 400 to 200
for (int i=0; i<400; i++) {
  if (IsPrime(i) && IsPrime(2 * i + 1)) {
    // i and 2i+1 are twin primes
  }
}
0
 

Author Comment

by:JohnPryce
ID: 8046726
Thanks, SimesA.  I'm rewarding you with 50 pts.  That was very helpful.  Sorry for the late response, but I've been out of town the past few days.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question