Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

1000 factorial

Name: Anonymous 2011-04-02 22:59

Well, someone wrote that they could write a program to do 1000! in one line of Haskell. I said that it would take less than 100 lines of C to do the same. I wrote the program, and it is less than 100 lines of C:

#include <stdio.h>
#include <string.h>

#define LENGTH 10000

int max (int a, int b) { return (a > b) ? a : b; }
int min (int a, int b) { return (b > a) ? a : b; }

int length (unsigned char * a)
 {
   int i;
   i = LENGTH - 1;
   while ((a[i] == 0) && (i >= 0)) i--;
   return i + 1;
 }

void add (unsigned char * a, unsigned char * b)
 {
   int i, c, C;
   c = min(max(length(a), length(b)) + 1, LENGTH);
   for (i = 0, C = 0; i < c; i++)
    {
      a[i] += b[i] + C;
      C = a[i] > 99;
      if (C) a[i] -= 100;
    }
 }

   unsigned char x [LENGTH], y [LENGTH];
void mul (unsigned char * a, unsigned char * b)
 {
   int i, j, c, d, C;
   memset(y, '\0', LENGTH);
   c = min(length(a) + length(b) + 1, LENGTH);
   d = length(b);
   for (i = 0; i < d; i++)
    {
      memset(x, '\0', LENGTH);
      for (j = 0, C = 0; (i + j) < c; j++)
       {
         x[i + j] = (a[j] * b[i] + C) % 100;
         C = (a[j] * b[i] + C) / 100;
       }
      add(y, x);
    }
   memcpy(a, y, LENGTH);
 }

void numset (unsigned char * a, unsigned long b)
 {
   int i;
   for (i = 0; i < LENGTH; i++)
    {
      a[i] = b % 100;
      b /= 100;
    }
 }

int tz (unsigned char * a)
 {
   int i;
   for (i = 0; i < LENGTH; i++) if (a[i] != 0) return 0;
   return 1;
 }

   unsigned char minus1 [LENGTH], prod [LENGTH];
void fact (unsigned char * a)
 {
   numset(prod, 1);
   while (!tz(a))
    {
      mul(prod, a);
      add(a, minus1);
    }
   memcpy(a, prod, LENGTH);
 }

   unsigned char arg_res [LENGTH];
int main (void)
 {
   unsigned long a;
   int i;
   memset (minus1, 99, LENGTH);
   printf("Input the number to take the factorial of: ");
   scanf("%lu", &a);
   numset(arg_res, a);
   fact(arg_res);
   i = LENGTH - 1;
   while (arg_res[i] == 0) i--;
   while (i >= 0)
    {
      printf("%02d", (int) arg_res[i]);
      i--;
    }
   putchar('\n');
   return 0;
 }

Name: n3n7i 2011-09-01 3:16

