Those who used this way of solving the task, could you explain the logic/flow of this? I understand this is recursion, but switching the x & y and diving by the remainder, etc. I just can't follow it. Much appreciate it.
What kind of trick is that, and where did you get it?:)
I don't know about such too clever tricks... and I don't like them... and I'm sorry, but I have no idea what it's doing:(
I solved this task with a very down to earth thinking:
I searched the divisors of the first number, in a cycle.
if(number1%i==0) then put them into a list.
I also looked for the divisors of the second number and put them in another list.
Then I used nested loops to compare the two lists to see if there were any common elements, and put them into a third list,
and finally I searched the largest element of this third list.
Well, maybe it's not very efficient solution... but worked:)
I hope someone will know the answer to your original question, I'm curious too.
this was in the community help section where people posted asking for help. actually, quite a number of them had this method. I don't know if they all know this little trick or just copied/pasted?? don't know. I hope someone answers.
That's the Euclidean algorithm. So it's from a really smart guy ;) You can read about it on wikipedia with coding examples including this recursive attempt. I used it as well cause I remembered it from school (but had to look it up again in the web to remeber exactly how it works). You can find lots of coding examples in the web (recursive and non recursive - the latter may be a good starting point to explore it more).
facinating stuff. Again, thanx Thomas for the reply/input. I guess I could apply it if I ever need to get a GCD in the future. As far as fully understanding the logic, I will have to save for a later time so I can continue to cover CG's material.
The logic behind the algorithm is very simple. Take two numbers, a and b, find the remainder after division, then make 'a' equal 'b' and 'b' equal the remainder. Repeat those steps until the remainder equals 0, and the answer will be a:
int a =/*any number*/;int b =/*any number*/;int remainder =1;// set remainder to anything other than 0while(remainder !=0){// loop while remainder does not equals 0
remainder = a % b;// find remainder after division (modulo operator)
a = b;// make a equal b
b = remainder;//make b equal remainder}// at this point the correct answer is 'a'
Personally, recursion is just a parlor trick that looks cool but 99.99% of the time is inferior to a loop. This is because every method call takes up memory on the stack and having too many unresolved methods can lead to a massive amount of memory being used very easily. Too many method calls on the stack will result in a stack overflow error. Take this code as an example:
Run the code as is and it will enter an infinite loop. You could leave your computer for a couple hours and when you come back the loop would still be going. Comment out line 3 and run the program again. In less than a second a StackOverflowError will occur, meaning the program completely filled all memory allocated to the stack, and the program will end.
0
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.