CodeWars符号化問題2021、03、16-Cubesの山を構築


[質問]


Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3.
You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build?
The parameter of the function findNb (find_nb, find-nb, findNb) will be an integer m and you have to return the integer n such as n^3 + (n-1)^3 + ... + 1^3 = m if such a n exists or -1 if there is no such n.
Examples:
findNb(1071225) --> 45
findNb(91716553919377) --> -1
(要約)1からnまでの各数字の3平方の和が、与えられた数とnがどれだけ等しいかを探し出す.
なければ-1はreturnです

[回答]

function findNb(m) {
  let n = 1;

  while(Math.pow(n * (n + 1) / 2  , 2) <= m) {
    if(Math.pow(n * (n + 1) / 2  , 2) === m) {
      return n
    }
    else {
      n++;
    }
  }

  return -1;
}
1から任意の数字nまで、3つの平方の和を求めて比較すると、2つの繰り返し文が必要になる可能性があります.
だから高校時代に習ったシグマの公式を思い出してそれを使うことにしました

もちろん正しい方法は思い出せない