⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 harmonic.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
字号:
package jmathlib.toolbox.general;

import jmathlib.core.tokens.numbertokens.DoubleNumberToken;
import jmathlib.core.tokens.Token;
import jmathlib.core.tokens.OperandToken;
import jmathlib.core.functions.ExternalFunction;

/**External function to calculate the nth harmonic number*/
public class harmonic extends ExternalFunction
{
	/**calculate the harmonic number
	@param operand[0] = The index of the harmonic number
	@return the harmonic number
	*/
	public OperandToken evaluate(Token[] operands)
	{
		OperandToken result = new DoubleNumberToken(0);
		if(operands.length >= 1 && operands[0] instanceof DoubleNumberToken)
		{
			//harmonic(n) = 1 + 1/2 + 1/3 + ... + 1/n
			double index = ((DoubleNumberToken)operands[0]).getValueRe();
			
			double total = 0;
			for(int count = 1; count <= index; count++)
			{
				total += (1.0/ count);
			}
			result = new DoubleNumberToken(total);
		}
		return result;
	}
}


/*
@GROUP
general
@SYNTAX
answer = harmonic(value)
@DOC
Returns the harmonic number with an index of value.
@EXAMPLES
<programlisting>
harmonic(5) = 2.283333333333333
harmonic(10) = 2.9289682539682538
</programlisting>
@NOTES
This calculates sum(1..value){1/n}
@SEE
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -