CSharp large fibonacci 源代码
时间: 2023-10-23 21:10:04 浏览: 80
以下是C#的大型斐波那契数列的源代码:
using System;
using System.Numerics;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of terms to display:");
int numTerms = int.Parse(Console.ReadLine());
BigInteger prev = 0;
BigInteger curr = 1;
Console.WriteLine(prev);
Console.WriteLine(curr);
for (int i = 2; i < numTerms; i++)
{
BigInteger temp = curr;
curr += prev;
prev = temp;
Console.WriteLine(curr);
}
}
}
相关问题
CSharp 斐波那契查找 源代码
以下是CSharp实现的斐波那契查找的源代码:
```
using System;
class FibonacciSearch
{
public static int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
public static int Search(int[] arr, int x)
{
int n = arr.Length;
int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;
while (fibM < n)
{
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
int offset = -1;
while (fibM > 1)
{
int i = Math.Min(offset + fibMMm2, n - 1);
if (arr[i] < x)
{
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x)
{
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else
return i;
}
if (fibMMm1 == 1 && arr[offset + 1] == x)
return offset + 1;
return -1;
}
static void Main()
{
int[] arr = { 10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100 };
int n = arr.Length;
int x = 85;
Console.WriteLine("Found at index: " + Search(arr, x));
}
}
```
希望对你有所帮助!
阅读全文