# GCD

• 16
• Locked
We continue our attempts to enter the cosmic space belonging to Planet Linear Chaos. Here's another task taken from a border-crossing interview, for young arrivals: find the greatest common divisor (GCD) of two positive integers.
You can't complete this task, because you're not signed in.
Angel Stefan
Level 23 , Sibiu, Romania
30 December 2021, 18:36
if(!( a > 0 || b > 0)) is a NO-NO but if( a < 1 || b < 1) is ok! 😡😤
1 March 2022, 04:53
Yes seems its scanning source rather than bytecode
Level 37 , Tampa, United States
8 August 2022, 20:08
``if(!( a > 0 || b > 0))``
This wouldn't work logically because if one is true and one is false then the logical OR would result in true and the logical NOT would result in the statement being evaluated to false. Take if I entered -500 and 70 a > 0 would be false while b > 0 would be true false || true would be true !true would be false.
Level 22 , Iasi, Romania
2 June 2021, 15:07
i knew about Euclidian algorithm so it took me 3 minutes to write a code to find GCD, and after that... it took me 20 minute to understand what they want from me on the second requirement. in my opinion this requirement was wrote very bad that's why i will say here what's the problem, if you don't want a direct suggestion don't read this. let's say you ask the user to enter 2 numbers (a and b) you may be tempted to write the code to react if the user will introduce a string or a double or a negative number, well they don't care about that. after the user input they just want you to check if a and b are bigger than zero (throw an exception if not), that's all. is true the program will throw an exception by itself if you try to enter a string but still... i think that point is very bad wrote and after you find the best way to find a GCD is really frustrating to lose so much time for that exception...
1 March 2022, 04:50
Also it requires you explicitly throw an exception of either cant be converted to positive integer rather than using the Integer.parseInt and the throws Exception which is already on the main method.
Zourkas
Level 16 , Frankfurt, Germany
18 March 2021, 13:45
Why always return first? If second is bigger than first why should we return first?
Samuel Michael
Level 16 , Stafford, United States
Expert
25 February 2021, 19:40
Created 3 arrays, one to populate a%i==0, other b%i==0, used third array to populate with duplicate elements with nested for loop, max of third array is GCD https://en.wikipedia.org/wiki/Greatest_common_divisor
Ryan
Level 16 , Ashburn, United States
20 July 2020, 20:58
Two extremely helpful hints: 1. You don't need to catch the exception, you just need to throw one if one of the numbers is not positive (zero is not positive). 2. Ignore the part of requirement 2 that says "If the entered lines can't be converted to positive integers", it should say "If the entered lines are not positive integers (zero is not considered positive)". If you follow this requirement down the converted path you will try Math.abs() and that will mess everything up. The rest of this task is straight forward as long as you interpret requirement #2 with the hints above.
1 March 2022, 04:56
Yes, as we were all no-doubt doing Math.abs() etc for 'cant be converted' so after this one I'm just writing my code, making certain it works, then if I don't get it on first submit I look at solution for things that are almost impossible to predict based on requirements. Once I see it, if its anything other than tiny thing, I redo it from memory so it sticks.
Mayu
24 June 2020, 08:02
The second requirement was confusing, I approached it assuming that the input would not be an integer.
Level 27 , Spanish Town, Jamaica
16 June 2020, 01:08
This one honestly made me sad. I coded finding the GCD without googling first. I was fairly proud of my approach, as it was 100% self derived. even created my own exception class, felt pretty darn clever. then failed the 2nd requirement about a zillion times. As someone mentioned, the explanation there needed to be more specific. it is hard to gauge what's wrong when the required output looks exactly like your own.
Trevor
Level 23 , Phoenix, United States
28 December 2019, 01:32
There needs to be a much better explanation as to what is expected by throwing an exception on this one.
Felix
Level 19
6 April 2020, 04:15
I totally agree here. It took me ages to figure out that 0 isn't considered a positive number...
Isaiah Burkes
Level 16 , Tampa, United States
25 December 2019, 22:00
What did you return if there is no GCD? For example if the inputs were: 10 3
Roman
Level 41
26 December 2019, 10:56
1
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
15 February 2021, 01:47