Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.27/5 (4 votes)
See more:
Is there any way to store value which exceeds range of all data types in C++.
How to program to store the result of factorial of 10000
Posted
Updated 28-Apr-19 13:55pm

C++
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
    int i;
    int n = 69;
    RossiBigInt nfactorial(1);
    for (i = 2; i <= n; i++) {
        RossiBigInt bigi(i);
        nfactorial = nfactorial * bigi;
        cout << i << " factorial = " << nfactorial << endl;
    }
    return 0;
}


For bigint go through this link. http://groups.google.com/group/sources/msg/496e8d3ae812abbb[^]
 
Share this answer
 
v3
Comments
Olivier Levrey 26-Apr-11 5:14am    
My 5. I just fixed the tags for code formatting.
What you want to use is a Big number class or library. Search on google to find one. For example:
https://mattmccutchen.net/bigint/[^]
 
Share this answer
 
I have been using the Number Theory Library[^] for dealing with large integers. It worked well for my needs.
 
Share this answer
 
AFAIK, Not a defined datatype - you would need to develop some own method. unsigned __int64 data type can store large values, but it's still not enough here.

Similar discussion here[^] with some possible ways.
 
Share this answer
 
#include<stdio.h>
#include<iostream>
#include<algorithm>
#define forr(i,a,b) for(int i=(a);i<(b);++i)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;--i)
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define fst first
#define snd second
#define sz(c) ((int)c.size())
#define MAXP 1000000
#define BASE 1000000
#define LMAX 10000
using namespace std;
typedef long long ll;


struct bint {
	int l;//LONGITUD
	ll n[LMAX];//EL NUMERO 
	bint(ll x = 0) {
		l = 1;
		forn(i, LMAX) {
			if (x) l = i + 1;
			n[i] = x % BASE;
			x /= BASE;

		}
	}
	void out() {
		cout << n[l - 1];
		dforn(i, l - 1) printf("%6.6llu", n[i]);
	}
	void invar() {
		fill(n + l, n + LMAX, 0);
		while (l > 1 && !n[l - 1]) l--;
	}
};
bint operator*(const bint & a, ll b) {
	bint c;
	ll q = 0;
	forn(i, a.l) q += a.n[i] * b, c.n[i] = q % BASE, q /= BASE;
	c.l = a.l;
	while (q) c.n[c.l++] = q % BASE, q /= BASE;
	c.invar();
	return c;
}
int main() {
	int n = 10000;
	bint res(1);
	for (int i = 1; i <= n; ++i) {
		res = res * i;
	}
	res.out();
}

Ahi esta Campeón, en programación competitiva este tipo de cosas son pan de cada día
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900