S(n)=(1/1 + 1/2 + 1/3 + 1/4 + ... + 1 / n) [英] S(n) = (1/1 + 1/2 + 1/3 + 1/4 + … + 1/n)
问题描述
第2部分的目标是开发一种算法并实现代码来计算倒数的串联和S(n)=(1/1 + 1/2 + 1/3 + 1/4 + ... + 1 / n )使你的程序找到最小数字n,其系列倒数之和S(n)等于或超过用户输入的值。
例如,如果用户输入1.5,您的程序应显示以下内容:
最小的系列总和> = 1.5是S(2),其值为1.5。
我尝试过的事情:
我已经尝试过我所知道的所有事情并且可以'似乎让输出正确。
The goal of Part 2 is to develop an algorithm and implement code to calculate the series sum of reciprocals S(n) = (1/1 + 1/2 + 1/3 + 1/4 + … + 1/n) such that your program finds the smallest number n for which the series sum of reciprocals S(n) EQUALS OR EXCEEDS a value entered by the user.
For example, if the user enters 1.5 your program should display the following:
The smallest series sum >= 1.5 is S(2) which has the value 1.5.
What I have tried:
I have tried everything I know and can't seem to get the output to be correct.
推荐答案
对于大N,谐波系列有很多舍入误差。你的程序是否适用于N = 10?
对于N = 1,000,000 S = 14.393,相邻项之间的差异可能小于舍入。
使用补偿加法 Kahan求和算法 - 维基百科 [ ^ ]
并将每个术语存储在数组S []中。然后二进制搜索数组中的目标值。
The harmonic series has a lot of roundoff error for large N. Does your program work for N = 10?
For N = 1,000,000 S = 14.393, and the difference between adjacent terms may be less than roundoff.
Use compensated addition Kahan summation algorithm - Wikipedia[^]
and store each term in an array S[]. Then binary search the array for the target value.
这篇关于S(n)=(1/1 + 1/2 + 1/3 + 1/4 + ... + 1 / n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!