Computer Science Canada Quick problem |
Author: | Panphobia [ Tue Nov 13, 2012 11:40 pm ] | ||
Post subject: | Quick problem | ||
Umm I do not think this is too big of a problem but my program has an error where it divides by 0. I have set in things that should make this not the case, what could be the problem now?
|
Author: | DemonWasp [ Tue Nov 13, 2012 11:56 pm ] |
Post subject: | RE:Quick problem |
This problem is pretty subtle, and it took me a moment too. The problem is that int isn't large enough to hold values like 40!, which is approximately 8 x 10^47. Even if you use long, you can't handle values above 66! ~= 5.4 x 10^92. There are two ways to handle this problem. First, you could use Java's BigInteger class, which can handle arbitrary-value integers, including 100! x 100!. Second, you could take a good long look at the formula for combinations (what you've implemented in combo()) and try to come up with a version that doesn't generate numbers that are quite so huge. Even then, 100 choose 50 is more than can be stored in the long data type. You could use double, which can store substantially larger numbers, if you're willing to sacrifice some accuracy, or you can use BigInteger. |
Author: | Panphobia [ Wed Nov 14, 2012 12:52 am ] |
Post subject: | RE:Quick problem |
Thank you man, all I did was cast the return statement of the combo function to (int) made fac return a double, worked fine ![]() |
Author: | Tony [ Wed Nov 14, 2012 1:04 am ] |
Post subject: | RE:Quick problem |
now, consider that you've just calculated the value of 10! and your for-loop advances to 11. To calculate fac(11), you compute 11 * fac(10)... which you already knew the answer for just moments before! but now have to compute fac(10) (again) and fac(9) (also again) and fac(8)... |
Author: | Panphobia [ Wed Nov 14, 2012 1:30 am ] |
Post subject: | RE:Quick problem |
yea i understand what you are trying to say tony, but efficiency was not my aim lol, i was just solving some project euler ![]() |
Author: | Insectoid [ Wed Nov 14, 2012 8:40 am ] |
Post subject: | RE:Quick problem |
A lot of project euler is about efficiency. Many of the problems are designed to be impossible or take ridiculously long to execute with naive brute-force algorithms. |
Author: | mirhagk [ Wed Nov 14, 2012 12:54 pm ] |
Post subject: | RE:Quick problem |
I'm doing euler as well, trying to do it in haskell. Essentially every solution can be like 2 lines of code, but it's RIDICULOUSLY hard to optimize it. I can solve the first few problems, but later ones quickly become impossible. |
Author: | bl0ckeduser [ Wed Nov 14, 2012 9:56 pm ] |
Post subject: | Re: Quick problem |
Is this the ProjectEuler thing where you have to compute big factorials ? I can strongly recommend writing custom code that does it using "pencil-and-paper" methods. It's fun and there are skills to learn. |
Author: | md [ Wed Nov 14, 2012 11:12 pm ] |
Post subject: | Re: RE:Quick problem |
Panphobia @ 2012-11-14, 12:52 am wrote: Thank you man, all I did was cast the return statement of the combo function to (int) made fac return a double, worked fine
![]() That doesn't solve your issue, it just hides it so that you don't notice that your answers are wrong. |
Author: | Panphobia [ Wed Nov 14, 2012 11:23 pm ] |
Post subject: | Re: RE:Quick problem |
md @ Wed Nov 14, 2012 11:12 pm wrote: Panphobia @ 2012-11-14, 12:52 am wrote: Thank you man, all I did was cast the return statement of the combo function to (int) made fac return a double, worked fine
![]() That doesn't solve your issue, it just hides it so that you don't notice that your answers are wrong. |
Author: | Insectoid [ Thu Nov 15, 2012 12:20 am ] |
Post subject: | RE:Quick problem |
That's the wrong attitude to have in computer science. |
Author: | Tony [ Thu Nov 15, 2012 12:39 am ] |
Post subject: | Re: RE:Quick problem |
Insectoid @ Thu Nov 15, 2012 12:20 am wrote: That's the wrong attitude to have in computer science.
well... not necessarily. There's a trade-off between ideal solution and fast (to develop) solution. If one throws just enough code until something works, then you are correct ? that is bad news. But if one understands exactly what they are trading off, then it's a fair trade. |
Author: | Panphobia [ Thu Nov 15, 2012 12:44 am ] |
Post subject: | RE:Quick problem |
What i was getting at, is the question does not ask for efficiency, so I was looking at it from the perspective, "get it done as fast as possible", not spend more time to make it more efficient ![]() |