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.
Comments(15)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
MayuLevel 18 , Toronto
24 June, 08:02
The second requirement was confusing, I approached it assuming that the input would not be an integer.
Adrian Level 17 , Spanish Town
16 June, 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.
TrevorLevel 23 , Phoenix
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.
FelixLevel 19
6 April, 04:15
I totally agree here. It took me ages to figure out that 0 isn't considered a positive number...
Isaiah BurkesLevel 16 , Tampa
25 December 2019, 22:00
What did you return if there is no GCD? For example if the inputs were: 10 3
RomanLevel 41
26 December 2019, 10:56
1
SvenLevel 19 , Hanover
16 August 2019, 14:28
I don't see the problem here, but I always get a Time out! when I try to do the validation... Any suggestions on why that happens?
AlexLevel 41
16 August 2019, 14:41
Sounds like you may have an infinite loop in your code. Please consult the Help section with your code to get a definite answer.
AhmedLevel 23 , Amsterdam
26 May 2019, 17:09
Waa what tough exercise! I just googled the answer on Google about how to calculate the GCD and I came up with an answer that uses recursive. After copying and pasting I passed the verification but I have no clue how the code that I copied works :-)
oli blaustromLevel 18 , Luzern
25 February, 12:42
Check out euclidean algorithm, it's really cool number theory and also one of the first things I learned in CS.
Guadalupe GagnonLevel 37 , Tampa
4 December 2018, 17:57
Some added notes to the task for (hopefully) further clarification (my thoughts after passing it, not from the developers): 1) The program does not need to display anything other than the greatest common divisor. This means that no message needs to be displayed if an exception is thrown. (where the user doesn't enter a number or the number is not positive) this is what i used: throw new NumberFormatException("Number needs to be positive"). 2) no need to use any try-catch statements for invalid user data. If an exception is thrown it will crash the program, but i passed just fine not catching these exceptions. 3) example inputs and outputs from my program that passed. The first two lines are user input and the third line being the program output (noted in the first example): A) (enter 2 positive numbers) 500 // user input 50 // user input 50 // output from program of the greatest common divisor B) (enter a negative number) -50 Exception in thread "main" java.lang.NumberFormatException: Number needs to be positive at com.codegym.task.task14.task1420.Solution.getPositiveInt(Solution.java:71) at com.codegym.task.task14.task1420.Solution.main(Solution.java:15) Process finished with exit code 1 C) (enter a positive number then a string) 20 twelve Exception in thread "main" java.lang.NumberFormatException: For input string: "twelve" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at com.codegym.task.task14.task1420.Solution.getPositiveInt(Solution.java:69) at com.codegym.task.task14.task1420.Solution.main(Solution.java:16) Process finished with exit code 1
franLevel 14 , Cordoba
13 March 2019, 18:48
There are very useful notes provided by Guadalupe in order to acomplish this task. I will add to these notes that the 0 (zero) number is not a postive number. Cheer up friends!!!!!!
BerksonLevel 17 , Fortaleza
21 June 2019, 14:08
Man the same to me I hate the 0 number hahahaha... finished.
Isaiah BurkesLevel 16 , Tampa
25 December 2019, 22:00
What did you return if there is no GCD? For example if the inputs were: 10 3
Guadalupe GagnonLevel 37 , Tampa
26 December 2019, 14:05
1