+ 2

I need a code for twin prime

21st Nov 2016, 2:46 PM
ROSHNI SHRESTHA
ROSHNI SHRESTHA - avatar
2 Respostas
+ 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; }
29th Nov 2016, 7:38 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 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; }
29th Nov 2016, 8:41 AM
Abhishek Kumar
Abhishek Kumar - avatar