Javascript中的素数函数...多个语句
So I'm tasked with making program that can tell whether a number is prime or not. I want to solve this problem using a multiple if-statement. The one that I came up with is awkward. So i just want some feedback on how to make it better.:
所以我的任务是制作可以判断数字是否为素数的程序。我想使用多个if语句来解决这个问题。我提出的那个很尴尬。所以我只想要一些关于如何让它变得更好的反馈:
function primenumber(num) {
if (num / num === 1 && num % 2 !== = 0 || num % 3 !== = 0 | num % 5 !== = 0 | num % 7 !== = 0) {
return true;
} else {
return false
}
}
I figured these numbers are the lowest common denominators. So a number is prime if these numbers don't divide evenly into it. Any feedback is greatly appreciated. Keep in mind that I am new to Javascript.
我认为这些数字是最低的共同点。因此,如果这些数字不均匀分配,则数字为素数。任何反馈都非常感谢。请记住,我是Javascript的新手。
1 个解决方案
#1
0
I figured it out:
我想到了:
If((num === 1) || (num === 2) || (num === 3) || (num === 5) || (num === 7))
{ return true;
}
If((num%2===0) || (num%3===0) || (num%5===0) || (num%7===0)){
return false;}
}
return true;
}
It may not be the most sophisticated coding but it's mine and it works perfectly.. at least according to coderbyte
它可能不是最复杂的编码,但它是我的,它完美地工作..至少根据coderbyte
更多相关文章
- typeescript:在数字上使用parseInt()时出错
- 剑指offer python版 数组中只出现一次的两个数字
- 如何在python 3中将单词转换为数字(自己的键和值)?
- 你怎么检查python字符串是否只包含数字?
- python - pandas或者sklearn中如何将字符形式的标签数字化
- 三个猜数字游戏代码(Python)
- 正则将长数字转为英式写法(从后向前3个数字一个逗号)
- 关于mysql对字符串的数字的排序
- sqlserver2008r2查找非中文字母数字出现的第一个位置