开发者

How long it would take you to make a working code to print out factorial of very very large numbers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I was given this coding question in interview:

given a very very large number (say more than long or any in-built types) print out its factorial. you can not assume a max limit anywhere in the program.I had to make a working code on computer and during interview.

I am really curious, how long on average would it take for others?

this is subjective question but an average will set some ballpark figures and a benchmarks for such a coding question开发者_StackOverflow.

What I did?

I chose C and represented number by a linked list of characters (containing a single digit). though perhaps it can be made more efficient to store chunks in int/long and do int arithmetic than store it in chunk of characters. I took 2 hours and spat out a code with things in place, major fns coded, but then interviewer said she wanted a completely working one and asked me to do it offline and mail it to her.


The good solution is to write a BigInt class that supports addition and mutilplication only. The number shouldn't be kept in base 10, rather in base 10000, i.e. each digit is a number 0-9999. Writing this is about 50-60 lines of code which should be relatively quick. I would also go with vector rather than list

Of course if you're not allowed to use an existing big int class.


Check the following links

  • calculate-the-factorial-of-an-arbitrarily-large-number-showing-all-the-digits
  • http://www.daniweb.com/software-development/cpp/code/216490
  • calculating-factorial-of-large-numbers-in-c


I'd start by suggesting a straight factorial in Common Lisp as Lisp already supports arbitrary precision arithmetic.

Assuming "no max limits anywhere" is a bit disingenuous since the computer has limited memory / disk space in any case but I understand the general gist.

Barring those two points of argument, you need to implement some sort of ADT for an arbitrary-sized number like Armen suggests.


To what accuracy? My first impulse would be to simply use Stirling's approximation.


There's no way I'm going to implement factorial for longs (bigger than ints) not to say above that.. It's totally pointless. No matter how fast my biginter library is.

32bit -> 4G, product of the numbers just from 1G till 4G.. well, lets just get a quick (under)estimation for the number of digits: 3G * 9 = 27G. rough estimation for the storage for that: 2^10=1024, so for 3 digits you need 1.25 bytes.. that's 11.25G. remember, this was a serious underestimation...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