C#:用 BigInteger 计算 斐波那契数列

using System.Numerics; 

计算 斐波那契数列(Fibonacci sequence),不受长整型位数限制。

编写  fibonacci.cs  如下

// C# program for Fibonacci Series
// using Dynamic Programming
using System;
using System.Numerics;

class fibonacci {

	static BigInteger fib(int n)
	{	
        BigInteger a = new BigInteger(0);
		BigInteger b = new BigInteger(1);
		BigInteger c = new BigInteger(0);

		int i;
		for (i = 2; i <= n; i++) {
			c = a + b;
            a = b;
            b = c;
		}
		return b;
	}

	// Fibonacci Series test
	public static void Main(string[] args)
	{
        if (args.Length <1){
            Console.WriteLine(" usage: fib.exe n ");
            return;
        }       
		int n;
        if (int.TryParse(args[0], out n)){
            if (n >1) Console.WriteLine(fib(n));
        } else {
            Console.WriteLine(" input n must be +int ");
        }
	}
}

where csc
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

编译  csc fibonacci.cs
fibonacci.cs(8,9): error CS0246: 未能找到类型或命名空间名称“BigInteger”


编译  csc  /r:System.Numerics.dll  fibonacci.cs

运行 fibonacci.exe 1000
参阅:https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/

相关推荐

  1. C#: BigInteger 计算 数列

    2024-04-24 00:32:01       16 阅读
  2. c++】数列

    2024-04-24 00:32:01       25 阅读
  3. Matlab定义函数计算数列

    2024-04-24 00:32:01       31 阅读
  4. perl:BigInt 计算 数列

    2024-04-24 00:32:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 00:32:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 00:32:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 00:32:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 00:32:01       20 阅读

热门阅读

  1. django忽略migrate

    2024-04-24 00:32:01       14 阅读
  2. QT-结构体到类

    2024-04-24 00:32:01       12 阅读
  3. Linux学习08_学习shell脚本编程

    2024-04-24 00:32:01       12 阅读
  4. Spring源码中的抽象工厂模式

    2024-04-24 00:32:01       15 阅读
  5. VUE-ajax

    2024-04-24 00:32:01       14 阅读
  6. CUDA流与异步

    2024-04-24 00:32:01       14 阅读
  7. linux irq:

    2024-04-24 00:32:01       16 阅读