How to usethe Arithmetic Series Formula in Javascript

In this approach, we are going to iterate everytime with the increment of 15, as 15 is the LCM of 3 and 5, And if number is divisible by 15 then it will also be divisible by 3 and 5. And it will only work if we are starting our iteration from 0.

Example: This example describes how you can find out the numbers which are divisible by 3 and 5 using arithmetic series formula

Javascript
// JavaScript program to print 
// numbers that are divisible
// by 3 and 5

// Lcm of 3 and 5 is 15
// LCM(3, 5) = 15
let n = 150;

// Function for finding 
// out divisible number

function divisibility(n) {

    // Start loop from 0 to n
    // by increment of +15 every time
    for (let i = 0; i <= n; i += 15) {
        console.log(i);
    }
}

// Calling function divisibility
divisibility(n);

Output
0
15
30
45
60
75
90
105
120
135
150

Time Complexity: The time complexity will be O(n) as loop will iterate n times.

Auxiliary Space: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.

JavaScript Program to Print All Numbers Divisible by 3 and 5 of a Given Number

In this article, we are going to discuss how to print all the numbers that are divisible by 3 and 5. We will write a program for finding out those numbers. For this, we can use loop or conditional statements.

Example:

Input: 75
Output: 0, 15, 30, 45, 60, 75
Input: 150
Output: 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150

We can solve this by using the following methods:

Table of Content

  • Using for Loop
  • Using LCM Method
  • Using the Arithmetic Series Formula
  • Using a while loop

Similar Reads

Approach 1: Using for Loop

Using n = 50 as an example, the program should output all numbers below 50 that are divisible by both 3 and 5. To do this, divide each number between 0 and n by 3 and 5 in a for loop. Print that value if the remainder in both scenarios is 0....

Approach 2: Using LCM Method

Here we are taking LCM of 3 anad 5 which is 15. If any number which is divisible by 15 it means it can also divisible by 3 and 5 so we are only searching for that number which is divisible by 15 and printing that number only....

Approach 3: Using the Arithmetic Series Formula

In this approach, we are going to iterate everytime with the increment of 15, as 15 is the LCM of 3 and 5, And if number is divisible by 15 then it will also be divisible by 3 and 5. And it will only work if we are starting our iteration from 0....

Approach 4: Using a while loop

We can iterate through the numbers starting from 0 and increment by 1 until the given number (inclusive). For each number, we check if it is divisible by both 3 and 5. If it is, we print it....

Contact Us