如何在javascript中找到斐波那契数列中的素数之和?

发布于 2025-01-09 01:22:49 字数 331 浏览 0 评论 0原文

这是我尝试做的代码

function sumFibs(num) {
  let a=1,b=1,c=0,sum=2,count=0;
  while(b<=num){
    c=a+b
    a=b
    b=c
    for(let i=1;i<=c;i++){
      if(c%i===0){
        count++
      }
    }
    if(count===2){
      sum+=c
    }
  }
  return sum
}
console.log(sumFibs(6))

有人可以帮助我了解如何解决这个问题吗?

This is the code i tried to do

function sumFibs(num) {
  let a=1,b=1,c=0,sum=2,count=0;
  while(b<=num){
    c=a+b
    a=b
    b=c
    for(let i=1;i<=c;i++){
      if(c%i===0){
        count++
      }
    }
    if(count===2){
      sum+=c
    }
  }
  return sum
}
console.log(sumFibs(6))

Can someone help me understand how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2025-01-16 01:22:50

为每一步创建一个函数。

/**
 * Calculates the Fibonacci sequence up to a given number of times
 * @param   {Number} num Amount of numbers to return
 * @return  {Array[]} An array of numbers
 */
function fibonacci(num) {
  let a = 1,
    b = 0,
    f = [],
    temp;
  while (num > 0) {
    temp = a;
    a = a + b;
    b = temp;
    f.push(b);
    num--;
  }
  return f;
};

/**
 * Calculates the a given number and determines if it is a prime.
 * @param   {Number} int An integer
 * @return  {Boolean} True if int is a prime
 */
function prime(int) {
  if (int < 2) return false;
  for (let i = 2; i <= Math.sqrt(int); i++) {
    if (int % i == 0) return false;
  }
  return true;
};
/**
 * Calls fibonacci(n) then .filter() each number and returns an array of primes and the sum of the primes is the return of .reduce()
 * @param   {Number} num Amount of numbers to get from Fibocanni sequence
 * @return  {Number} A sum of all primes
 */
function fibPrmSum(num) {
  let fib = fibonacci(num);
  console.log(`Fibonacci: ${fib}`);
  let prm = fib.filter(f => prime(f));
  console.log(`Prime: ${prm}`);
  return prm.reduce((sum, now) => sum + now, 0);
};

console.log(`Sum of all primes in the first 6 numbers of the Fibonacci sequence is ${fibPrmSum(6)}`);

Make a function for each step.

/**
 * Calculates the Fibonacci sequence up to a given number of times
 * @param   {Number} num Amount of numbers to return
 * @return  {Array[]} An array of numbers
 */
function fibonacci(num) {
  let a = 1,
    b = 0,
    f = [],
    temp;
  while (num > 0) {
    temp = a;
    a = a + b;
    b = temp;
    f.push(b);
    num--;
  }
  return f;
};

/**
 * Calculates the a given number and determines if it is a prime.
 * @param   {Number} int An integer
 * @return  {Boolean} True if int is a prime
 */
function prime(int) {
  if (int < 2) return false;
  for (let i = 2; i <= Math.sqrt(int); i++) {
    if (int % i == 0) return false;
  }
  return true;
};
/**
 * Calls fibonacci(n) then .filter() each number and returns an array of primes and the sum of the primes is the return of .reduce()
 * @param   {Number} num Amount of numbers to get from Fibocanni sequence
 * @return  {Number} A sum of all primes
 */
function fibPrmSum(num) {
  let fib = fibonacci(num);
  console.log(`Fibonacci: ${fib}`);
  let prm = fib.filter(f => prime(f));
  console.log(`Prime: ${prm}`);
  return prm.reduce((sum, now) => sum + now, 0);
};

console.log(`Sum of all primes in the first 6 numbers of the Fibonacci sequence is ${fibPrmSum(6)}`);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文