Determining finite decimal numbers (with.Java)
This is an article about the problem of “Identifying finite decimal numbers (with.Java)”.
As I solve coding test problems, I look back on the problems I solved and look into different solution methods to learn more.
Let’s look at the problem first.
problem
A decimal number that has only a finite number of digits after the decimal point and does not continue is called a finite decimal number.
When converting a fraction to a decimal, we want to determine whether the fraction can be expressed as a finite decimal number.
The conditions for a fraction to become a finite decimal number are as follows.
When expressed as an irreducible fraction, the only prime factors in the denominator must be 2 and 5.
When two integers a and b are given as parameters, complete the solution function so that it returns 1 if a/b is a finite decimal number and 2 if it is an infinite decimal number.
Restrictions
- a, b are integers
- 0 < a ≤ 1,000
- 0 < b ≤ 1,000
Input/Output Example
a | b | result |
---|---|---|
7 | 20 | 1 |
11 | 22 | 1 |
12 | 21 | 2 |
My solution to the problem
class Solution {
public int solution(int a, int b) {
int answer = 0;
int temp = b / GCD(a, b);
while(temp != 1){
if(temp % 2 == 0){
temp /= 2;
}else if(temp % 5 == 0){
temp /= 5;
}else {
answer = 2;
return answer;
}
}
answer = 1;
return answer;
}
private int GCD(int a, int b){
if(b == 0){
return a;
}else{
return GCD(b, a % b);
}
}
}
Solution explanation
Stores the value of dividing b by the greatest common divisor (GCD) of a and b in the variable temp.
To do this, we call the GCD method.
The loop is performed until temp becomes 1.
Check if temp is divisible by 2 or 5.
If it is divisible, divide the corresponding values by 2 and 5, respectively, and then update temp again.
If temp is not divisible by 2 or 5, we set answer to 2 and return this value.
If all conditions pass, set answer to 1 and return this value.
This code efficiently solves the problem by determining the relationship between two given numbers.
In particular, the key to solving the problem is determining whether it is divisible by using the greatest common divisor.