Comprobar si un número dado es factorial de cualquier número

Dado un número n, la tarea es determinar si n puede ser un factorial de algún número x
Ejemplos: 
 

Input: N = 24
Output: Yes
Explanation: 4! = 24

Input: N = 25
Output: No

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

C++

// C++ implementation for
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if
// the given number is a
// factorial of any number
bool isFactorial(int n)
{
  for (int i = 1;; i++) {
    if (n % i == 0) {
      n /= i;
    }
    else {
      break;
    }
  }
 
  if (n == 1) {
    return true;
  }
  else {
    return false;
  }
}
 
// Driver Code
int main()
{
  int n = 24;
 
  bool ans = isFactorial(n);
  if (ans == 1) {
    cout << "Yes\n";
  }
  else {
    cout << "No\n";
  }
 
  return 0;
}

C

// C implementation for
// the above approach
#include <stdio.h>
#include<stdbool.h>
 
// Function to check if
// the given number is a
// factorial of any number
bool isFactorial(int n)
{
  for (int i = 1;; i++) {
    if (n % i == 0) {
      n /= i;
    }
    else {
      break;
    }
  }
  
  if (n == 1) {
    return true;
  }
  else {
    return false;
  }
}
 
// Driver code
int main()
{
    int n = 24;
  
  bool ans = isFactorial(n);
  if (ans == 1) {
    printf("Yes\n");
  }
  else {
    printf("No\n");
  }
}
 
// Thiss code is contributed by allwink45.

Java

// Java implementation for the above approach
class GFG
{
 
    // Function to check if the given number
    // is a factorial of any number
    static boolean isFactorial(int n)
    {
        for (int i = 1;; i++)
        {
            if (n % i == 0)
            {
                n /= i;
            }
            else
            {
                break;
            }
        }
     
        if (n == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 24;
        boolean ans = isFactorial(n);
         
        if (ans == true)
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
    }
}
 
// This code is contributed by AnkitRai01

Python3

# Python3 implementation of the approach
 
# Function to check if
# the given number is a
# factorial of any number
def isFactorial(n) :
    i = 1;
    while(True) :
         
        if (n % i == 0) :
            n //= i;
             
        else :
            break;
             
        i += 1;
 
    if (n == 1) :
        return True;
     
    else :
        return False;
 
# Driver Code
if __name__ == "__main__" :
    n = 24;
    ans = isFactorial(n);
     
    if (ans == 1) :
        print("Yes");
 
    else :
        print("No");
 
# This code is contributed by kanugargng

C#

// C# implementation for the above approach
using System;
     
class GFG
{
 
    // Function to check if the given number
    // is a factorial of any number
    static Boolean isFactorial(int n)
    {
        for (int i = 1;; i++)
        {
            if (n % i == 0)
            {
                n /= i;
            }
            else
            {
                break;
            }
        }
     
        if (n == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
        int n = 24;
        Boolean ans = isFactorial(n);
         
        if (ans == true)
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// Javascript implementation for
// the above approach
 
// Function to check if
// the given number is a
// factorial of any number
function isFactorial(n)
{
    for (var i = 1;; i++)
    {
        if (n % i == 0)
        {
            n = parseInt(n/i);
        }
        else
        {
            break;
        }
    }
 
    if (n == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
// Driver Code
var n = 24;
var ans = isFactorial(n);
if (ans == 1)
{
    document.write("Yes");
}
else
{
    document.write("No");
}
 
// This code is contributed by noob2000.
</script>
Producción

Yes

Complejidad de tiempo: O (log 10 n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por spp____ 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 *