Calculate fibonacci series in linear time

Posted on 2008-10-21
Last Modified: 2012-05-05
Is there a way to calculate the fibonacci series in linear time instead of O(n^2)?
Question by:ubuntuguy
  • 2
LVL 84

Expert Comment

Comment Utility
where n means the nth  fibonacci number?
If you can do additions in O(1) then yes.
LVL 31

Expert Comment

Comment Utility
= get yourself a really, really big ALU chip :)

Accepted Solution

javaexperto earned 500 total points
Comment Utility
This is linear I make it personally.
	public void fibonacci(int finish) {


		int n= finish;	

		int c= 0;	

		int e= 1;	

		int t= 0; 

		for (int i = 1; i <= n; i++) {


			System.out.print(c + " ");


			c= c+e;        

			e= t; 	



Open in new window

LVL 84

Expert Comment

Comment Utility
that's O(n) as long as f(n) is small enough to fit in an int
if adding large numbers is O(log(f(n)) then this method becomes O(n^2)

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Setup specific permissions in Microsoft Access 9 56
java continue statement 10 70
Problem to event 3 47
Not needed 13 53
If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now