【问题标题】:Factorial of factorial of a number一个数的阶乘的阶乘
【发布时间】:2014-03-24 10:01:27
【问题描述】:

如何有效地计算一个数的阶乘的阶乘。
示例:对于 3 => (3!)! = (6)! = 720
蛮力方法是使用简单的 for 循环简单地调用阶乘两次,但可以做得更好。

for(i=1;i<=n;i++) 
   fact=fact*i; 

编辑:需要结果为 ((n!)!)MOD 10^m,其中 m 是整数,0

【问题讨论】:

  • 暴力破解有什么问题? (提示:计数因子为 10。)

标签: algorithm math factorial


【解决方案1】:

请注意,对于 n >=5(5!!=120! 末尾有 19 个以上的零),结果为 0,对于较小的值,结果很容易计算。 p>

【讨论】:

  • 我认为你是对的,除了 n >= 3 甚至可能是正确的。这是因为 3*2*1 == 6,它会产生包含 5 和 2 的东西. 5 * 2 是 10。其余的 10 倍总是可以被 10 或 10^m 整除。
【解决方案2】:

由于ab mod n ≣ (a mod n)(b mod n),您可以使用蛮力算法,每次乘法后除以10m。如果乘积等于 0,则可以停止。

【讨论】:

    【解决方案3】:

    我在这里使用 PHP。我认为它可以帮助你

    <?php
      function double_factorial ($n)
        {
            if($n <= 1) 
            {
                return 1;
            }
            else 
            {
                 $fat1=$n * factorial($n - 1);
                return $fat1 * factorial($fat1 - 1);
            }
        }
        echo double_factorial(3);
    
    ?>
    

    【讨论】:

      【解决方案4】:

      1.对于标准整数类型

      • 我同意 MBo
      • 我更喜欢预先计算的值表

      2.对于大整数

      【讨论】:

        【解决方案5】:

        以下代码已经过测试,运行良好。

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        
        namespace ConsoleApplication50
        {
            class Program
            {
                static void Main(string[] args)
                {
                    NumberManipulator manipulator = new NumberManipulator();
                    Console.WriteLine("Factorial of six is :" + manipulator.factorial(16));
                    Console.ReadLine();
                }
            }
        class NumberManipulator
        {
            public int factorial(int num)
            {
                int result=1;
                int b = 1;
                do
                {
                    result = result * b;//fact has the value 1  as constant and fact into b will be save in fact to multiply again. 
                    Console.WriteLine(result);
                    b++;
                } while (num >= b);
                return result;
            }
          }
        }
        

        【讨论】:

        • 它解决了哪个任务,为什么打印Factorial of six,和这个问题有什么联系?
        【解决方案6】:
        public class Factorial {
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int input = scanner.nextInt();
            int result = Factorial(input);
            System.out.println(result);
            scanner.close();
        }
        
        private static int Factorial(int input) {
            int m = 1;
            if (input < 0) {
                return 0;
            } else if (input > 0) {
                m = input * Factorial(input - 1);
            }
            return m;
        }
        
        }
        

        【讨论】:

        • 请提供一些解释,而不是只发布原始代码。
        • 这不显示How can the factorial of a factorial of a number be efficiently computed,也不争辩效率。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        • 1970-01-01
        • 1970-01-01
        • 2018-08-17
        • 1970-01-01
        • 2011-01-26
        相关资源
        最近更新 更多