Contents

About Finding Common Multiples (with.Java)

   Aug 20, 2023     1 min read

In this article, we learned about finding a common multiple (with.Java)

We’re going to learn as we go by solving the Coding Test problem, reflecting on the problem we solved, and exploring other ways to solve it. Let’s start with the problem.

Problem

Given an integer number and n, m. Complete the solution function so that it returns 1 if number is a multiple of n and a multiple of m, or 0 otherwise.

Example input and output

num: 60 n: 2 m: 3 result: 1

In other words, 60 is a multiple of 2, which is also a multiple of 3, so 1 should be the result.

My solution to the problem

class Solution {
    public int solution(int number, int n, int m) {
        int answer = (number % n == 0) && (number % m == 0) ? 1 : 0;
        return answer;
    }
}

To determine the conjugate multiple, we divided num by n via the % operator and determined that if the remainder was zero, it was a multiple, so we solved by putting a 0 in answer if it wasn’t a 1. At the same time, it must also be a multiple of m, so we added a condition using the && AND logical operators.