- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Smallest number that is divisible by first n numbers in JavaScript

We are required to write a JavaScript function that takes in a number, say n. The function should find and return the smallest possible number that is exactly divisible by all numbers from 1 to n.

## Example

Following is the code −

const smallestDivisible = (num) => { let i, n = 1; const largestPower = (n, num) => { let p, e = 2, largest = n; while ((p = Math.pow(n, e)) <= num) { largest = p; e += 1; } return largest; } const isPrime = n => { let i, num = Math.ceil(Math.sqrt(n)); for (i = 3; i <= num; i += 2) { if (n % i === 0) { return false; } } return true; } for (i = 3; i <= num; i += 2) { if (isPrime(i)) { n *= largestPower(i, num); } } return n * largestPower(2, num); } console.log(smallestDivisible(20));

## Output

Following is the output on console −

232792560

- Related Questions & Answers
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Generating a random number that is divisible by n in JavaScript
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Count n digit numbers divisible by given number in C++
- Finding a pair that is divisible by some number in JavaScript
- Sum which is divisible by n in JavaScript
- Check if product of first N natural numbers is divisible by their sum in Python
- Largest N digit number divisible by given three numbers in C++
- Greatest number divisible by n within a bound in JavaScript
- Sum of first N natural numbers which are divisible by X or Y
- Find N digits number which is divisible by D in C++
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Smallest number of perfect squares that sums up to n in JavaScript
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- C++ Program for Smallest K digit number divisible by X?

Advertisements