Start learning free

LESSON 126 OF 410 · WHILE LOOPS & FOR LOOPS

Print Multiples in JavaScript (Time Complexity: O(A))

Practice Read the full explanation

Given two positive integers A and B, print to the console the first A positive 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 = 1 => not divisible
5 modulo 3 = 2 => not divisible
6 modulo 3 = 0 => divisible
7 modulo 3 = 1 => not divisible
8 modulo 3 = 2 => not divisible
9 modulo 3 = 0 => divisible
10 modulo 3 = 1 => not divisible
11 modulo 3 = 2 => not divisible
12 modulo 3 = 0 => divisible
13 modulo 3 = 1 => not divisible
14 modulo 3 = 1 => not divisible
15 modulo 3 = 2 => divisible
Hints:

If we look at the explanation, a simple algorithm comes to mind:
Start by iterating from 1 and check if it is divisible by B.
Keep a counter of how many numbers you printed so far.
Stop when you reached the A number of printed numbers.
STEP 1 OF 7
    Ctrl + Enter to check

    JAVASCRIPT · WHILE LOOPS & FOR LOOPS

    Print Multiples in JavaScript, explained

    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, filtering data, and more. A common pitfall is to iterate through all numbers and check divisibility, which can be inefficient.

    Approach

    To solve this problem, we can start with a naive approach and then optimize it:

    Naive Approach

    The naive approach involves iterating through all numbers starting from 1 and checking if each number is divisible by B. We keep a counter to track how many numbers we have found that are divisible by B. This approach is simple but not optimal as it may involve unnecessary checks.

    Optimized Approach

    An optimized approach leverages the fact that multiples of B are evenly spaced. Instead of checking each number, we can directly generate the multiples of B by multiplying B with an incrementing counter. This approach is more efficient as it avoids unnecessary checks.

    Algorithm

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

    1. Initialize a counter to 1.
    2. Iterate from 1 to A.
    3. In each iteration, multiply the counter by B to get the next multiple of B.
    4. Print the result.
    5. Increment the counter.

    Code Implementation

    // Function to print the first A non-negative numbers divisible by B
    function printMultiples(A, B) {
      // Initialize counter
      let count = 1;
      
      // Loop to print the first A multiples of B
      for (let i = 1; i <= A; i++) {
        // Calculate the multiple
        let multiple = count * B;
        
        // Print the multiple
        console.log(multiple);
        
        // Increment the counter
        count++;
      }
    }
    
    // Example usage
    printMultiples(5, 3); // Output: 3, 6, 9, 12, 15
    

    Complexity Analysis

    The time complexity of the optimized approach is O(A) because we are iterating A times to print the multiples. The space complexity is O(1) as we are using a constant amount of extra space.

    Edge Cases

    Potential edge cases include:

    • A or B being 1: The function should handle small values correctly.
    • Large values of A and B: The function should efficiently handle large inputs without performance degradation.

    Examples:

    printMultiples(1, 1); // Output: 1
    printMultiples(3, 10); // Output: 10, 20, 30
    

    Testing

    To test the solution comprehensively, consider a variety of test cases:

    • Simple cases with small values of A and B.
    • Edge cases with A or B being 1.
    • Complex cases with large values of A and B.

    Example test cases:

    printMultiples(5, 3); // Output: 3, 6, 9, 12, 15
    printMultiples(1, 1); // Output: 1
    printMultiples(3, 10); // Output: 10, 20, 30
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the problem requirements and constraints thoroughly.
    • Start with a simple, naive solution to get a basic understanding.
    • Look for patterns or properties that can help optimize the solution.
    • Break down the problem into smaller, manageable parts.
    • Practice solving similar problems to improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to solve the problem of printing the first A non-negative numbers divisible by B. We explored both naive and optimized approaches, provided a detailed algorithm, and implemented the solution in JavaScript. Understanding and solving such problems is crucial for developing strong problem-solving skills. Keep practicing and exploring further to improve your abilities.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free