# Check if a number is an Unusual Number or not in C++

C++Server Side ProgrammingProgramming

Here we will see a number is unusual number or not. A number is said to be unusual if the greatest prime factor of the number is strictly greater than square root of the number. Some of the unusual numbers are: 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 20, 21, 22, 23, 26, 28, 29, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 44, 46

To solve this, we will try to find the largest prime factor, then check whether the factor is greater than square root of the number or not. If yes, then the number is unusual number, otherwise not.

## Example

Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int largestPrimeFactor(int num) {
int max_prime = -1;
while (num % 2 == 0) { //remove all 2s from the number
max_prime = 2;
num >>= 1;
}
for (int i = 3; i <= sqrt(num); i += 2) {
while (num % i == 0) {
max_prime = i;
num = num / i;
}
}
if (num > 2)
max_prime = num;
return max_prime;
}
bool isUnusual(int num) {
int largePrimeFactor = largestPrimeFactor(num);
if (largePrimeFactor > sqrt(num)) {
return true;
} else {
return false;
}
}
int main() {
int n = 14;
if (isUnusual(n)) {
cout << n << " is an unusual number";
} else {
cout << n << " is not an unusual number";
}
}

## Output

14 is an unusual number
Published on 22-Oct-2019 11:39:28