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 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
Contact Us