English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A pair of (positive) integer numbers are called twin primes if they are both prime numbers and the difference between them is 2, i.e. they are consecutive odd numbers and they are prime numbers. (3, 5), (5, 7) and (11, 13) are three examples of such pair of twin prime numbers.

Write a program to display all the pairs of twin prime numbers which are less than 100.

Caution: You need to write a program to find and list all the twin primes. Your program should be easily extended to other ranges.

2007-12-26 12:10:41 · 2 answers · asked by Brandon P 1 in Computers & Internet Programming & Design

2 answers

Step one: write a function isPrime that accepts a single integer and returns true if the argument is prime, false otherwise. In C-ish pseudocode...

boolean isPrime(integer p){
  boolean ans = true;
  for(integer i = 2; i < p; ++i){
   if (p%i == 0){
    ans = false;
    break;
   }
  }
  return ans;
}

Step two: write a program that uses it:

integer lower = 3;
integer upper = 97;
for(integer i = lower; i <= upper; i+=2){
  if (isPrime(i) && isPrime(i+2)){
   output i, i+2
  }
}

2007-12-26 12:18:29 · answer #1 · answered by jgoulden 7 · 0 0

Not anymore, at one time C++ was just an extension of C. Since then, C has changed, and so has C++. However that being said it's not unusual for the same program to compile both C and C++. The method is the same, but there are just some differences int he libraries that you need the compiler to link to.

2016-05-26 11:22:15 · answer #2 · answered by ? 3 · 0 0

fedest.com, questions and answers