检查互质数-JavaScript
如果两个数字之间不存在共同的素数,则称两个数字为互质数(1不是质数)
例如-
4 and 5 are co-primes 9 and 14 are co-primes 18 and 35 are co-primes 21 and 57 are not co-prime because they have 3 as the common prime factor
我们需要编写一个包含两个数字的函数,如果它们是互质的,则返回true,否则返回false
示例
让我们为该函数编写代码-
const areCoprimes = (num1, num2) => {
const smaller = num1 > num2 ? num1 : num2;
for(let ind = 2; ind < smaller; ind++){
const condition1 = num1 % ind === 0;
const condition2 = num2 % ind === 0;
if(condition1 && condition2){
return false;
};
};
return true;
};
console.log(areCoprimes(4, 5));
console.log(areCoprimes(9, 14));
console.log(areCoprimes(18, 35));
console.log(areCoprimes(21, 57));输出结果
以下是控制台中的输出-
true true true false
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短