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 小时前
别滥用 Task.Run:C# 异步并发实操指南
c#·.net
我好喜欢你~9 小时前
C#---StopWatch类
开发语言·c#
一阵没来由的风12 小时前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
一枚小小程序员哈18 小时前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
Eternity_GQM20 小时前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word
cimeo1 天前
【C 学习】06-算法&程序设计举例
c#
百锦再1 天前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
WYH2871 天前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
hqwest2 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农2 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net