Print Multiples in Java with Time Complexity O(A)


Given two positive integers A and B, print to the console the first A non-negative numbers that are divisible by B

A number X is divisible by B if X modulo B == 0

Example:

Input: A = 5, B = 3

Output:
3
6
9
12
15

Explanation:
The first 5 positive integers that are divisible by 3 are
3, 6, 9, 12 and 15
1 modulo 3 = 1 => not divisible
2 modulo 3 = 2 => not divisible
3 modulo 3 = 0 => divisible
4 modulo 3 = 0 => not divisible
5 modulo 3 = 0 => not divisible
6 modulo 3 = 0 => divisible
7 modulo 3 = 0 => not divisible
8 modulo 3 = 0 => not divisible
9 modulo 3 = 0 => divisible
10 modulo 3 = 0 => not divisible
11 modulo 3 = 0 => not divisible
12 modulo 3 = 0 => divisible
13 modulo 3 = 0 => not divisible
14 modulo 3 = 0 => not divisible
15 modulo 3 = 0 => divisible

Understanding the Problem

The core challenge of this problem is to find the first A non-negative numbers that are divisible by B. This problem is significant in various applications such as generating sequences, scheduling tasks, and more. A common pitfall is to iterate through all numbers and check divisibility, which is inefficient.

Approach

To solve this problem, we can use a straightforward approach:

  1. Start iterating from 1.
  2. Check if the current number is divisible by B.
  3. If it is, print the number and increment the counter.
  4. Stop when the counter reaches A.

This approach ensures that we only print the required numbers and stop as soon as we have printed A numbers.

Naive Solution

The naive solution involves iterating through all numbers and checking if each is divisible by B. This is not optimal because it may involve unnecessary checks.

Optimized Solution

An optimized solution involves directly calculating the multiples of B:

  1. Start from B and keep adding B to get the next multiple.
  2. Print each multiple until we have printed A numbers.

This approach is more efficient as it directly generates the required multiples.

Algorithm

Here is a step-by-step breakdown of the optimized algorithm:

  1. Initialize a counter to 0.
  2. Start a loop from 1 to infinity.
  3. In each iteration, calculate the current multiple as currentMultiple = B * counter.
  4. Print the current multiple.
  5. Increment the counter.
  6. Stop the loop when the counter reaches A.

Code Implementation

public class PrintMultiples {
    public static void main(String[] args) {
        int A = 5; // Number of multiples to print
        int B = 3; // Divisor

        // Counter to keep track of how many multiples have been printed
        int count = 0;

        // Start from 1 and keep checking for multiples of B
        for (int i = 1; count < A; i++) {
            if (i % B == 0) {
                System.out.println(i);
                count++;
            }
        }
    }
}

Complexity Analysis

The time complexity of the optimized solution is O(A) because we only iterate until we find A multiples. The space complexity is O(1) as we are not using any additional space.

Edge Cases

Potential edge cases include:

These cases are handled by the algorithm as it directly calculates the multiples.

Testing

To test the solution comprehensively, consider the following test cases:

Use a testing framework like JUnit to automate the tests.

Thinking and Problem-Solving Tips

When approaching such problems:

Conclusion

In this blog post, we discussed how to print the first A non-negative numbers divisible by B efficiently. We explored both naive and optimized solutions, analyzed their complexities, and provided a detailed Java implementation. Understanding and solving such problems is crucial for developing strong problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: