Check whether a number has exactly three distinct factors or not | C ++ program to check whether number has exactly three distinct factors or not ; Utility function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool isThreeDisctFactors ( long long n ) { int sq = ( int ) sqrt ( n ) ; if ( 1LL * sq * sq != n ) return false ; return isPrime ( sq ) ? true : false ; } int main ( ) { long long num = 9 ; if ( isThreeDisctFactors ( num ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; num = 15 ; if ( isThreeDisctFactors ( num ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; num = 12397923568441 ; if ( isThreeDisctFactors ( num ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }