हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एकमात्र इनपुट के रूप में एक संख्या लेता है। फ़ंक्शन को वह सबसे छोटी संख्या ढूंढनी चाहिए जो पहले n प्राकृतिक संख्याओं से बिल्कुल विभाज्य हो।
उदाहरण के लिए -
n = 4 के लिए, आउटपुट 12 होना चाहिए, क्योंकि 12 सबसे छोटी संख्या है जो 1, 2, 3 और 4 से विभाज्य है।
उदाहरण
इसके लिए कोड होगा -
const smallestMultiple = num => {
let res = 0;
let i = 1;
let found = false;
while (found === false) {
res += num;
while (res % i === 0 && i <= num) {
if (i === num) {
found = true;
}
i++;
}
i = 1;
}
return res;
};
console.log(smallestMultiple(2));
console.log(smallestMultiple(4));
console.log(smallestMultiple(12));
console.log(smallestMultiple(15));आउटपुट
कंसोल में आउटपुट होगा -
2 12 27720 360360