?
Solved

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

Posted on 2003-02-25
3
Medium Priority
?
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress

801 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