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: Anonymous 2011-09-01 2:43


go fuck an autistic nigger, you fucking assburgers fagstorm shitbreath pencildick nigfucker

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]

Name: Anonymous 2011-09-01 3:24

Name: Anonymous 2011-09-01 9:03

Hey C fags when you include a header the amount of lines in your program actually increases, if it didn't you could just write everything under one big macro include a header and do EVERYTHING as a one liner, you have to explicitly use extern or your code is invalid and the amount of lines used compiler/system/architecture dependent.

Name: Anonymous 2011-09-01 9:38

@Anonymous
No, those are standard library functions. Just like "alert" in javascript. The calculations are not done by iostream.

Name: Anonymous 2011-09-01 9:39

@Anonymous
No, those are standard library functions. Just like "alert" in javascript. The calculations are not done by iostream.

Name: Anonymous 2011-09-01 9:40

Sorry for double post. Seems like double click is double post on my browser...

Name: Anonymous 2011-09-01 10:03

((λ (n) ((((λ (f) ((λ (fs) (λ (n) (λ (a) (if (= n 0) a ((fs (- n 1)) (* a n)))))) (λ (a) ((f f) a)))) (λ (f) ((λ (fs) (λ (n) (λ (a) (if (= n 0) a ((fs (- n 1)) (* a n)))))) (λ (a) ((f f) a)))))
 n) 1)) 1000)

Name: Anonymous 2011-09-01 10:16

>>88
Even lambda calculus is shorter and more readable than C.

Language for real man my anus.

Name: Anonymous 2011-09-01 11:38

!x:1e3

Factorial of extended precision value of 103. J, motherfuckers.

Name: Anonymous 2012-07-15 23:49

>>46
fuck you and die you little queerfag, this is /prob/, we are here! that is all!

Name: Anonymous 2012-07-16 0:10

>>88
Please teach me your magic, wizard.

Name: Anonymous 2012-07-16 0:18

I've tried and tried to understand lambda calculus but my dumb ass struggles to retain the information and I just don't get it. Maybe the context-free grammar thing is too confusing for me.

So, could someone please share a good tutorial for absolute fagstorms?

Name: Anonymous 2012-07-16 0:23

>>93
Read SICP.

Name: Anonymous 2012-07-16 0:37

>>93
It's a lot simpler than you think.

Name: Anonymous 2012-07-16 0:37

A fack of four is twenty four.

Name: Anonymous 2012-07-16 1:01

(define (hax anus) (cond ((= anus 1) 1) (else (* anus (hax (- anus 1))))))
(hax 1000)

http://imgur.com/iaxdy

>>82
http://imgur.com/mx4T2

Name: Anonymous 2012-07-16 2:02

multithreaded haskell factorial for the win: 2 million factorial in less than 19 seconds.

