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

📄 fibonacci.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 fibonacci number*/
public class fibonacci extends ExternalFunction
{
	/**calculate the Fibonacci number
	@param operand[0] = The index of the Fibonacci number
	@return the Fibonacci number
	*/
	public OperandToken evaluate(Token[] operands)
	{
		if (getNArgIn(operands) != 1)
			throwMathLibException("fibonacci: number of arguments != 1");
            
	    if (!(operands[0] instanceof DoubleNumberToken)) 
        	throwMathLibException("fibonacci: first argument must be a number");

		//harmonic(n) = 1 + 1/2 + 1/3 + ... + 1/n
		double index = ((DoubleNumberToken)operands[0]).getValueRe();
			
		double total1 = 0;
		double total2 = 1;
			
		for(int count = 1; count <= index; count++)
		{
			double temp = total2 + total1;
			total1 = total2;
			total2 = temp;
		}
		OperandToken result = new DoubleNumberToken(total1);
		
        return result;
	}
}


/*
@GROUP
general
@SYNTAX
answer = fibonacci(value)
@DOC
Returns the fibonacci number with an index of value.
@EXAMPLES
<programlisting>
fibonacci(5) = 5 
fibonacci(10) = 55
</programlisting>
@NOTES
@SEE
*/

⌨️ 快捷键说明

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