迭代计算黄金比例

标签 c math

enter image description here

这是由 a0 = 1 和 a(n+1) = sqrt(1 + an) 定义的序列,我正在尝试计算它的前 40 项。

#include <stdio.h>
#include <math.h>

int main ()
{
    float limit = 1;

    for(int i = 0; i < 40; i++ ) {

        limit = 1 + sqrt(limit);
        printf("%1.21f\n", limit);
    }

    return(0);
}

输出

2.000000000000000000000
2.414213657379150390625
2.553774118423461914062
2.598053216934204101562
2.611847877502441406250
2.616121292114257812500
2.617442846298217773438
2.617851257324218750000
2.617977619171142578125
....

我的代码为我提供了 真实值 + 1。我可以从最终答案中减去 1,但我觉得这是丑陋的代码。

最佳答案

我想你正在期待这个..

 #include <stdio.h>
 #include <math.h>

 int main ()
 {
 float limit = 1;

    for(int i = 0; i < 40; i++ ) {

    limit =sqrt( 1 + limit);//changes here
    printf("%1.21f\n", limit);
 }

 return(0); 
}

关于迭代计算黄金比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034063/

相关文章:

c - OpenGL的工作组大小和本地大小有什么区别?

c - 如何在 C 函数中传递二维数组(矩阵)?

c - 当第一个路径失败时,为什么我不能再次对同一个变量使用 scanf

file - 批处理文件基础知识 : Adding a hardcoded integer to a variable in a for loop

math - float 学有问题吗?

objective-c - 将 CGFloat 转换为 NSUInteger 返回 0

c - C 中的动态链接(使用 dlopen)和 header 包含

algorithm - 动态规划中的包含 - 排除

c# - 使用 x, y 坐标计算角度

algorithm - 特殊 Blob 填充算法的名称