हमें एक जावास्क्रिप्ट फ़ंक्शन लिखने की आवश्यकता है जो केवल इनपुट के रूप में एक संख्या लेता है। फलन को ऐसी सबसे छोटी संख्या ज्ञात करनी चाहिए जो सभी प्रथम 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