Contents

Cursed number 3 (with.Java)

   May 13, 2024     2 min read

This is an article looking into the “Curse Number 3 (with.Java)” problem.

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

3x Villagers do not use multiples of 3 and the number 3 because they consider 3 to be a cursed number.

When the integer n is given as a parameter, complete the solution function to return n by replacing it with the number used in 3x Village.

Restrictions

  • 1 ≤ n ≤ 100

Input/Output Example

nresult
1525
4076

My solution to the problem

class Solution {
     public int solution(int n) {
         int answer = 0;
         for(int i = 1; i <= n; i++){
             answer++;
             while(answer % 3 == 0 || String.valueOf(answer).contains("3")){
                 answer++;
             }
         }

         return answer;
     }
}

Solution explanation

Set the answer variable to its initial value of 0.

This variable represents the result.

Execute the loop while increasing the i variable from 1 to n.

Increments the answer variable by 1.

Run the loop as long as the answer variable is a multiple of 3 or the string contains “3” after conversion to a number.

Each time the loop is executed, the answer variable is incremented by 1.

When the loop ends, the answer variable is returned.

Advantages

Performs a function that counts the number of numbers within a given range that are multiples of 3 or contain 3 in the number.

The code is concise and easy to understand.

disadvantage

Because it is implemented by increasing the answer variable by 1 and checking whether the conditions are met, the efficiency may be low in certain ranges.

Converting the answer variable to a string to check if it contains “3” may result in a performance penalty.

Improvements

Instead of using a loop to increment the answer variable by 1, you can improve performance by using a method to directly find numbers that are multiples of 3 or contain 3 in the number.

For example, to find multiples of 3, you can count in a range by increments of 3.

To check whether a number contains 3, you can divide the number by 10 and check the remainder.