📜  查找N个阶乘和的最后两位数

📅  最后修改于: 2021-04-29 13:57:30             🧑  作者: Mango

给定数字N,任务是查找前N个自然数阶乘的单位和十位数,即1!+2!+3!+….N!的最后两位。其中N <= 10e18。
例子:

Input : n = 2 
Output :3
1! + 2! = 3
Last two digit  is 3

Input :4
Output :33
1!+2!+3!+4!=33
Last two digit is 33

天真的方法:在这种方法中,只需计算每个数字的阶乘并求和。最终得到总和的单位和十位数。这将花费大量时间和不必要的计算。
高效的方法:在这种方法中,仅在[1,10]范围内计算N的单位和十位数,因为:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8!= 40320
9!= 362880
10!= 3628800
很快。
由于10!= 3628800,且大于10的阶乘有两个尾随零。因此,N> = 10在求和时对单位和十位无贡献。
所以,

下面是上述方法的实现。

C++
// C++ program to find the unit place digit
// of the first N natural numbers factorials
#include 
using namespace std;
#define ll long int
// Function to find the unit's and ten's place digit
int get_last_two_digit(long long int N)
{
 
    // Let us write for cases when
    // N is smaller than or equal
    // to 10.
    if (N <= 10) {
        ll ans = 0, fac = 1;
        for (int i = 1; i <= N; i++) {
            fac = fac * i;
            ans += fac;
        }
        return ans % 100;
    }
 
    // We know following
    // (1! + 2! + 3! + 4!...+10!) % 100 = 13
    else // (N >= 10)
        return 13;
}
 
// Driver code
int main()
{
    long long int N = 1;
    for (N = 1; N <= 10; N++)
        cout << "For N = " << N
             << " : " << get_last_two_digit(N)
             << endl;
 
    return 0;
}


Java
//Java program to find the unit place digit
//of the first N natural numbers factorials
public class AAA {
 
    //Function to find the unit's and ten's place digit
    static int get_last_two_digit(long N)
    {
 
     // Let us write for cases when
     // N is smaller than or equal
     // to 10.
     if (N <= 10) {
         long ans = 0, fac = 1;
         for (int i = 1; i <= N; i++) {
             fac = fac * i;
             ans += fac;
         }
         return (int)ans % 100;
     }
 
     // We know following
     // (1! + 2! + 3! + 4!...+10!) % 100 = 13
     else // (N >= 10)
         return 13;
    }
 
    //Driver code
    public static void main(String[] args) {
         
        long N = 1;
         for (N = 1; N <= 10; N++)
             System.out.println( "For N = " + N
                  + " : " + get_last_two_digit(N));
    }
 
}


Python3
# Python3 program to find the unit
# place digit of the first N natural
# numbers factorials
 
# Function to find the unit's
# and ten's place digit
def get_last_two_digit(N):
     
    # Let us write for cases when
    # N is smaller than or equal
    # to 10
    if N <= 10:
        ans = 0
        fac = 1
        for i in range(1, N + 1):
            fac = fac * i
            ans += fac
        ans = ans % 100
        return ans
         
    # We know following
    # (1! + 2! + 3! + 4!...+10!) % 100 = 13
    # // (N >= 10)
    else:
        return 13
 
# Driver Code
N = 1
for N in range(1, 11):
    print("For N = ", N, ": ",
           get_last_two_digit(N), sep = ' ')
 
# This code is contributed
# by sahilshelangia


C#
// C# program to find the unit
// place digit of the first N
// natural numbers factorials
using System;
 
class GFG
{
 
// Function to find the unit's
// and ten's place digit
static int get_last_two_digit(long N)
{
 
// Let us write for cases when
// N is smaller than or equal
// to 10.
if (N <= 10)
{
    long ans = 0, fac = 1;
    for (int i = 1; i <= N; i++)
    {
        fac = fac * i;
        ans += fac;
    }
    return (int)ans % 100;
}
 
// We know following
// (1! + 2! + 3! + 4!...+10!) % 100 = 13
else // (N >= 10)
    return 13;
}
 
// Driver code
public static void Main()
{
    long N = 1;
    for (N = 1; N <= 10; N++)
        Console.WriteLine( "For N = " + N +
            " : " + get_last_two_digit(N));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP
= 10)
        return 13;
}
 
// Driver code
$N = 1;
for ($N = 1; $N <= 10; $N++)
    echo "For N = " . $N . " : " .
          get_last_two_digit($N) . "\n";
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Javascript


输出:
For N = 1 : 1
For N = 2 : 3
For N = 3 : 9
For N = 4 : 33
For N = 5 : 53
For N = 6 : 73
For N = 7 : 13
For N = 8 : 33
For N = 9 : 13
For N = 10 : 13