Factorial for integers (imperative)

Oforth examples. Feel free to post your own code.

Factorial for integers (imperative)

Postby Franck » 07 May 2015 13:13

The code :

Code: Select all
: fact { | i | 1 swap loop: i [ i * ] }


- A local variable, i, is declared : it is needed for the loop.
- Then #fact pushes 1 on the stack and swap it with the integer on top of the stack.
- This integer is used by #loop: so the loop will run from i = 1 to i = this integer.
- Into the loop, i is pushed, then multiplicated by the top of the stack.
- At the end, fact is calculated on the top of the stack.

Usages :

>10 fact .s
[1] (Integer) 3628800
ok
>1000 fact .s
[1] (Integer) 4023872600770937735437024339230039857193748642107146325437999104299385123986
290205920442084869694048004799886101971960586316668729948085589013238296699445909974245040
870737599188236277271887325197795059509952761208749754624970436014182780946464962910563938
874378864873371191810458257836478499770124766328898359557354325131853239584630755574091142
624174743493475534286465766116677973966688202912073791438537195882498081268678383745597317
461360853795345242215865932019280908782973084313928444032812315586110369768013573042161687
476096758713483120254785893207671691324484262361314125087802080002616831510273418279777047
846358681701643650241536913982812648102130927612448963599287051149649754199093422215668325
720808213331861168115536158365469840467089756029009505376164758477284218896796462449451607
653534081989013854424879849599533191017233555566021394503997362807501378376153071277619268
490343526252000158885351473316117021039681759215109077880193931781141945452572238655414610
628921879602238389714760885062768629671466746975629112340824392081601537808898939645182632
436716167621791689097799119037540312746222899880051954444142820121873617459926429565817466
283029555702990243241531816172104658320367869061172601587835207515162842255402651704833042
261439742869330616908979684825901254583271682264580665267699586526822728070757813918581788
896522081643483448259932660433676601769996128318607883861502794659551311565520360939881806
121385586003014356945272242063446317974605946825731037900840244324384656572450144028218852
524709351906209290231364932734975655139587205596542287497740114133469627154228458623773875
382304838656889764619273838149001407673104466402598994902222217659043399018860185665264850
617997023561938970178600408118897299183110211712298459016419210688843871218556461249607987
229085192968193723886426148396573822911231250241866493531439701374285319266498753372189406
942814341185201580141233448280150513996942901534830776445690990731524332782882698646027898
643211390835062170950025973898635542771967428222487575867657523442202075736305694988250879
689281627538488633969099598262809561214509948717012445164612603790293091208890869420285106
401821543994571568059418727489980942547421735824010636774045957417851608292301353580818400
969963725242305608559037006242712434169090041536901059339838357779394109700277534720000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000
[2] (Integer) 3628800
ok
>

Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Return to Oforth examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron