I am a slow learner.

So, I was totally failed for my first programming competition . But that was a  very very good experience that made me finally believe that I am a slow learner. I have a very slow skill on logic. But, along those spesification comes a big curiousity wich sometimes could be a tragic ending. Yup, I can’t hold the curiousity when I can’t solve something, but I don’t have ‘that quality’ to support my curiousity. Did you get this? Owh just keep reading if you don’t mind…

It was not an individual competition. I did this with my two college senior. And I was never be able to contribute even a single answer.. 😦
So this is one of question from the “problem solving” session:

Write a program to show all the non-negative solutions from this equation:

x+y+z=50

where:

x >= 0, y >=0,  z >= 0


We were given 60 minutes of time to solve this.
It wasn’t so hard for me to understand the problem, this is not my first time to see this kind of problem. And of course I think, for those who have background in math and programming maybe see this problem as easy as an elementary school math problems :).
So I should make a program that can write the output of all posibilities that can be the solutions for that equation. One of the solution is 0+0+50=50
That , actually, is the key to the algorithm. But instead of thinking how the algorithm could increment each variables at one iteration until it reach 50 and then show the output only when it meets the solution, I was thinking how am I gonna randomized each variables until the variables could complete the x+y+z=50 problem. Then why I count on random function? Because I think, computer would have no objection if I told it to keep randomizing variables until it’s meet the solution :).
And now I can clearly see this reason as a stupid reason.
Then, with that silly algorithm, you know what could happen when it run, wich at that time I haven’t noticed yet. Yes, the computer do the iteration like forever! (FYI: I did limit the random to 50,but then again it was really stupid :p).

Both of my senior also thought the solution of the algorithm should use random function. After a few minutes they came with a solution that made want to post this :p . They gave me the easiest algorithm and of course without random function.

for (int x=0; x<=50; x++) {
for (int y=0; y<=50; y++){
for (int z=0; z<=50; z++){
if ((x+y+z) == 50 ) {
System.out.println(x+"+" +y+"+"+z+"=50");
}
}
}
}

Yeah, you can laugh now. 🙂

There is no need to use the random function to solve this kind of problem. The computer just need less than a second to run the program. So the key to the problem is how to increment each three variables.

What happen when this algorithm run is.

  1. At first all variables were set to zero.
  2. Then it start the iteration by incrementing only for variable z.
  3. The z is now 1. Variables values for this iteration to be tested is 0+0+1 . This not meet the condition. and the z still not reach 50.
  4. It repeat the step 3 until z reach 50.
  5. voila. so the z is now 50 thats mean 0+0+50 . And it meet the condition so it shows the output of current values.
  6. What happen after  this is, the y is now start incrementing.
  7. The z is set back to zero.  And the x is still zero, because it doesn’t start incrementing yet.
  8. so y is now 1. Variables values for this iteration to be tested is 0+1+0. This is not meet the condition. And the z (yes, the z) still not reach 50.
  9. It repeat the step 8 until z reach 50. (yes, until the z is 50 and the y is always 1)
  10. BUT,when the z is 49. Variables values for this iteration to be tested is 0+1+49. This meet the condition, then show the output. After this, z still incrementing and it tested for 0+1+50 , but this not meet the condition.
  11. Then, the z is set back to zero again and the y is now 2.
  12. It all happen in the same way, when the z is 48, the values should be like 0+2+48 . And of course this again meet the condition.
  13. I shouldn’t do the total explanation, should I? :p

So, its amazing that computer did all the calculation for only less than a second.  When it is embarassing to me  as a 2,5 year college student who study computer science who forget this simple problem. I feel soo ashamed of my self.

But I learned so much things from this:

  • I should see things down-up.. not up-down.
  • I really should do much more coding..
  • Computer is not my slave, it is my tool. :))
  • Solving any kind of problems is not good with an empty stomach and less sleep.
  • I AM A SLOW LEARNER.. 🙂

thanks to Kang Ammar and Kang Dwi !

Leave a comment