$ ./factorial 20000000 +RTS -s
37768210576
(~11MB of digits snipped)0000000000
   1,648,364,640 bytes allocated in the heap
     325,424,052 bytes copied during GC
      18,401,832 bytes maximum residency (29 sample(s))
       1,815,432 bytes maximum slop
             130 MB total memory in use (13 MB lost due to fragmentation)

                                    Tot time (elapsed)  Avg pause  Max pause
  Gen  0      1411 colls,  1410 par   12.93s    2.91s     0.0021s    0.0557s
  Gen  1        29 colls,    29 par    3.18s    0.54s     0.0185s    0.1733s

  Parallel GC work balance: 1.37 (81339519 / 59252792, ideal 8)

                        MUT time (elapsed)       GC time  (elapsed)
  Task  0 (worker) :    0.00s    ( 18.71s)       0.03s    (  0.22s)
  Task  1 (worker) :    1.65s    ( 15.39s)       2.62s    (  3.54s)
  Task  2 (bound)  :   14.96s    ( 15.62s)       2.79s    (  3.31s)
  Task  3 (worker) :    0.00s    (  0.00s)       0.00s    (  0.00s)
  Task  4 (worker) :    0.02s    ( 13.83s)       2.17s    (  5.09s)
  Task  5 (worker) :    0.02s    ( 13.84s)       2.87s    (  5.09s)
  Task  6 (worker) :    0.02s    ( 13.81s)       3.57s    (  5.12s)
  Task  7 (worker) :    0.06s    ( 13.82s)       3.71s    (  5.11s)
  Task  8 (worker) :    0.03s    ( 13.81s)       3.49s    (  5.12s)
  Task  9 (worker) :    0.05s    ( 13.81s)       3.53s    (  5.12s)

  SPARKS: 902 (788 converted, 0 overflowed, 0 dud, 0 GC'd, 114 fizzled)

  INIT    time    0.00s  (  0.00s elapsed)
  MUT     time   25.47s  ( 15.49s elapsed)
  GC      time   16.11s  (  3.44s elapsed)
  EXIT    time    0.00s  (  0.00s elapsed)
  Total   time   41.59s  ( 18.93s elapsed)

  Alloc rate    64,705,280 bytes per MUT second

  Productivity  61.3% of total user, 134.6% of total elapsed

gc_alloc_block_sync: 25914
whitehole_spin: 0
gen[0].sync: 0
gen[1].sync: 286

Name: Anonymous 2012-07-16 2:03

step

Name: Anonymous 2012-07-16 2:09

20000000
oops. copied the wrong first line, couldn't just copy the whole thing on account of my scrollback buffer being much smaller than 11MB.

Name: Anonymous 2012-07-16 3:08

haskell is shit

Name: =*=F=R=O=Z=E=N==V=O=I=D=*= !frozEn/KIg 2012-07-16 5:16

>>84
did you called for me?
_______
delenda terrum bullshit orbit est.

Name: Anonymous 2012-07-16 10:52

>>94
Ah, I haven't read it yet. I guess I should.

Name: Anonymous 2012-07-16 13:02

Total   time   41.59s  ( 18.93s elapsed)
What manner of witchcraft is this?

Name: Anonymous 2012-07-16 15:41

memset(y, '\0', LENGTH);
oh lawd imagine how slow this is

Name: Anonymous 2012-07-16 23:59

In APL:
×/⍳1000
In NARS2000, it's even shorter:
!1000x

Name: Anonymous 2012-07-17 0:01

ITT slow as fuck factorials that take several seconds to do what should only take a couple milliseconds at most.

Name: Anonymous 2012-07-17 2:19

>>107
Mine only took a couple milliseconds, don't know what you're talking about.

Name: Anonymous 2012-07-17 6:43

>>108
You are in the minority in this thread.

Name: Anonymous 2012-07-17 7:07

>>105
The entire thread is full of people described by Yossi here: http://yosefk.com/c++fqa/ctors.html#fqa-10.9 -- the people who think that they care about performance, but all they really care about is stroking their egos by participating in discussions, in this case on an anonymous textboard for added patheticalness.

Name: Anonymous 2012-07-17 8:47

>>8
332 bytes

#include<stdio.h>
int x[3][9999]={{1}},i=0,j,k,a,b;main(){while(i++^1001){for(x[2][0]++,j=0;x[2][j]>9;j++)x[2][j]%=10,x[2][++j]++;a=i&1,b=a^1;for(j=0;j<2568;j++)for(k=0;k<2568;k++)x[a][j+k]=x[b][j]*x[2][k]+(j?x[a][j+k]:0);for(j=0;j<2568;)x[a][j+1]+=x[a][j]/10,x[a][j++]%=10;}for(j=2567;j>=0;j--)printf("%d%s",x[i&1][j],j?"":"\n");}


$ gcc fact.c -O4
$ time ./a.out
real    0m7.641s
user    0m7.612s
sys    0m0.004s

Name: Anonymous 2012-07-17 10:23

ITT: Single threaded, no SSE

Nice failed priorities

Now go back to jerking off to hentai

Name: Anonymous 2012-07-17 10:52

>>112
>>13,15 is trivially parallelisable.

Name: Anonymous 2012-07-17 15:11

>>112
fuck you and die, faggot

Name: Anonymous 2012-07-17 15:45

>>112
jerking off to hentai
I never got the appeal of hentai. Ecchi, on the other hand, makes me cum massive buckets.
Maybe I am too innocent for actual hentai. ;_;

Name: Anonymous 2012-07-17 15:49

>>113
What is the sorcery with the "odd part of the swinging factorial"?

Name: Anonymous 2012-07-17 19:28

>>116
Premature "optimization" that, in Haskell at least, actually makes it a bit slower.

>>113
It still takes over a second to do what should take less than 10 milliseconds.

Name: Anonymous 2012-07-17 21:32

>>115
I understand. Hentai is nice when it's vanilla, though.

The good thing of ecchi is that it lets your imagination do whatever the hell it wants and I think it's hotter that way. While you're restricted to PLEASE INSERT YOUR OCHINCHIN INSIDE MY PUSSY KYAAAAAAAAAAA with hentai.

Name: Not >>115 2012-07-17 21:45

>>118
Yup.

Name: Anonymous 2012-07-18 14:12

>>117
be patient, geez
i bet you get your ANUS in a knot every time you have to wait in the line for a concert

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