Check if a number is an Unusual Number or not

Given a positive integer N. The task is to check if N is an unusual number or not. Print ‘YES’ if M is an unusual number else print ‘NO’.

Unusual number : In Mathematics, an unusual number is a natural number whose greatest prime factor is strictly greater than square root of n.

The first few 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, 47, 51

Examples:

Input : N = 14
Output : YES
Explanation : 7 is largest prime factor of 14
and 7 is strictly greater than square root of 14

Input : N = 16
Output : NO
Explanation : 2 is largest prime factor of 16
and 2 is less than square root of 16 ( i.e 4 ).


Approach :

  1. Find the largest prime factor of the given number N. To find the largest prime factor of N refer this .
  2. Check if the largest prime factor of N is strictly grater than square root of N.
  3. If ‘YES’ then N is an Unusual number otherwise Not.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to check Unusual number 
#include <bits/stdc++.h> 
using namespace std; 
    
// Utility function to find largest 
// prime factor of a number 
int largestPrimeFactor(int n) 
    // Initialize the maximum prime factor 
    // variable with the lowest one 
    int max = -1; 
    
    // Print the number of 2s that divide n 
    while (n % 2 == 0) { 
        max = 2; 
        n >>= 1; // equivalent to n /= 2 
    
    
    // n must be odd at this point, thus skip 
    // the even numbers and iterate only for 
    // odd integers 
    for (int i = 3; i <= sqrt(n); i += 2) { 
        while (n % i == 0) { 
            max = i; 
            n = n / i; 
        
    
    
    // This condition is to handle the case 
    // when n is a prime number greater than 2 
    if (n > 2) 
        max = n; 
    
    return max; 
    
// Function to check Unusual number 
bool checkUnusual(int n) 
    // Get the largest Prime Factor 
    // of the number 
    int factor = largestPrimeFactor(n); 
    
    // Check if largest prime factor 
    // is greater than sqrt(n) 
    if (factor > sqrt(n)) { 
        return true
    
    else
        return false
    
    
// Driver Code 
int main() 
    int n = 14; 
    
    if (checkUnusual(n)) { 
        cout << "YES"
             << "\n"
    
    else
        cout << "NO"
             << "\n"
    
    
    return 0; 
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to check Unusual number
   
class GFG {
   
    // Utility function to find largest
    // prime factor of a number
    static int largestPrimeFactor(int n)
    {
        // Initialize the maximum prime factor
        // variable with the lowest one
        int max = -1;
   
        // Print the number of 2s that divide n
        while (n % 2 == 0) {
            max = 2;
            n >>= 1; // equivalent to n /= 2
        }
   
        // n must be odd at this point, thus skip
        // the even numbers and iterate only for
        // odd integers
        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            while (n % i == 0) {
                max = i;
                n = n / i;
            }
        }
   
        // This condition is to handle the case
        // when n is a prime number greater than 2
        if (n > 2)
            max = n;
   
        return max;
    }
   
    // Function to check Unusual number
    static boolean checkUnusual(int n)
    {
        // Get the largest Prime Factor
        // of the number
        int factor = largestPrimeFactor(n);
   
        // Check if largest prime factor
        // is greater than sqrt(n)
        if (factor > Math.sqrt(n)) {
            return true;
        }
        else {
            return false;
        }
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int n = 14;
   
        if (checkUnusual(n)) {
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to check Unusual number 
from math import sqrt
    
# Utility function to find largest 
# prime factor of a number 
def largestPrimeFactor(n):
  
    # Initialize the maximum prime factor 
    # variable with the lowest one 
    max = -1
    
    # Print the number of 2s that divide n 
    while n % 2 == 0:
        max = 2
        n >>= 1; # equivalent to n /= 2 
    
    # n must be odd at this point, thus skip 
    # the even numbers and iterate only for 
    # odd integers 
    for i in range(3,int(sqrt(n))+1,2): 
        while n % i == 0:
            max = i; 
            n = n / i; 
    
    # This condition is to handle the case 
    # when n is a prime number greater than 2 
    if n > 2:
        max = n
    
    return max 
    
# Function to check Unusual number 
def checkUnusual(n):
    # Get the largest Prime Factor 
    # of the number 
    factor = largestPrimeFactor(n) 
    
    # Check if largest prime factor 
    # is greater than sqrt(n) 
    if factor > sqrt(n): 
        return True
    else :
        return False
    
# Driver Code 
if __name__ == '__main__':
      
    n = 14
    
    if checkUnusual(n): 
        print("YES")
    else:
        print("NO")
  
# This code is contributed 
# by Harshit Saini 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to check Unusual number
   
using System;
class GFG {
   
    // Utility function to find largest
    // prime factor of a number
    static int largestPrimeFactor(int n)
    {
        // Initialize the maximum prime factor
        // variable with the lowest one
        int max = -1;
   
        // Print the number of 2s that divide n
        while (n % 2 == 0) {
            max = 2;
            n >>= 1; // equivalent to n /= 2
        }
   
        // n must be odd at this point, thus skip
        // the even numbers and iterate only for
        // odd integers
        for (int i = 3; i <= Math.Sqrt(n); i += 2) {
            while (n % i == 0) {
                max = i;
                n = n / i;
            }
        }
   
        // This condition is to handle the case
        // when n is a prime number greater than 2
        if (n > 2)
            max = n;
   
        return max;
    }
   
    // Function to check Unusual number
    static bool checkUnusual(int n)
    {
        // Get the largest Prime Factor
        // of the number
        int factor = largestPrimeFactor(n);
   
        // Check if largest prime factor
        // is greater than sqrt(n)
        if (factor > Math.Sqrt(n)) {
            return true;
        }
        else {
            return false;
        }
    }
   
    // Driver Code
    public static void Main()
    {
        int n = 14;
   
        if (checkUnusual(n)) {
            Console.WriteLine("YES");
        }
        else {
            Console.WriteLine("NO");
        }
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to check Unusual number 
    
// Utility function to find largest 
// prime factor of a number 
function largestPrimeFactor($n
    // Initialize the maximum prime factor 
    // variable with the lowest one 
    $max = -1; 
    
    // Print the number of 2s that divide n 
    while ($n % 2 == 0) { 
        $max = 2; 
        $n >>= 1; // equivalent to n /= 2 
    
    
    // n must be odd at this point, thus skip 
    // the even numbers and iterate only for 
    // odd integers 
    for ($i = 3; $i <= sqrt($n); $i += 2) { 
        while ($n % $i == 0) { 
            $max = $i
            $n = $n / $i
        
    
    
    // This condition is to handle the case 
    // when n is a prime number greater than 2 
    if ($n > 2) 
        $max = $n
    
    return $max
    
// Function to check Unusual number 
function checkUnusual($n
    // Get the largest Prime Factor 
    // of the number 
    $factor = largestPrimeFactor($n); 
    
    // Check if largest prime factor
    // is greater than sqrt(n) 
    if ($factor > sqrt($n)) { 
        return true; 
    
    else
        return false; 
    
    
// Driver Code  
$n = 14; 
  
if (checkUnusual($n)) { 
    echo "YES"."\n"
else
    echo "NO"."\n"
// This code is contributed 
// by Harshit Saini 
?>

chevron_right


Output:

YES

Time complexity: \text{O}(\sqrt{n})
Auxiliary space: \text{O}(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Harshit Saini