在 C++ 中计算大阶乘;

发布于 2024-08-18 14:56:20 字数 640 浏览 9 评论 0原文

我知道这是一个经典的编程问题,因此我想澄清我并不是在寻找代码作为解决方案,而是希望能朝着正确的方向推动。我正在学习 C++,作为学习过程的一部分,我正在尝试一些编程问题。我正在尝试编写一个处理高达 10 亿阶乘的数字的程序。显然,这些数字将是巨大的,并且太大而无法使用正常的算术运算来处理。任何关于我应该朝哪个方向尝试解决此类问题的指示将不胜感激。

如果可能的话,我宁愿尝试在不使用额外库的情况下解决这个问题

谢谢

PS - 问题就在这里 http:// www.codechef.com/problems/FCTRL


这是我用来解决这个问题的方法,这是通过阅读下面的评论实现的:

​​ 解决方案 -- 数字 5 是任何以零结尾的数字的质因数。因此,递归地将阶乘数除以 5,然后将商相加,就可以得到阶乘结果中尾随零的数量

。 - 126 中尾随零的数量! = 31

126/5 = 25 余数 1

25/5 = 5 余数 0

5/5 = 1 余数 0

25 + 5 + 1 = 31

这适用于任何值,只需继续除,直到商小于 超过5

I understand this is a classic programming problem and therefore I want to be clear I'm not looking for code as a solution, but would appreciate a push in the right direction. I'm learning C++ and as part of the learning process I'm attempting some programming problems. I'm attempting to write a program which deals with numbers up to factorial of 1billion. Obviously these are going to be enormous numbers and way too big to be dealing with using normal arithmetic operations. Any indication as to what direction I should go in trying to solve this type of problem would be appreciated.

I'd rather try to solve this without using additional libraries if possible

Thanks

PS - the problem is here http://www.codechef.com/problems/FCTRL


Here's the method I used to solve the problem, this was achieved by reading the comments below:

Solution -- The number 5 is a prime factor of any number ending in zero. Therefore, dividing the factorial number by 5, recursively, and adding the quotients, you get the number of trailing zeros in the factorial result

E.G. - Number of trailing zeros in 126! = 31

126/5 = 25 remainder 1

25/5 = 5 remainder 0

5/5 = 1 remainder 0

25 + 5 + 1 = 31

This works for any value, just keep dividing until the quotient is less
than 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

剧终人散尽 2024-08-25 14:56:20

浏览了这个问题,不确定我是否真的答对了,但这是一个演绎性的猜测:

第一个问题 - 如何在数字末尾得到一个零?乘以 10。

如何乘以 10?要么乘以 10,要么乘以 2 x 5...

所以,对于 X!你有多少个 10 和 2x5...?

(幸运的是 2 和 5 是质数)

编辑:这是另一个提示 - 我认为您不需要进行任何乘法。如果您需要其他提示,请告诉我。

Skimmed this question, not sure if I really got it right but here's a deductive guess:

First question - how do you get a zero on the end of the number? By multiplying by 10.

How do you multiply by 10? either by multiplying by either a 10 or by 2 x 5...

So, for X! how many 10s and 2x5s do you have...?

(luckily 2 & 5 are prime numbers)

edit: Here's another hint - I don't think you need to do any multiplication. Let me know if you need another hint.

剪不断理还乱 2024-08-25 14:56:20

提示:您可能不需要计算 N!为了找到 N 末尾的零的数量!

Hint: you may not need to calculate N! in order to find the number of zeros at the end of N!

一刻暧昧 2024-08-25 14:56:20

要解决这个问题,正如克里斯·约翰逊所说,你必须查看 0 的数量。

10 的因数本身就是 1,2,5,10。所以,你可以遍历N个数字中的每一个!并将它们写成 2^x * 5^y * 10^z。丢弃数字的其他因素。

现在答案将大于(x,y)+z。

我从这个问题中学到的一件有趣的事情是,最好将数字的阶乘存储为素因数以便于比较。

实际上x^y,RSA算法中有一个简单的方法,不记得了。如果我找到一篇文章,我会尝试更新该帖子。

To solve this question, as Chris Johnson said you have to look at number of 0's.

The factors of 10 will be 1,2,5,10 itself. So, you can go through each of the numbers of N! and write them in terms of 2^x * 5^y * 10^z. Discard other factors of the numbers.

Now the answer will be greaterof(x,y)+z.

One interesting thing I learn from this question is, its always better to store factorial of a number in terms of prime factors for easy comparisons.

To actually x^y, there is an easy method used in RSA algorithm, which don't remember. I will try to update the post if I find one.

尴尬癌患者 2024-08-25 14:56:20

这不是对你的问题的一个很好的答案,因为你对我最初读到的内容进行了一些修改。但无论如何我都会把它留在这里,以证明实际上尝试通过主要的蛮力进行计算是不切实际的。

