Fibonacci function

Question posted in Computer Software on 05 2010
Rate question difficulty level 1 Votes
Write Fibonacci function to calculate Nth value in Fibonacci sequence
 
 
6 Answers
 
The correct answer here is the Golden Ratio, but don't give it unless you first solve the sequence on the board.
Here is the answer interviewers are looking for. This is the corrected version:
public int getNthFib(int n)
{
if (n = 1)
return 0;
else if (n = 2)
return 1;
else
{
int oldValue = 0;
int currentValue = 1;
for (int i = 3; i <= n; i++)
{
currentValue = oldValue + currentValue;
}
}
return currentValue;
}

05/22/2010
 
 
well, I promise to start checking these.
Here's the answer i meant to post:

public int getNthFib(int n)
{
if (n = 1)
return 0;
else if (n = 2)
return 1;
else
{
int secondValue = 0;
int firstValue = 1;
int currentValue = 1;
for (int i = 3; i <= n; i++)
{
currentValue = firstValue + secondValue;
secondValue = firstValue;
firstValue = currentValue;
}
}
return currentValue;
}

05/31/2010
 
 
Usually the interviewer is looking for the simple answer to this question, and this would be sufficient:
public static PointPair slowClosestPair(XYPoint[] points) {
public static XYPoint point1;
public static XYPoint point2;
public static double dmin = 999999999;

for (int i=0; i < points.length - 1; i++) {
for (int j = i+1; j < points.length; j++) {
double x1 = points[i].x;
double y1 = points[i].y;
double x2 = points[j].x;
double y2 = points[j].y;
double distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
if (distance < dmin) {
dmin = distance;
point1 = points[i];
point2 = points[j];
}
}
}
PointPair closest = new PointPair(point1, point2, dmin);
return closest;
}

However for large numbers of points, this is inefficient.
The wiki article on this topic is quite sufficient:
http://en.wikipedia.org/wiki/Closest_pair_of_points_problem

07/13/2010
 
 
private void Febonecci(int f1,int f2, int N)
{
Console.WriteLine(f2);
if (f2 >= N)
return;
Febonecci(f2, f1 + f2, N);
}

07/13/2010
 
 
This functions assumes that the first Fibonacci value is 0 (zero).

Create Function dbo.Fibonacci (@intSeqNo Int)
Returns Int
as

Begin

Declare
@fltGoldenRatio Float,
@intResult Int ,
@fltSqrt5 Float

Set @intSeqNo = @intSeqNo - 1
Set @fltSqrt5 = Sqrt(5)
Set @fltGoldenRatio = (1 + @fltSqrt5) / 2

Set @intResult = (Power(@fltGoldenRatio, @intSeqNo) - Power(((-1) / @fltGoldenRatio), @intSeqNo)) / @fltSqrt5

Return @intResult

End

07/14/2010
 
 
here is what I came out with:

public static int GetNthFib(int n){
return n < 2 ? n : GetNthFib(0, 1, n-2);
}

private static int GetNthFib(int f0, int f1, int n){
return n == 0 ? f1 : GetNthFib(f1, f0 + f1, n - 1);
}

07/14/2010
 
 
Add an answer*
 
Your name
Email
 
Company: Microsoft
Location: United States
developer programming

add a question

arrow_blue


Now hiring!
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------