Не проходит последний пункт. Решение Громозкое, так как не помню школьную программу.
package com.javarush.task.task14.task1420;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
НОД
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(reader.readLine());
if (a <= 0) throw new NullPointerException();
int b = Integer.parseInt(reader.readLine());
if (b <= 0)throw new NullPointerException();
int twoA = 1;
int threeA = 1;
int fiveA = 1;
int wholeA = 0;
while (true){
while (true){
if (a % 2 == 0 && b % 2 == 0){
a = a / 2;
b = b / 2;
twoA *= 2;
}else {
if (a % 2 == 0){
a = a / 2;
}
if (b % 2 == 0){
b = b / 2;
}
break;
}
}
while (true){
if (a % 3 == 0 && b % 3 == 0){
a = a / 3;
b = b / 3;
threeA *= 3;
}else {
if (a % 3 == 0){
a = a / 3;
}
if (b % 3 == 0){
b = b / 3;
}
break;
}
}
while (true){
if (a % 5 == 0 && b % 5 == 0){
a = a / 5;
b = b / 5;
fiveA *= 5;
}else {
if (a % 5 == 0){
a = a / 5;
}
if (b % 5 == 0){
b = b / 5;
}
break;
}
}
wholeA = a;
break;
}
int nod = twoA * threeA * wholeA;
System.out.println(nod);
reader.close();
}
}