
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a number is an Unusual Number or not in C++
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
#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
- Related Articles
- Check if a number is an Achilles number or not in C++
- Check if a number is an Achilles number or not in Python
- Check if a number is a Krishnamurthy Number or not in C++
- Check if a number is jumbled or not in C++
- Check if a given number is sparse or not in C++
- Check if a number is Quartan Prime or not in C++
- Check if a number is Primorial Prime or not in C++
- Check if a number is a Pythagorean Prime or not in C++
- Check if a number is in given base or not in C++
- Check if a number is divisible by 23 or not in C++
- Check if a number is divisible by 41 or not in C++
- Check if a number is power of 8 or not in C++
- C# Program to check if a number is prime or not
- Check Whether a Number is an Autobiographical Number or Not in JAVA
- Check if given number is Emirp Number or not in Python

Advertisements