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/

相关推荐
魔法阵维护师5 小时前
从零开发游戏需要学习的c#模块,第十六章(安装 MonoGame 并创建第一个窗口)
学习·游戏·c#·monogame
吴可可1235 小时前
用Teigha修改并保存CAD文件
数据库·算法·c#
xiaoshuaishuai87 小时前
C# 签名异常与Gas预估失败调试方案
开发语言·网络·tcp/ip·c#
xiaoshuaishuai87 小时前
C# Gemini 辅助网络安全漏洞分析
开发语言·web安全·c#
风继续吹..7 小时前
C# 文件输入输出 精简理解
开发语言·c#
魔法阵维护师8 小时前
从零开发游戏需要学习的c#模块,第十章(设计模式入门)
学习·游戏·设计模式·c#
风继续吹..8 小时前
C# 文件 IO 实操练习题 5道
开发语言·c#
苦荞米8 小时前
C#中,Ticks不能作为时间戳使用。
c#
吴可可1239 小时前
Teigha处理CAD样条曲线的方法解析
数据库·算法·c#
XS0301069 小时前
并发编程三
开发语言·c#