!2500[/code]16288,884241692,635468966,810574743,966336539,994283436,659333376,117059851,739595300,666601568,118117109,111430182,218994996,706377540,737964295,726648036,84914477,39
8269956,576650394,995303908,153606931,358938562,424868716,863336511,787772831,963234651,490597845,804707452,80712773,761945183,179002366,243765637,991536689,969242581,
709947395,573553799,155162061,20587956,162836453,609056109,182552093,352343844,29882417,375246821,954281460,20336896,525591606,956233891,343329496,954631026,393022945,
474865068,966259267,963805071,707264234,749398946,807274223,651874046,23994635,224545104,61309775,665397330,572064502,645799793,490535692,439961861,758186037,617483580
,487420516,854225746,700866725,272078424,896992597,788322485,750313103,767538280,635190313,55438652,113070059,895360069,459016503,698021402,127430434,703720577,4546036
84,221486207,712971570,279183098,247144580,669751192,292412687,570776382,442783145,813125272,512987140,13465430,577373695,416037438,604330731,495427723,748498601,31677
7072,913720020,200624759,285687594,697103942,902831458,433117148,104802139,150255844,954156372,702572242,931979348,640772104,241935322,544694355,717741028,42721831,573
93383,946811950,229862119,18492668,601533950,515675995,793861869,111189410,513752442,848879659,1774939,446410165,714053104,744903131,715021128,531205114,521790600,4483
2229,285647606,408017904,177251780,563861670,452217895,698401839,16268343,830469429,772772782,341220769,473426587,820287290,19473077,524695825,215527904,355576391,3056
00088,839325393,721013677,844373796,989572057,534519771,31549187,963257721,208029673,279152430,652933276,800258223,453219383,978743812,269682334,913717476,68767081,112
170724,712287720,561807845,229060596,372853438,939340670,348358259,624827210,411996569,765719571,305348561,907445521,649287971,976375847,487178355,765492815,778069121,
838364685,540983459,992106337,314470299,659462768,807774194,455026719,275830902,631301620,632068053,5745274,643641270,818310893,189040468,508343150,208376066,332465734
,970601526,332798266,648668957,684928388,346914251,393674102,236838190,309415765,24962992,701286434,254040733,64624752,399588405,701518471,706282680,92033896,216655874
,206291783,633993514,147758055,661610275,976159918,807613941,637566649,34779587,69377199,455553747,637235892,554444579,113470055,333978002,998933446,236448649,95633864
3,549877097,69790252,117694271,543914179,639916424,71991406,456604783,333397965,866797905,100968905,477558448,660543042,454554471,492045598,502849277,515838640,5002083
65,860739763,710206685,971849678,108935761,798782539,66278141,381636294,637082189,768125799,193702797,967538238,466562473,387279176,788278704,807481230,413644276,13972
0229,104456308,83258037,763826781,395687638,241302508,20291782,679358425,712165041,212352088,250542961,656610307,562083717,426864028,254048045,585013278,396707312,9880
98509,307199244,525251413,18638107,871271406,375801619,527964709,310126699,327425652,342396160,313371140,810226949,214136412,603864243,886523013,717112551,532688276,16
4952934,427157810,894957954,46837445,796764595,217297020,162001470,343757782,370085850,953552320,637100882,919579912,163108370,28314403,969241003,234290634,568270458,9
55949171,264334907,57977769,908075381,921113966,351587586,648467738,374135641,552139894,953507856,890412402,614641785,184184650,269635082,32520382,461666556,52083240,7
49659841,927331974,627710176,727263009,232886075,400144727,578901134,34342119,214962884,370001625,512726452,523206152,157166541,752489388,503280463,130706903,614053713
,733296237,361667312,991010932,983656540,560377330,832262770,42696095,731040694,487906848,645462190,989961711,109989113,241974798,68964703,59871195,609328565,827196434
,230198178,800412242,371942746,686047154,961984072,73558094,313894903,724884222,66777831,669419732,898160360,633747237,482986968,369023008,889690448,245258289,10570687
6,230750084,254201797,244121746,320131347,525589214,486094781,766265733,538907918,16852228,868499907,315181338,394080723,321126032,440189828,823699970,328255861,187143
922,82019147,768883662,612191302,509135461,511051477,630808296,519282900,741066316,50077242,544314881,58045728,870693282,326830433,19004661,600521723,836651817,3815298
98,440636383,917095475,899004094,206317468,376377314,153856018,840069377,218558903,334939371,343395777,264426365,318130887,683599836,88345497,158322556,553595094,84089
4654,614406383,376396868,199531042,942079405,403474627,428650274,595771829,568255995,466465003,325664472,293654101,201924430,539079190,443194487,46380416,281749159,263
00696,294947812,447714612,418298771,332692848,211154125,929312681,287195951,419960699,391722211,424311898,584816200,565373258,799897110,855302098,610980845,978364362,6
85231061,369005215,230788471,798866541,648729889,538439509,890111560,301062943,923923769,988997147,648363950,708508382,34563798,637202146,673551309,938897540,440786998
,3118907,883241416,258343188,784000679,367284896,11745823,289734561,911522255,979545433,492376668,285434347,783001266,108600121,473498779,217963921,384546755,156766188
,951735866,744108093,964636023,471349527,19498920,540070752,394649104,708510770,999131507,315610483,632330982,876174119,389012968,417760250,231722884,408641474,3403748
93,362833795,392300949,100935853,949419952,854923937,872507275,773843355,108735204,114864752,466565493,366395162,830913421,138498633,186453324,268762572,668445390,7709
03011,717202242,312950408,723851830,364004678,106656009,78222984,968635868,416766693,788940980,240045058,68275614,302839043,567118028,698385054,274108921,811298084,233
976240,6277809,181325997,330000786,683674194,376654268,377368369,949823431,206244095,885960702,59149846,318232687,179678572,748082487,793549844,548187548,311400556,647
44050,666472476,457947299,489568330,636742840,865513804,654776149,494225932,236964194,164784786,218640251,626514525,549920894,810545056,499250214,652147224,638246034,8
92251401,29945773,476251196,919612592,752826016,419187902,895630887,737745713,281219681,141023586,882321633,118519558,748181198,803719862,314967759,867112077,291645583
,600510531,17148754,676649505,951293767,894337326,791942862,414631993,126449392,270145239,108425380,56269282,654566300,820253116,701666036,949374826,952156733,28626270
0,190206665,725355635,411559887,439781283,350874285,109852181,238143636,938256407,98028863,909426094,536431398,247852543,854757537,942646791,855344906,17659039,3849051
22,375234004,320416026,882823286,655632286,53156546,748028668,750196973,803337027,786891236,116711910,780819707,846081218,679760982,625290715,984522150,204916425,23259
1826,590887410,401320568,45430558,979943145,617193692,59705465,421939829,497586908,594951642,657711985,208269462,425640322,372314303,330475772,671495014,49400654,15561
9566,942191085,558759378,416790160,458835621,823599743,742665152,596726797,511419955,382867531,134959437,2011587,472231152,746047820,309830039,699792147,174883667,8557
60287,780241438,38062954,704157981,72533480,950728570,115662921,849376586,317866043,9133972,729549191,452640224,225320222,817060812,601144385,749818368,781373067,88882
7129,923654130,858902252,804622762,751192618,314790547,633755929,57344102,888830740,499275949,997990026,813425216,747458578,654838647,236496508,876074530,642629714,199
563056,483975900,965902520,59913821,418901761,372054615,449749487,163282905,699188377,895083782,877246773,269374715,942460000,414707728,522199826,307191493,572953655,7
49106244,370767768,17669677,808468909,623091993,519298870,943235516,530358122,980923396,832082235,382703392,292864000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
[/code]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List