所以这就是我要解决的挑战:
“酷号”定义为一个数字,其中每个数字的平方和最终达到1。
ex:23
2 ^ 2 + 3 ^ 2 = 13
1 ^ 2 + 3 ^ 2 = 10
1 ^ 2 + 0 ^ 2 = 1
注意:所有“非酷”数字最终将在其系列中返回4并无限循环。
查找从1到1,000,000(含)的所有“酷数字”。然后加起来。
这是我的代码。它可以工作但是当我发布我的总和(70601040511)时,它会被主机服务器拒绝。也许你在这里看到的东西我没有?
function isCool(int){
var intArr = [];
var squareArr = [];
var sumOfSquares = 0;
function numToArr(num){ //split int into separate digits
var arr = num.toString().split("");
var res = [];
var len = arr.length;
for (i=0; i<len; i++){
res[i] = parseInt(arr[i]);
}
return res;
}
function getSquareArray(){ //get the square of each digit in array
var res = [];
var len = intArr.length;
for(i=0; i < len;i++){
res[i] = Math.pow(intArr[i],2);
}
return res;
}
function getSumOfSquares(){ //get sum of squares in int array
var res = 0;
var len = squareArr.length;
for (i=0; i < len; i++){
res += squareArr[i];
}
return res;
}
intArr = numToArr(int);
squareArr = getSquareArray(intArr);
sumOfSquares = getSumOfSquares(squareArr);
if(sumOfSquares === 4){
return false;
} else if (sumOfSquares === 1){
return true;
} else {
return isCool(sumOfSquares); //call function on itself until 4 or 1 is reached
}
}
总结所有很酷的数字,包括给定的限制
function getCoolNumbers(limit){
var limit = document.getElementById("num").value;
var res = [];
for (var i = 1; i<=limit; i++){
if(isCool(i)===true){
res.push(i);
}
}
document.getElementById("coolarray").innerHTML = res;
return res;
}
var coolSum = 0;
function sumCoolNumbers(){
var res = 0;
var arr = [];
var limit = document.getElementById("num").value;
arr = getCoolNumbers(limit);
var len = arr.length
for(var i = 0; i<len; i++){
res += arr[i];
}
coolSum = res;
document.getElementById("coolsum").innerHTML = "= " + res;
document.getElementById("coolquant").innerHTML = len + " Cool Numbers"
}
答案 0 :(得分:1)
@nnnnnn发表了一些好评,你应该解决他/她强调的问题。为了和@nnnnnn和@PeterJacuzzi一起去,我还写了我自己的isCool
版本,并得出70600040511
这是否是正确答案,我不知道。但是4个人(包括你)根据你对问题的说明得出了同样的答案。
所以我不确定这个答案对你有什么帮助,除了向你展示解决问题的不同方法 - 也许还有一些空间/时间方面的考虑。
const digits = x =>
x < 10
? [x]
: digits (x / 10 >> 0) .concat ([x % 10])
const add = (x,y) =>
x + y
const sum = xs =>
xs.reduce (add, 0)
const sq = x =>
x * x
const isCool = x =>
x === 1 ? true :
x === 0 || x === 4 ? false :
isCool (sum (digits (x) .map (sq)))
const time = (s, f) =>
(console.time(s), f(), console.timeEnd(s))
time ('without memo', $ => {
const xs =
Array.from(Array(1e6), (_,x) => x).filter(isCool)
console.log('count %d; sum %d', xs.length, sum(xs))
})
// count 143070; sum 70600040511
// without memo: 6558.235ms
这是一个记忆版本,占用更多空间,但运行时间更短
const digits = x =>
x < 10
? [x]
: digits (x / 10 >> 0) .concat ([x % 10])
const add = (x,y) =>
x + y
const sum = xs =>
xs.reduce (add, 0)
const sq = x =>
x * x
const isCoolMem = (memo => {
const readwrite = (x, f) =>
memo.has (x)
? memo.get (x)
: (memo.set (x, f (x)), memo.get (x))
const aux = x =>
readwrite (x, y =>
y === 1 ? true :
y === 0 || y === 4 ? false :
aux (sum (digits (y) .map (sq))))
return aux
}) (new Map ())
const time = (s, f) =>
(console.time(s), f(), console.timeEnd(s))
time ('with memo', $ => {
const xs =
Array.from(Array(1e6), (_,x) => x).filter(isCoolMem)
console.log('count %d; sum %d', xs.length, sum(xs))
})
// count 143070; sum 70600040511
// with memo: 2667.815ms
time ('with memo, round 2', $ => {
const xs =
Array.from(Array(1e6), (_,x) => x).filter(isCoolMem)
console.log('count %d; sum %d', xs.length, sum(xs))
})
// count 143070; sum 70600040511
// with memo, round 2: 488.205ms