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

using System.Numerics;

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

编写 fibonacci.cs 如下

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/

相关推荐
上位机付工2 小时前
C#与倍福TwinCAT3进行ADS通信
开发语言·c#
土了个豆子的3 小时前
02.继承MonoBehaviour的单例模式基类
开发语言·visualstudio·单例模式·c#·里氏替换原则
疯狂的维修3 小时前
c#中public类比博图
c#·自动化
土了个豆子的6 小时前
03.缓存池
开发语言·前端·缓存·visualstudio·c#
xiaowu0801 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
VisionPowerful1 天前
九.弗洛伊德(Floyd)算法
算法·c#
ArabySide1 天前
【C#】 资源共享和实例管理:静态类,Lazy<T>单例模式,IOC容器Singleton我们该如何选
单例模式·c#·.net core
gc_22991 天前
C#测试调用OpenXml操作word文档的基本用法
c#·word·openxml
almighty271 天前
C#海康车牌识别实战指南带源码
c#·海康车牌识别·c#实现车牌识别·车牌识别源码·c#车牌识别
c#上位机1 天前
wpf之TextBlock
c#·wpf