Primera cifra en factorial de un número

Dado un entero positivo n, encuentre el primer dígito en su factorial.

Ejemplos: 

Input  : n = 5
Output : 1
Factorial of 5 is 120 and first
digit is 1.

Input  : 1000
Output : 4

Una solución simple es calcular el factorial del número y luego encontrar el primer dígito en él. 
La solución anterior provoca un desbordamiento pronto. Una mejor solución es utilizar el hecho de que el factorial contiene ceros finales y eliminar los ceros finales no cambia el primer dígito. Por ejemplo, el primer dígito de x * y es lo mismo que x * y * 100 para x > 0 e y > 0.

C++

// A C++ program for finding the First digit
// of the large factorial number
#include <bits/stdc++.h>
using namespace std;
 
int firstDigit(int n)
{
    long long int fact = 1;
 
    for (int i = 2; i <= n; i++) {
        fact = fact * i;
 
        // Removing trailing 0s as this
        // does not change first digit.
        while (fact % 10 == 0)
            fact = fact / 10;       
    }
 
    // loop for divide the fact until it
    // become the single digit and return
    // the fact
    while (fact >= 10)
        fact = fact / 10;
     
    return fact;
}
 
// derive main
int main()
{
    int n = 5;
    cout << firstDigit(n);
    return 0;
}

Java

// A Java program for finding the First digit
// of the large factorial number
class GFG{
static int firstDigit(int n)
{
    int fact = 1;
 
    for (int i = 2; i <= n; i++) {
        fact = fact * i;
 
        // Removing trailing 0s as this
        // does not change first digit.
        while (fact % 10 == 0)
            fact = fact / 10;    
    }
 
    // loop for divide the fact until it
    // become the single digit and return
    // the fact
    while (fact >= 10)
        fact = fact / 10;
     
    return fact;
}
 
// derive main
public static void main(String[] args)
{
    int n = 5;
    System.out.println(firstDigit(n));
}
}
//This code is contributed by Smitha Dinesh Semwal

Python3

# Python3 program for finding
# the First digit of the
# large factorial number
import math
def firstDigit(n) :
    fact = 1
 
    for i in range(2, n + 1) :
        fact = fact * i
 
        # Removing trailing 0s
        # as this does not
        # change first digit.
        while (fact % 10 == 0) :
            fact = int(fact / 10)
 
    # loop for divide the fact
    # until it become the single
    # digit and return the fact
 
    while (fact >= 10) :
        fact = int(fact / 10)
     
    return math.floor(fact)
 
# Driver Code
n = 5
print (firstDigit(n))
 
# This code is contributed by
# Manish Shaw(manishshaw1)

C#

// A C# program for finding the First digit
// of the large factorial number
using System;
 
class GFG {
     
    static int firstDigit(int n)
    {
        int fact = 1;
     
        for (int i = 2; i <= n; i++)
        {
            fact = fact * i;
     
            // Removing trailing 0s as this
            // does not change first digit.
            while (fact % 10 == 0)
                fact = fact / 10;    
        }
     
        // loop for divide the fact until
        // it become the single digit and
        // return the fact
        while (fact >= 10)
            fact = fact / 10;
         
        return fact;
    }
     
    // driver function
    public static void Main()
    {
        int n = 5;
         
        Console.Write(firstDigit(n));
    }
}
 
// This code is contributed by parashar.

PHP

<?php
// PHP program for finding
// the First digit of the
// large factorial number
 
function firstDigit($n)
{
    $fact = 1;
 
    for ($i = 2; $i <= $n; $i++)
    {
        $fact = $fact * $i;
 
        // Removing trailing 0s as this
        // does not change first digit.
        while ($fact % 10 == 0)
            $fact = $fact / 10;
    }
 
    // loop for divide the fact
    // until it become the single
    // digit and return    the fact
 
    while ($fact >= 10)
        $fact = $fact / 10;
     
    return floor($fact);
}
 
// Driver Code
$n = 5;
echo firstDigit($n);
 
// This code is contributed by aj_36.
?>

Javascript

<script>
 
// JavaScript program for finding the
// First digit of the large factorial number
function firstDigit(n)
{
    let fact = 1;
   
    for(let i = 2; i <= n; i++)
    {
        fact = fact * i;
   
        // Removing trailing 0s as this
        // does not change first digit.
        while (fact % 10 == 0)
            fact = fact / 10;    
    }
   
    // Loop for divide the fact until it
    // become the single digit and return
    // the fact
    while (fact >= 10)
        fact = fact / 10;
       
    return (Math.round(fact));
}
 
// Driver code
let n = 5;
 
document.write(firstDigit(n));
 
// This code is contributed by splevel62
 
</script>
Producción : 

1

 

El código anterior también falla para valores ligeramente más altos. La mejor idea parece ser encontrar el factorial de un gran número y luego encontrar el primer dígito.
 

Publicación traducida automáticamente

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