Comprobar si un número es un número inusual o no

Dado un entero positivo N. La tarea es verificar si N es un número inusual o no. Escriba ‘SÍ’ si M es un número inusual, de lo contrario escriba ‘NO’.
Número inusual : En Matemáticas, un número inusual es un número natural cuyo mayor factor primo es estrictamente mayor que la raíz cuadrada de n.
Los primeros números inusuales son: 
 

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 
 

Ejemplos

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 ).

Acercarse : 

  1. Encuentre el factor primo más grande del número N dado. Para encontrar el factor primo más grande de N, consulte este .
  2. Comprueba si el factor primo más grande de N es estrictamente mayor que la raíz cuadrada de N.
  3. Si ‘SÍ’, entonces N es un número inusual, de lo contrario, no.

A continuación se muestra la implementación del enfoque anterior: 

C++

// 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;
}

Java

// 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");
        }
    }
}

Python3

# 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

C#

// 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");
        }
    }
}

PHP

<?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
?>

Javascript

<script>
 
// javascript 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
         
        var 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 (var 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
     
    function  checkUnusual(n)
    {
        // Get the largest Prime Factor
        // of the number
         
        var 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
 
        var n = 14;
    
        if (checkUnusual(n)) {
            document.write("YES");
        }
        else {
            document.write("NO");
        }
         
 </script>
Producción: 

YES

Complejidad temporal:  \text{O}(\sqrt{n})
Espacio auxiliar: \text{O}(1)
 

Publicación traducida automáticamente

Artículo escrito por AR_Gaurav y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *