斐波那契系列之和

che*_*oky 2 c fibonacci

我制作了这段代码来获得fibnacci系列的术语总和:

int main() {
  int previous, current = 0, next = 1,
      sum = current, threshold;
  printf("Enter the threshold: ") ;
  scanf("%d", &threshold) ;
  printf("Fibonacci series: %d", current) ;
  while (sum < threshold)
  {
    previous = current;
    current = next;
    next = previous + current;
    printf(" + %d", current) ;
    sum += current;

    if((log10(sum) + 1) >= 7)
      break;
  }
  printf(" = %d\n", sum);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要你从系列的那一端告诉我所有术语的总和大于7位数.我试过这个,我不知道它是否方便:

if((log10(sum) + 1) >= 7)
          break;
Run Code Online (Sandbox Code Playgroud)

因为我已经了解log10()消耗了大量的处理器时间和资源,¿有更有效的方法吗?

Dav*_*nan 5

最简单,最自然的方法是测试是否sum >= 1000000.