Codes between Compilers

Pages: 12
Thankyou sir.........

I tried replacing void with int,

I removed conio and everything related to it
I put std::infront of cin and cout

.......but the site still says the output is wrong
And the for loop? Do you change that?

Can you update the code in the LiveWorkspace as well - then we can see what you have done exactly.
Once you get your program to compile with a C++ compiler, put it on (which happens to be an online compiler that allows arbitrary input) and give it the input similar to what your contest gives you.

For example, run it with

in the input field, and if you wrote it correctly, it should show
after changes...............$7
its not calculating 100! now!!!!!!!!!!!!!!!!!!!!!!
i changed it a bit more.......................$25

but it still wont calculate 100!
This is where you have to be aware of the limits of your data types - you need a different method because with the standard types you are not going to get anywhere near factorial 100. Even a 64 bit unsigned int only gives 20 significant figures.

These online judge problems are often not straight forward.

Spotted another thing in your code:
for(j=1,k=1; j<=t; j++,k++)

k is the only variable used, so it could be written like this:

for(k=0; k < t; k++) { //I just edited that
      std::cout<<factorial(testcase[k]) << std::endl;  

You should get used to using arrays from zero and not subtract 1 everywhere.
Last edited on
A different method could be to store numbers as bits, then do multiplication by bit shifting. Shifting left by 1 is the same as multiplying by 2. So do code to factor your multiplying number into 2's.

Example: 50 * 33

= 50 * 2 * 2 * 2 * 2 * 2 + 50
= (50 << 5) + 50

You can use the log function to see how factors of 2 there are in a number, rounding down.

Any way I am just writing this off the cuff, so I had better leave you in the expert hands of cubbi - as I haven't actually done it. (Maybe I should try it myself !!)

Sorry I didn't mention this earlier - I was focussing on getting your code to compile & didn't ask what the actual question was.

Hope all goes well :)

Last edited on
ok, you lost me there..........i have no clue whatsoever about bit shifting multiplication (Remember, i'm a student who started programming about 7 months ago)

i still dont know how to get 100!.................

in tc++ it worked.......

i tried using long long, unsigned __int64, and unsigned long long dint work.

but it worked for lond double.............and the site wont accept it.
As I said unsigned long long (a 64 bit number) will only give 20 significant places - as cubbi's post shows you need a lot more than that.

long double will hold a big enough number, but will only give you 18 significant places, so you need another method. As I said these online judge things aren't that easy.

ok, you lost me there..........i have no clue whatsoever about bit shifting multiplication (Remember, i'm a student who started programming about 7 months ago)

Did you see the link I posted? I added it afterwards. It explains what bit shifting means, & I gave a quick example.

You can google to get more examples - hell there is probably the answer to the question somewhere.

Any way I am going to bed - it's 04:45AM here. Have a go at the new method, then cubbi or someone else may help you.

Good Luck

..oh, right, LWS allows input too now.
Calculating factorials up to 100 is not a one-liner program in C++, that's what makes it a competition, I guess (in practice, it's trivial with boost-1.53 or with other multiprecision libraries)

Or you could just hardcode all 100 possible answers as an array of strings.
Topic archived. No new replies allowed.
Pages: 12