标签云

微信群

扫码加入我们

WeChat QR Code


Everybody who does factorials using recursion is in a state of sin! Only recursive Fibonacci is worse. :-)

2018年09月26日50分02秒

You really need to make sure n and $n are not negative!

2018年09月25日50分02秒

Isn't there a decimal type in C#? Using strings? ouch! What about GMP (the GNU MultiPrecision library in C, used by many languages for bignums).

2018年09月25日50分02秒

The decimal type has finite range, only much greater than doubles. Strings are really easy to print. You could use base 256 which would simplify the code a bit but would be awkward to convert to string. Swings and roundabouts I guess.

2018年09月25日50分02秒

What's why I started to learn Ruby. It has built in Bignum.

2018年09月26日50分02秒

Alexander Prokofyev: Lots of languages have built-in bignum support.

2018年09月26日50分02秒

Funny, very non useful though.

2018年09月26日50分02秒

+1: very funny :-)

2018年09月25日50分02秒

there is so much truth here - who needs factorials in a C-like language anyway ?

2018年09月26日50分02秒

why not use an array instead?

2018年09月25日50分02秒

I was about to post: (defn fac [n] (reduce * (range 1 (inc n)))), but then I saw yours :)

2018年09月26日50分02秒

Well, that is elegant.

2018年09月25日50分02秒

I really dislike Delphi, but I love the fact this algorithm (unlike most of the others on this page) takes into account that factorial results don't change (and for large n are expensive) and you might as well only calculate a particular value once.

2018年09月25日50分02秒

Improved formatting

2018年09月26日50分02秒

If you want to enable the scala tail recursion optimisation for the traditional funtion do: def fact(n: Int,acc: BigInt): BigInt = if (n == 0) acc else fact(n-1,acc*n) . And call 5! with fact(5,1)

2018年09月26日50分02秒

C99 is fine with that.

2018年09月25日50分02秒

simple, but wrong ! Thats why I hate languages without builtin arbitrary precision arithmetic !

2018年09月26日50分02秒

blabla999: Not sure what you mean. This all integer based (this is not floating point).

2018年09月25日50分02秒