javascript - 汉明数字代码不起作用
问题描述
我试图获得前 10 个汉明数(素数为 2、3 和 5 的数字)。第一个getFactors(num)
函数返回我想要的(set
主要因素之一),但是第二个hammingNum()
函数没有按预期运行。有任何想法吗?
let factors = []
isPrime = true
function getFactors(num){
while (num % 2 === 0){
factors.push(2);
num /= 2;
}
for (i = 3; i < num; i++){
if (num % i === 0){
isPrime = false;
break;
} else {
isPrime = true
}
}
if (isPrime){
factors.push (num)
}
if (!isPrime) {
factors.push (i)
getFactors (num/i)
}
return new Set (factors) //return a set of prime numbers
}
function hammingNums(){
let list = []
while (list.length < 11){
for (i = 1; i<1000; i++){
if ((getFactors (i)).has (2 && 3 && 5)){
list.push (i)
}
}
} return list
hammingNums()
解决方案
在第二个示例中,您将传递2 && 3 && 5 === 5
给该has
方法。
这个怎么样?
var s = getFactors(i);
if(s.has(2) && s.has(3) && s.has(5)) {
...
推荐阅读
- excel - 加载过程中出现以下问题: Excel 名称
- java - 谁选择在 Java 中执行哪个重载函数?
- typescript - 用于处理数据库模式默认值的 Typescript 实用程序类型
- python-3.x - 在 cmd 中使用“pip install ...”安装任何模块时出错
- angular - Angular 10+ 构建到绝对路径缺少 de index.html 文件
- c# - 未加载视图
- javascript - 我在 mvc Web 应用程序上的提交按钮有问题
- css - 创建扩展 Tailwind css 类的类
- highcharts - Highcharts:同一窗格中具有不同 Y 轴的多个图表?
- c# - 如何使用键中的点添加到路由值元素