0hc.net Menu
Home Overview
About Menu
Impressum

Programming Style - Factorial

Not difficult at all: In Haskell: main = print $ fac 1000 fac n = product [1..n] So we write the factorial function in Ruby: def fac(n) n==1 ? 1 : n * fac(n-1) end puts fac(5) And it does work! puts fac(1000) Or not? Just a SystemStackError is printed. In Perl: sub fac {my ($n) = @_; $n==1 ? 1 : $n * fac($n-1)} print fac(1000); Result: 1.#INF In Python: def fac(x): if x>1: return(x*fac(x-1)) else: return(1) print fac(1000) Result: RuntimeError And the same algorithm in Haskell: main = print $ fac 1000 fac n | n > 1 = n * fac (n-1) | otherwise = 1 And the right result is printed. But we can optimize the Ruby program via tail recursion: def fac(n) mul_fac(1,n) end def mul_fac(m,n) n==1 ? m : mul_fac(m*n,n-1) end puts fac(1000) And it does work! puts fac(2000) Or not? Just a SystemStackError is printed. We know that already... Conclusion: Write the program in the right style - than the right results will be provided: So a correct Ruby version: def fac(x) y=1; (1..x).each{|i| y=y*i}; y end puts fac(2000) Or: module Main where import System main :: IO () main = getArgs >>= \li -> print $ product [1..(read (li!!0) :: Integer)] If you really want to calculate large factorials, then the best possibility is a prime swing algorithm. Just the fac 2000 result: 33162750924506332411753933805763240382811172081057803945719354370603807790560082 24002732308597325922554023529412258341092580848174152937961313866335263436889056 34058556163940605117252571870647856393544045405243957467037674108722970434684158 34375243158087753364512748799543685924740803240894656150723325065279765575717967 15367186893590561128158716017172326571561100042140124204338425737127001758835477 96899921283528996665853405579854903657366350133386550401172012152635488038268152 15224692099520603156441856548067594649705155228820523489999572645081406553667896 95321014676226713320268315522051944944616182392752040265297226315025747520482960 64750927394165856283531779574482876314596450373991327334177263608852490093506621 61014445970941270782131373256383157230201994991495831647094277447387032798554967 42986088393763268241524788343874695958292577405745398375015858154681362942179499 72399813599481016556563876034227312912250384709872909626622461971076605931550201 89513558316535787149229091677904970224709461193760778516511068443225590564873626 65303773846503907880495246007125494026145660722541363027549136715834060978310749 45282217490781347709693241556111339828051358600690594619965257310741177081519922 56451677857145805660218565476095237746301667942248844448579834980154803262082989 09658573817518886193766928282798884535846398965942139529844652910920091037100461 49449915828588050761867924946385180879874512891408019340074625920057098729578599 64365065589561241023101869055606030878362911050560124590899838341079936790205207 68586691834779065585447001486926569246319333376124280974200671728463619392496986 28468719993450393889367270487127172734561700354867477509102955523953547941107421 91330135681954109194146276641754216158762526285808980122244389024867718205495941 57519917012717675717874958616196659318788551418357820926014820717773317353960343 04969082070589958701381980813035590160762908388574561288217698136182483576739218 30311841471913398689284234400077924669120976673165143349443747323563657204884447 83318549416930301245316762327453678793228474738244850922831399525097325059791270 31047683601481191102229253372697693823670057565612400290576043852852902937606479 53345817966612383960526254910718666386935476610845504619810208405063582767652658 94923932495196859541716724193295306836734955440045863598381610430594498266275306 05423580755894108278880427825951089880635410567917950974017780688782869810219010 90014835206168888372025031066592206860148364983053278208826353655804360568678128 41692171330471411763121758957771226375847531235172309905498292101346873042058980 14418063875382664169897704237759406280877253702265426530580862379301422675821187 14350291863763634030017325181826207603974736959520264263236414544685111342720215 04583838510101369413130348562219166316238926327658153550112763078250599691588245 33457435437863683173730673296589355199694458236873508830278657700879749889992343 55556624068283476378468518384497364887395247510322422211056120129582965719136810 86938254757641188868793467251912461921511447388362695916436724900716534282281526 61247800463922544945170363723627940757784542091048305461656190622174286981602973 32404652020199281385488268195100728286970107073750092766648750217477537274235150 87482467202741700315811228058961781221607474379475109506209385566745812525183766 82157712807861499255876132352950422346387878954850885764466136290394127665978044 20209228133798711590089626487894241321045492500356667063290944157937298674342147 05072135889320195807230647814984295225955890127548239717733257229103257609297907 33299545056388362640474650245080809469116072632087494143973000704111418595530278 82735765481918200244969776111134631819528276159096418979095811733862720608891043 29452449785351470141124421430554860896395783783473253235957632914389252883939862 56273242862775563140463830389168421633113445636309571965978466338551492316196335 67535513840342580416291983782226690952177015317533873028461084188655413832917195 13321178957285416620848236828179325129312375215419269702697032994776438233864830 08871530373405666383868294088487730721762268849023084934661194260180272613802108 00507821574100605484820134785957810277070778065551277254050167433239606625321641 50048087724030476119290322101543853531386855384864255707907953411765195711886837 39880683895792743749683498142923292196309777090143936843655333359307820181312993 45502420604456334057860696247196150560339489952332180043435996725662392719643540 28720554750120798543319706747973131268135236537440856622632067688375851327828962 52333284341812977624697079543436003492343159239674763638912115285406657783646213 91124744705125522634270123952701812704549164804593224810885867460095230679317596 77555810116799400052498063037631413444122690370349873557999160092592480750524855 41568266281760815446308305406677412630124441864204108373119093130001154470560277 77372437806718889977085105672727678124719883285769584421758889516046786820481001 00478164623582208385324881342708340798684866321627202088233087278190853788454691 31556021728873121907393965209260229101477527080930865364979858554010577450279289 81460368843182150863724621696787228216934737059928627711244769092090298832016683 01702734202597656717098633112163495021712644268271196502640542282317596308744753 01847194095524263411498469508073390080000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000
0hc.net    © 2001-2014 Harald Wolfsgruber