I am writing a library featuring several common mathematical methods as a way to hone my skills. I am trying to implement the Fibonacci sequence using arrays. Here is the code in the library:
我正在写一个以几种常用数学方法为特色的图书馆,以此来磨练我的技能。我正在尝试使用数组来实现斐波那契序列。这是图书馆的代码:
public static int[] Fibonacci(int numElement)
{
int n = numElement - 1;
int[] a = new int[numElement + 1];
a[0] = 0;
a[1] = 1;
for (int i = 2; i <= n; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
}
To test it I used a console application in which I referenced my dll:
为了测试它,我使用了一个控制台应用程序,其中我引用了我的dll:
static void Main(string[] args)
{
int[] b = new int[9];
b = numberTheory.Fibonacci(9);
foreach (var item in b)
{
Console.WriteLine(item);
}
}
}
However, this is the output of the above code (9 for input):
然而,这是上述代码的输出(输入的9):
0
0
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
Any other input results in the same output format. How can I fix my code to get the desired output?
任何其他输入结果都是相同的输出格式。如何修改代码以获得所需的输出?
EDIT : it seems that the loop does not iterate irrespective of the position of return statement (or its existence at all).
编辑:似乎循环没有迭代,不管返回语句的位置(或者它的存在)。
3 个解决方案
#1
0
You are returning prematurely and the wrong type - as pointed by @konked. However the solution provided by him, still has a problem: Fibonacci(9) should be equal to 34 (not 21). So you will need n+1 places in the array.
你过早地返回错误的类型——正如@konked所指出的那样。但是他提供的解决方案仍然有一个问题:Fibonacci(9)应该等于34(不是21)。所以你需要在数组中有n+1个位置。
public int[] Fibonacci(int numElement)
{
if (numElement < 0)
throw new ArgumentOutOfRangeException("numElement", numElement, "Fibonnaci number to get must be greater or equal than 0");
var n = numElement + 1; //you need n+1 positions. The 9th number is in 10th position
var a = new int[n];
a[0] = 0;
if (numElement == 0)
return a;
a[1] = 1;
for (var i = 2; i < n; i++)
a[i] = a[i - 2] + a[i - 1];
return a;
}
#2
0
The generation of the sequence terminates too early. Change it as follows.
序列的生成过早终止。改变它,如下所示。
public static int[] Fibonacci(int numElement)
{
int n = numElement - 1;
int[] a = new int[numElement + 1];
a[0] = 0;
a[1] = 1;
for (int i = 2; i <= n; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
#3
0
Your return statement is in the wrong place and is returning the wrong type ( element currently in the loop rather than the array ), you're also making some unnecessary variables as well if you change the method to the following it should work
您的返回语句位于错误的位置,并且返回错误的类型(当前在循环中的元素而不是数组),您也在做一些不必要的变量,如果您将方法更改为下面的方法,那么它应该可以工作。
public static int[] Fibonacci(int numElement)
{
int[] a = new int[numElement];
a[0] = 0;
a[1] = 1;
for (int i = 2; i < numElement; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
You can also check out a working fiddle here : https://dotnetfiddle.net/dWZck8
您还可以在这里查看一个工作的小提琴:https://dotnetfiddle.net/dWZck8。
#1
0
You are returning prematurely and the wrong type - as pointed by @konked. However the solution provided by him, still has a problem: Fibonacci(9) should be equal to 34 (not 21). So you will need n+1 places in the array.
你过早地返回错误的类型——正如@konked所指出的那样。但是他提供的解决方案仍然有一个问题:Fibonacci(9)应该等于34(不是21)。所以你需要在数组中有n+1个位置。
public int[] Fibonacci(int numElement)
{
if (numElement < 0)
throw new ArgumentOutOfRangeException("numElement", numElement, "Fibonnaci number to get must be greater or equal than 0");
var n = numElement + 1; //you need n+1 positions. The 9th number is in 10th position
var a = new int[n];
a[0] = 0;
if (numElement == 0)
return a;
a[1] = 1;
for (var i = 2; i < n; i++)
a[i] = a[i - 2] + a[i - 1];
return a;
}
#2
0
The generation of the sequence terminates too early. Change it as follows.
序列的生成过早终止。改变它,如下所示。
public static int[] Fibonacci(int numElement)
{
int n = numElement - 1;
int[] a = new int[numElement + 1];
a[0] = 0;
a[1] = 1;
for (int i = 2; i <= n; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
#3
0
Your return statement is in the wrong place and is returning the wrong type ( element currently in the loop rather than the array ), you're also making some unnecessary variables as well if you change the method to the following it should work
您的返回语句位于错误的位置,并且返回错误的类型(当前在循环中的元素而不是数组),您也在做一些不必要的变量,如果您将方法更改为下面的方法,那么它应该可以工作。
public static int[] Fibonacci(int numElement)
{
int[] a = new int[numElement];
a[0] = 0;
a[1] = 1;
for (int i = 2; i < numElement; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
You can also check out a working fiddle here : https://dotnetfiddle.net/dWZck8
您还可以在这里查看一个工作的小提琴:https://dotnetfiddle.net/dWZck8。