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/

相关推荐
chao1898441 天前
C 文件操作全解速览
服务器·c语言·c#
月巴月巴白勺合鸟月半1 天前
一个DevExpress的Docx文件处理的Bug的解决
c#·bug
.NET修仙日记1 天前
第一章:从零开始构建你的第一个C#/.NET应用程序
c#·.net·.net core
m5655bj1 天前
如何使用 Python 转换 Excel 工作表到 PDF 文档
开发语言·c#·excel
技术支持者python,php1 天前
SUB设备电子狗加密狗开发
c#
唐青枫1 天前
循环插入太慢?试试 C#.NET SqlBulkCopy,一次导入上百万数据
c#·.net
SmoothSailingT2 天前
C#窗体—子窗体获取父窗体TextBox框的值
c#·窗体
ysdysyn2 天前
C# 进程管理实战:检查与启动EXE程序的完整指南
开发语言·c#
云缘若仙2 天前
Godot游戏开发——C# (一)
c#·godot
花北城2 天前
【C#】List快速检查重复数据
开发语言·c#