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 http://ideone.com (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
7
5
10
1
2
75
99
100

in the input field, and if you wrote it correctly, it should show
120
3628800
1
2
24809140811395398091946477116594033660926243886570122837795894512655842677572867409443815424000000000000000000
933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
after changes...............

http://liveworkspace.org/code/3mQTbm$7
its not calculating 100! now!!!!!!!!!!!!!!!!!!!!!!
i changed it a bit more.......................

http://liveworkspace.org/code/3mQTbm$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.

http://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.80%29.aspx


These online judge problems are often not straight forward.

Spotted another thing in your code:
1
2
3
4
5
for(j=1,k=1; j<=t; j++,k++)
{
  std::cout<<factorial(testcase[k-1]);
  std::cout<<"\n";
}


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

1
2
3
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 :)

Edit:

http://www.cprogramming.com/tutorial/bitwise_operators.html
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 ....it 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