Large factorial with built-in data type

Hello,

Can I write a program in c++ for calculating the below series with long long int? (Fifty-first sentences)

Or I should use an array and other algorithms?

Thanks

https://i.ibb.co/qMmVBNH/Annotation-2020-07-10-034926.jpg

(excuse me
I sent the wrong photo
I edited the photo)

Last edited on
Translate it to English.
Dear salem c and dutch.

I'm sorry. I sent the wrong photo.
I edited the photo now.

Thank you
The max you can fit in an unsigned long long is 20!
https://stackoverflow.com/questions/19230573/cannot-calculate-factorials-bigger-than-20-how-to-do-so

One option would be to use an external library like
GNU MP Bignum library: https://gmplib.org/
or
https://github.com/williamchanrico/biginteger-cpp
This should help in your calculations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

using namespace std;

string operator+( const string& a , const string& b )
{
    string result {};

    int sum {0};
    int offset {0};
    size_t index_a {a.size()};
    size_t index_b {b.size()};

    do
    {
        sum = 0;
        if( index_a>0 ) sum += a[--index_a]-'0';
        if( index_b>0 ) sum += b[--index_b]-'0';

        sum += offset;

        if( sum>9 )
        {
            offset = 1;
            sum -= 10;
        }
        else offset = 0;

        result.insert(0,1,static_cast<char>(sum+'0'));
    }
    while( index_a!=0 || index_b!=0 );

    if( offset==1 ) result.insert(0,1,'1');

    return result;
}

string operator*( const string& a , const string& b )
{
    string result {"0"};
    string partialsum;
    int zeroes {0};

    for( auto iter_a {a.crbegin()} ; iter_a!=a.crend() ; ++iter_a )
    {
        int offset {0};
        partialsum.clear();
        for( auto iter_b {b.crbegin()} ; iter_b!=b.crend() ; ++iter_b )
        {
            auto product { static_cast<int>(*iter_a-'0')*static_cast<int>(*iter_b-'0')+offset };
            offset = product/10;
            partialsum.insert( 0 , 1 , static_cast<char>(product-offset*10) + '0' );
        }
        if( offset>0 ) partialsum.insert( 0 , 1 , static_cast<char>(offset) + '0' );
        partialsum += string(zeroes++,'0');
        result = result + partialsum;
    }

    return result;
}

string factorial( int i )
{
    if( i == 0 || i == 1 ) return "1";
    if( i == 2 ) return "2";

    string result {"2"};

    for( int index {3} ; index<=i ; ++index )
    {
        result = to_string(index)*result;
    }

    return result;
}

int main()
{
    for( auto n {0} ; n<=50 ; ++n ) cout << n << "! = " << factorial(n) << endl;
    cout << endl;

    cout << "49!*50! = " << factorial(49)*factorial(50) << endl;
    return 0;
}

https://wandbox.org/permlink/letdsVTa354KUVwH
@Shervan360
Maybe you should post your whole exercise, not a faulty picture of it. What are you attempting to calculate this sum for?

Presumably your series terminates? If so, at what?

If you try to write it as an actual fraction then the denominator will be 49!x50! which, even with a big-number class, will have a vast number of digits. The numerator would be even bigger. And I wouldn't care to cancel it down with a GCD algorithm. If you try to write it as a floating-point number then the summation would lead to huge round-off error.

So, where does this sum come from?

BTW - what do you mean by
(Fifty-first sentences)
Last edited on
Since the first terms are less than zero, you can't use any integer representation unless you keep both the numerator and denominator.

When computing any sort of a series, see if you can compute each term from the previous term. That usually improves speed an accuracy.

Is there anything you can do to the series using math? I notice several things about it but on a post-it, I can't figure how they would help, except to speed up the computation.

The last term in the series == 1/(first term). So the 49 terms are 24 pairs of the form a/b + b/a, plus one in the middle (which might be 1?)

Also, you can recast the series so that each term's denominator is 50!*49!. That might help with the computation.

50!*49! = 50*49!2, but I don't see how that helps.

The point here is that you should see if you can simplify this mathematically before trying to compute it numerically.

The whole series contains some very small numbers (first term) and some very big ones (last term). 50! * 49! is about 1.85*10127, which takes around 425 bits to represent, so you'd definitely need something like BigNum to compute it numerically, as Thomas1965 suggested.

Is this homework? If so then I suspect the point of the exercise is to show you the limitations of how computers represent numbers. That also makes me think it can be solved mathmatically
Topic archived. No new replies allowed.