Register
Sign In
Skip over navigation
Home
Add New Question
Jobs
Products
Fibonacci function
Question posted
in
Computer Software
on
05 2010
Rate question difficulty level
5
1 Votes
Write Fibonacci function to calculate Nth value in Fibonacci sequence
Add an answer
Share
|
6 Answers
David Hollowell
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
Guest
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
Guest
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
Guest
private void Febonecci(int f1,int f2, int N)
{
Console.WriteLine(f2);
if (f2 >= N)
return;
Febonecci(f2, f1 + f2, N);
}
07/13/2010
Guest
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
Guest
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
Enter the code shown above:
please wait..
Company:
Microsoft
Location:
United States
Tags:
developer
,
programming
Occupations:
Software\System Architecture
,
Software\Web Development
add a question
Now hiring!
Software Development Engineer, Principal IT Job
Microsoft
Issaquah, WA
a highly motivated and experienced SharePoint Software Development Engineer who is passionate about developing and ... as a coding practitioner in software engineering required Experience with Agile Development is a plus...
---------------------------
Software Development Engineer II-BING
Microsoft Bing
San Francisco, CA
o At least two years of experience in industrial software development o Strong C++ coding and object oriented design ... Segments: Computer Science, Developer, Engineer, Engineering, Research, Software Engineer, Technology, User...
---------------------------
Senior IT Software Development Engineer Job
Microsoft
Issaquah, WA
IT We are looking for talented and highly motivated Sr. Software Development Engineer (Sr. SDE) to drive design, ... Marketing services team is part of Microsoft Marketing IT organization that manages the Microsoft.com that reaches...
---------------------------
Software Development Engineer II, MSN
Microsoft Bing
Bellevue, WA
and end user needs Advocate for and experience with agile development Quality driven. Test, test, test. Unit, TDD. ... Developer, Editor, Engineer, Engineering, Java, Publishing, Software Engineer, Technology, Test Engineer, Testing, Web...
---------------------------
Senior IT Software Development Engineer (SDE) Job
Microsoft
Redmond, WA
IT Do you want to drive changes that can affect the development and testing communities across Microsoft? The ... in software development through all phases 4-6 years development experience with C#, C++, Asp.Net and Windows...
---------------------------
Principal IT Software Development Engineer-Azure Job
Microsoft
Quincy, WA
IT Location: Quincy, WA, US Job ID: 787628-80076 Division: IT Are you passionate about building industry-leading, ... based experience with implementation, architecture and development highly preferred Proven track record of shipping...
---------------------------
Software Development Engineer in Test - BING
Microsoft Bing
San Francisco, CA
Bings high quality bar? The Bing team is looking for a software developer in test who can take on the challenge of ... or Commensurate experience, and minimum of 5+ years of software development / testing experience. Candidate must...
---------------------------
IT Software Development Engineer (SDE) 2 Job
Microsoft
Redmond, WA
it easy. We make it work. Were looking for an experienced IT Software Design Engineer to play a key role in ... Required Qualifications: Minimum 5 years of software development experience for enterprise line of business...
Jobs
by
Simply
Hired
View all jobs