任何 bignum 库都无法计算 10 亿阶乘。表示这些数字需要的空间比几乎任何人在 RAM 中所拥有的空间都多。当您处理这些数字时,您将必须开始从存储中调入这些数字。有一些方法可以做到这一点。 最近计算出 π 到 27000 亿个位置的人使用了这样一个库

This isn't a good answer to your question as you've modified it a bit from what I originally read. But I will leave it here anyway to demonstrate the impracticality of actually trying to do the calculations by main brute force.

One billion factorial is going to be out of reach of any bignum library. Such numbers will require more space to represent than almost anybody has in RAM. You are going to have to start paging the numbers in from storage as you work on them. There are ways to do this. The guy who recently calculated π out to 2700 billion places used such a library

诠释孤独 2024-08-25 14:56:20

不要使用天真的方法。如果需要计算阶乘,请使用快速算法:http://www.luschny .de/math/factorial/FastFactorialFunctions.htm

Do not use the naive method. If you need to calculate the factorial, use a fast algorithm: http://www.luschny.de/math/factorial/FastFactorialFunctions.htm

半城柳色半声笛 2024-08-25 14:56:20

我认为在开始考虑 C++ 或任何其他语言之前,您应该想出一种用伪代码解决问题的方法。正如一些人指出的那样,这个问题的本质更像是一个算法问题,而不是一个 C++ 问题。那些建议搜索一些不起眼的图书馆的人正在把你引向滑坡的方向,因为学习编程就是学习如何思考,对吗?找到一本好的算法分析文本,它会对你很有帮助。在我们系,我们根据 CLRS 文本进行教学。

I think that you should come up with a way to solve the problem in pseudo code before you begin to think about C++ or any other language for that matter. The nature of the question as some have pointed out is more of an algorithm problem than a C++ problem. Those who suggest searching for some obscure library are pointing you in the direction of a slippery slope, because learning to program is learning how to think, right? Find a good algorithm analysis text and it will serve you well. In our department we teach from the CLRS text.

柳若烟 2024-08-25 14:56:20

你需要一个"big number"包 - 无论是你使用的还是一个你自己写的。

我建议对 “大量算法”。您需要实现 Java 的 BigDecimal

另一种看待它的方法是使用gamma 函数。您不需要将所有这些值相乘即可得到正确的答案。

You need a "big number" package - either one you use or one you write yourself.

I'd recommend doing some research into "large number algorithms". You'll want to implement the C++ equivalent of Java's BigDecimal.

Another way to look at it is using the gamma function. You don't need to multiply all those values to get the right answer.

隱形的亼 2024-08-25 14:56:20

首先,您应该将数字存储在某种数组中,例如 std::vector (数组中每个位置的数字),并且您需要找到某种算法来计算阶乘(可能以某种形式)专业课)。 ;)

To start you off, you should store the number in some sort of array like a std::vector (a digit for each position in the array) and you need to find a certain algorithm that will calculate a factorial (maybe in some sort of specialized class). ;)

灼痛 2024-08-25 14:56:20
    //SIMPLE FUNCTION TO COMPUTE THE FACTORIAL OF A NUMBER
    //THIS ONLY WORKS UPTO N = 65
    //CAN YOU SUGGEST HOW WE CAN IMPROVE IT TO COMPUTE FACTORIAL OF 400 PLEASE?     

    #include <iostream>
    #include <cmath>
    using namespace std;


    int factorial(int x);       //function to compute factorial described below

    int main()
        {
        int N; //= 150; //you can also get this as user input using cin.
        cout<<"Enter intenger\n";
        cin>>N;

        factorial(N);

        return 0;

        }//end of main




    int factorial(int x)        //function to compute the factorial
    {
     int i, n;
        long long unsigned results = 1;
        for (i = 1; i<=x; i++)
        {

        results = results * i;


        }

        cout<<"Factorial of "<<x<<" is "<<results<<endl;


        return results;
    }
    //SIMPLE FUNCTION TO COMPUTE THE FACTORIAL OF A NUMBER
    //THIS ONLY WORKS UPTO N = 65
    //CAN YOU SUGGEST HOW WE CAN IMPROVE IT TO COMPUTE FACTORIAL OF 400 PLEASE?     

    #include <iostream>
    #include <cmath>
    using namespace std;


    int factorial(int x);       //function to compute factorial described below

    int main()
        {
        int N; //= 150; //you can also get this as user input using cin.
        cout<<"Enter intenger\n";
        cin>>N;

        factorial(N);

        return 0;

        }//end of main




    int factorial(int x)        //function to compute the factorial
    {
     int i, n;
        long long unsigned results = 1;
        for (i = 1; i<=x; i++)
        {

        results = results * i;


        }

        cout<<"Factorial of "<<x<<" is "<<results<<endl;


        return results;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文