answersLogoWhite

0


Best Answer

The formula for the Fibonacci series is

Fn = Fn-1 + Fn-2 for n ≥ 2 with F0 = 0 and F1 = 1.

In layman's terms, in the Fibonacci series each successive number is the sum of the previous two numbers, starting with 1. So we have 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc

To do this in Java or any computer program you will need to create a recursive program, one that picks up previous values. This is quite easy, even for a beginner, once one grasps the Fibonacci fundamentals. Here is an example in Java:

public class Fibonacci {

public static long fib(int n) {

if (n <= 1) return n;

else return fib(n-1) + fib(n-2);

}

public static void main(String[] args) {

int N = Integer.parseInt(args[0]);

for (int i = 1; i <= N; i++)

System.out.println(i + ": " + fib(i));

}

}

For n, enter the number of values required, otherwise the increasing values of the results will overflow the parameters of the data display!

This code can be adapted for any progamming language, e.g. pascal, basic etc. One can even use the principles to construct a spreadsheet that will show the Fibonacci series in successive rows in Excel, for example.

  • For more information, enhanced development and downloadable Java code for generating Fibonacci numbers, see 'Related links' below this box
User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

// Print out the first N Fibonacci numbers in the order:

// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765

public static final void printFib(final int N) {

int f0 = 0;

int f1 = 1;

for (int i = 0; i < N; ++i) {

System.out.println(f0);

final int temp = f1;

f1 += f0;

f0 = temp;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Well, you might use recursion, but that would be exceedingly inefficient. A loop would be much better in this case. The only advantage I see is for didactical purposes, that you practice recursion. Something like this; I didn't try it:

// Call the class MyMath.fib()

int fib(int number)
{
if (number <= 2) return 1;
else return fib(number - 1) + fib(number - 2);

}

Note: A recursive function (or method) is one that invokes itself.

Well, you might use recursion, but that would be exceedingly inefficient. A loop would be much better in this case. The only advantage I see is for didactical purposes, that you practice recursion. Something like this; I didn't try it:

// Call the class MyMath.fib()

int fib(int number)
{
if (number <= 2) return 1;
else return fib(number - 1) + fib(number - 2);

}

Note: A recursive function (or method) is one that invokes itself.

Well, you might use recursion, but that would be exceedingly inefficient. A loop would be much better in this case. The only advantage I see is for didactical purposes, that you practice recursion. Something like this; I didn't try it:

// Call the class MyMath.fib()

int fib(int number)
{
if (number <= 2) return 1;
else return fib(number - 1) + fib(number - 2);

}

Note: A recursive function (or method) is one that invokes itself.

Well, you might use recursion, but that would be exceedingly inefficient. A loop would be much better in this case. The only advantage I see is for didactical purposes, that you practice recursion. Something like this; I didn't try it:

// Call the class MyMath.fib()

int fib(int number)
{
if (number <= 2) return 1;
else return fib(number - 1) + fib(number - 2);

}

Note: A recursive function (or method) is one that invokes itself.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Here is a function to generate the nth Fibonacci number, using a recursion:

public long F (int n)

{

if (n 1)

{

return n;

}

else

{

return F (n - 1) + F (n - 2);

}

}

Note that if n < 0, the function doesn't work. You could also program it by the algebraic formula for the nth Fibonacci number, as follows:

public long F (int n)

{

double a = Math.sqrt (5); //to save code

return (long) (Math.pow ((1 + a) / 2, n) - Math.pow ((1 - a) / 2, n)) / a;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The Fibonacci series is not a good candidate for solution using a multithreaded program. This is because each term is dependent on the prior two terms, making it impossible to keep more than one thread unblocked at any one moment of time.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Set two variables "a" and "b" to the starting number of the Fibonacci series, for example, 1 and 1. Then, repeatedly (i.e., in a loop) set:

sum = a + b;

b = a;

a = sum;

... and print the sum (or equivalently, variable "a").

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Well, you might use recursion, but that would be exceedingly inefficient. A loop would be much better in this case. The only advantage I see is for didactical purposes, that you practice recursion. Something like this; I didn't try it:

// Call the class MyMath.fib()

int fib(int number)
{
if (number <= 2) return 1;
else return fib(number - 1) + fib(number - 2);

}

Note: A recursive function (or method) is one that invokes itself.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Here is a recursive way of writing Fibonacci series

f(n) = f(n-1) + f(n-2)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Multithreaded program generates Fibonacci series using java-answer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What mean by multithreaded program?

A multithreaded program is one that has multiple threads in execution. They may execute parallel to one another or totally without relation to one another. In Java you can create multithreaded programs using Java threads.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


What components of program state are shared across threads in a multithreaded process?

Obviously Heap Memory


Program that generates and displays the Fibonacci?

/*WAP to display Fibonacci series*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int i,a=0,b=1,c; scanf("%d",&amp;n); printf("%d\n%d",a,b); for(i=0;i&lt;n;i++) { c=a+b; a=b; b=c; printf("\n%d",c); } getch(); }


Write a program that generates the Fibonacci series up to 200?

class Program { static void Main(string[] args) { int n1, n2, n3,i; n1 = 0; n2 = 1; for (i = 1; i &lt;= 20; i++) { n3 = n1 + n2; if (n3 &lt;= 200) { Console.WriteLine(n3); n1 = n2; n2 = n3; } } Console.ReadKey(); } }


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


Write a program using while loop that generates Fabonaci series up to 200?

//to generate Fibonacci series upto a range of 200....(in C).... #include&lt;stdio.h&gt; main() { int a,b,c,i; a=0; b=1; printf("\n FIBONACCI SERIES .....\t"); i=1; while(i&lt;=(200-2)) { c=a+b; printf("\t%d",c); a=b; b=c; i++; } }


Can you explain the program of Fibonacci series in c?

You mean you have written a program, but you don't understand it? Well, how could I explain it without seeing it?


What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


Program to generate Fibonacci series using storage class in c plus plus?

i dn't know. haha


Why java called as multithreaded programming?

Java is called multithreaded because it allows the programmer to define multiple threads of execution manually. The main program would continue to execute as one thread and we can speed up the processing by declaring individual threads. Threads in Java can be created in two ways: 1. By extending the Thread class &amp; 2. By implementing the Runnable interface


What is lex tool?

Lex is a parser generator tool. Its basically a program that generates the lexers.