0% found this document useful (0 votes)
15 views1 page

Twin Prime and Position C++

Uploaded by

boonerhett240
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

Twin Prime and Position C++

Uploaded by

boonerhett240
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <vector>
using namespace std;

// Function to check if a number is prime


bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}

// Function to find twin primes


bool isTwinPrime(int a, int b) {
return isPrime(a) && isPrime(b) && (b - a == 2);
}

int main() {
int limit; // Number of twin primes to generate
cout << "Enter the number of twin prime pairs to generate: ";
cin >> limit;

vector<pair<int, int>> twinPrimes; // Vector to store twin prime pairs


int number = 2; // Starting number to check for primes
int position = 1; // Position of twin prime pairs

while (twinPrimes.size() < limit) {


if (isTwinPrime(number, number + 2)) {
twinPrimes.push_back({number, number + 2});
cout << "(" << position << ") " << number << " and " << number + 2;
if (twinPrimes.size() < limit) {
cout << " "; // Add a space between outputs
}
position++;
}
number++;
}

cout << endl; // End the row with a newline


return 0;
}

You might also like