Codechef Question

please can you tell me how to write program for Small Factorial

i was written code by long double then also it shows wrong because it cannot calculate till 100!, it write in scientific notation.

Hello @vipin123,

Try reading this editorial.

It would help alot… :slight_smile:

http://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial

All the best…!!

Happy programming… :slight_smile:

1 Like

There are various ways to solve it. If you know python then its very easy. But in case of C++ you have to use the big Int library. But the best method for beginners who dont know about BigInt is that you store the answer of factorial in an array. For eg. you want to calculate 100 ! then save 100*99 as 9 9 0 0 in an array. Now multiply this array elements with 98 and so on till 2 upgrading values in each index with proper carry. Have a look at my solution for doubts if any. Thus the summary is you need to make a multiplication program of your own to store larger results in an array. Think its easy ! http://www.codechef.com/viewsolution/5637761

2 Likes

Well if you have basic idea about programming in Java you can have a look at BigInteger class. It would be really helpful in such cases and by using BigInteger you can code in same way as you would have coded for a small integer. Hope this helps.

2 Likes

can anyone give me important link for BIGINTEGER class tutorial in JAVA…thanks in advance…

This link provides explanation of every method with example. You need to click on the method to see the example.

1 Like

Just to add to flappy’s answer,not only Java can deal with large numbers efficiently,I solved the small factorials problem with only 3 lines of code using Ruby:

    gets.to_i.times {
puts (1..gets.to_i).inject(&:*);
}

Haskell can also do well with large numbers.

2 Likes