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/

相关推荐
nnsix27 分钟前
【C#】HttpPost请求 - Query参数 - URL编码方法
java·javascript·c#
无风听海1 小时前
TaskFactory
服务器·开发语言·c#
世洋Blog1 小时前
Unity编辑器基础
unity·c#·编辑器·游戏引擎
钰fly2 小时前
C#索引器 接口
c#
SunnyDays10112 小时前
使用 C# 隐藏 Excel 工作表 (单表格、批量处理 与 超级隐藏)
c#·隐藏excel工作表
2501_930707782 小时前
如何使用C#代码将 Excel 文件转换为 SVG
开发语言·c#·excel
kylezhao20192 小时前
C#上位机从入门到精通(场景化实战教程)学习内容简介
开发语言·c#
我是唐青枫10 小时前
C#.NET 索引器完全解析:语法、场景与最佳实践
c#·.net
FuckPatience15 小时前
C# 使用内存映射文件实现进程间通信
c#
kylezhao201917 小时前
如何在 C# 项目中使用 NLog 进行日志记录
开发语言·c#