+ 2
I need a code for twin prime
2 Réponses
+ 1
#include <iostream>
using namespace std;
bool is_prime(int num)
{
for(int i = 2; i <= num/2; i++){
if(num % i == 0)
return false;
}
return true;
}
bool is_twin_prime(int prime){
return is_prime(prime + 2);
}
int main() {
int num = 0;
cout << "Enter the num till you want to find twin prime: ";
cin >> num ;
for(int i = 1; i <= num ; i++){
if(is_prime(i) && (i + 2)<= num)
{
if(is_twin_prime(i)){
cout << i << " and " << (i + 2) << " are twin prime "<<endl ;
}
}
}
return 0;
}
+ 1
#include <iostream>
using namespace std;
bool is_prime(int num)
{
for(int i = 2; i <= num/2; i++){
if(num % i == 0)
return false;
}
return true;
}
bool is_twin_prime(int prime){
return is_prime(prime + 2);
}
int main() {
int num = 0;
cout << "Enter the num till you want to find twin prime: ";
cin >> num ;
for(int i = 1; i <= num ; i++){
if(is_prime(i) && (i + 2)<= num)
{
if(is_twin_prime(i)){
cout << i << " and " << (i + 2) << " are twin prime "<<endl ;
}
}
}
return 0;
}