You are not logged in. Please login at to post your questions!


HEY THERE (C++ coder)!Help me find error in Begginer level problem !

I am getting a Wrong answer , I am unable to detect whats wrong , Please help !

Link to my solution :

Link to the very simple problem :

asked 20 Nov '18, 09:08

prembharwani's gravatar image

accept rate: 0%


For this particular question, switch to python which has in built support for large numbers :)

(20 Nov '18, 11:17) vijju123 ♦♦4★

As you know factorials grow very fast and integers can store values only upto a certain range so you can see your program would result in overflows.You can try your ans on for ex 25.Your code will produce 2076180480 which is incorrect.Hence WA. PS:you can write a string multiplication code in c++ or can use BigInteger class of Java if u want. Happy Coding!!


answered 20 Nov '18, 09:26

ram_24's gravatar image

accept rate: 37%

Okay , Thanks !

(20 Nov '18, 09:33) prembharwani2★

The solution you have written would work correctly if there was no range for data types in c++. But data types come with their ranges and for saving the value of 100! which has 154 digits in its decimal form can't be saved in any data type available in c++. To counter this problem you can do a number of things like: 1. switch to python or java for this question(don't get me wrong here, I do consider c++ is the best language for competitive but for this question some other language will give you a more simple way to solve the problem). 2. Use some kind of optimisation to stop the code when the value gets too large to be calculated. 3. The 2nd option may look weird but you can actually do this, one way is to use an array. the way came up with was to make an array of the max number of digits in decimal expansions in the value, the value of elements in this array can be used to multiply all the values. Like for each multiplication you have to do it for each digit and then take into consideration the carry after multiplication of the digit for multiplication with the next digit.

Another thing you may do is to use a vector and as save the value of the multiplication in each term as long as the value of that element doesn't cause overflow.

I suppose I have provided you with a basic idea to use to solve this question.


answered 21 Nov '18, 02:12

awesomesarthak's gravatar image

accept rate: 50%

I wish I could hit that UpVote button (Sadly I dont have enough rep. pts)

Thanks Though !

(21 Nov '18, 10:35) prembharwani2★

You can accept his answer.. he will get 15 reputation... Just click on tick button @prembharwani
And giving answer to your question... As rightly said by everyone else you need string multiplication and stuff.. But with python , simple code will work.. as it takes care of big integers (which can't be stored in simple data types) on its own...

(22 Nov '18, 02:50) l_returns4★

Whoever called 100! a "small factorial" must have been joking. :)


answered 20 Nov '18, 09:48

eugalt's gravatar image

accept rate: 4%

Try the concept of unbounded integers, which you can implement using array.


answered 22 Nov '18, 02:45

rishichawla14's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 20 Nov '18, 09:08

question was seen: 242 times

last updated: 22 Nov '18, 09:09