1.3 KiB
1.3 KiB
title | localeTitle |
---|---|
Smallest multiple | أصغر متعددة |
المشكلة 5: أصغر متعددة
طريقة:
- في هذا التحدي ، نحتاج إلى العثور على LCM من 1 إلى n من الأرقام.
- للعثور على LCM لأحد الأعداد ، نستخدم الصيغة التالية:
- ![لكم]](
9453a93953
) - لإيجاد GCD (القاسم المشترك الأكبر) من رقمين نستخدم خوارزمية Euclidean.
- بعد الحصول على LCM من رقمين ، يمكننا الحصول على LCM من الأرقام من 1 إلى n.
حل:
`//LCM of two numbers function lcm(a, b){ return (a*b)/gcd(a, b); }
//Euclidean recursive algorithm function gcd(a, b){ if (b === 0) return a; return gcd(b, a%b); }
function smallestMult(n){ let maxLCM = 1;
//Getting the LCM in the range for (let i = 2; i <= n; i++){ maxLCM = lcm(maxLCM, i); } return maxLCM; } `