Last active
December 16, 2015 03:59
-
-
Save iwillspeak/5374227 to your computer and use it in GitHub Desktop.
# Compute the factorial of almost any number Mainly an excuse to use GMP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <gmp.h> | |
/** | |
* Compute a Factorial | |
* | |
* returns a string representation of the number `n` in base 10. | |
*/ | |
char* fact(unsigned int n) | |
{ | |
mpz_t res; | |
char* str; | |
mpz_init_set_ui(res, 1); | |
while (n > 1) { | |
mpz_mul_ui(res, res, n--); | |
} | |
str = mpz_get_str(NULL, 10, res); | |
mpz_clear(res); | |
return str; | |
} | |
int main (int argc, char* argv[]) | |
{ | |
unsigned int n; | |
char* f; | |
for (int i = 1; i < argc; i++) { | |
n = atoi(argv[i]); | |
f = fact(n); | |
printf("%d! = %s\n", n, f); | |
free(f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Computes the following in < 1 second:
100000! = 28242294079603478742934215780245355184774949260912248505789180865429779509010630178725517714138311636107136117373619629514749961831239180227260734090938324220055569688667840380377379444961268380147875111966906386044926144538111370090160766866405407170565952261298041958356778909047541512871140836924251535293096260672271038744246088635454363982931747761775532621851126474855864918180381519877161219681514129902304463824068896508357500229649939642364256635271614935207801331202943393059481996043539694202547610187382521727119665242224629786132218975049740195171653153048987483605056695271548017651216213800410981680797345354785175202462194504834501377326310693909350359885988263210528414140015756786096090291650746966135452625307325475302156830715404249700297210424646577137505616581728515517216288676066448180981858456491209123026827681473512252959123566976223847532826157700469481149648484913019536713400018031434012380511368173835296047361782363590900324442979506880200425497075817428109200515871132725419763955449965643205610662338099441079965090239835058688262541015044370484320696146208300581622032445457656923187565655142229321450667889684581664034312175421203842609068583814496145348996192319004679779630870771992965230958496008255678095829679128378996667544726319901350598963786237496380026027212712803720071903325318612085809426691843188133798061723913913711424525415380091954821781271034190574047677909474085485853003394617268058261532865988710695226333046882761562985860251045159006884365497700918591314079336300094266872450185905716172910319364909910982282171113034188814194168741059041047545569219310938636141579837579416940455279389371093841823524834691909773492295125953071398328562544812179152705046595958654656954779020092178370816222399126665698730839796362496210376149233471327600693023992176735118910577741564091153375237377967391642998745016394658581996592998334666059301647826349860479862872111189486311700077299334678795331022087853029064847682202470877226978827225703101293175999754152186080437662400387353762689053755715400425540320217748368675996336131595004238899499641854577857071050256848118475143977630753819681970818093539794067232920368908922783200763693358406584171498415879485891096945905672899713798415851643549629222750438948618153020439345247463008276498877765886574308087875413413076591503572256732064081391402979634937427007828401032999292566908606022083315983752819999156718709016301529164719115947920455896541741086277247149539333302596387664521161931270883282630583794072824775649170677720215796137315566388581174578829571182034171407714529475535797108182669757987365913307838682857595034767823707313759310787385400846883789925536631162479425047512615104634570611952476082258991470889730484997143514677051205872227048811238940155630842423255665822345240232500641846295922034770232544316636788345242347863518408683775566104412353145447124693099240549723842158184312016756039050317199725245656578583775808577538752965876118431967190114283315961863712299043641019251045901748994857351847545389669361365964528208636002427760428785759972672367893509433104811637811370730616577019136151552105444134731885196163472343099052391233625740331701021209610386118438680635978534912772759311376155125835880861436096960784145435311604736555422959099787360857819765485869182663659301669728836183187527585681057756759660676312618449087454840993232472607816610018772424475784827573256346192806768258063000257029675179576879240770103278259500518336875928686092661235311958664064242828565182177
... Snip so that I can post this comment ....
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000