Fibonacci Sequence - Calculate Nth Number

Apr 29, 2014

Write a recursive method that calculates the Nth number in the Fibonacci sequence. The first and second numbers in the sequence (the base cases) are both 1. After that, each subsequent number is the sum of the previous two. Stated a bit more formally:

fib(n)={1fib(n−1)+fib(n−2)n<2otherwise

For example, here is the first few numbers in the sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Your fib method should be part of a class named Fibonacci. In addition to the fib method, the Fibonacci class should have a main method that calls fib(9). If the result doesn't equal 34, you should print an error message. Otherwise, it should print out a message saying that it was successful.

After writing your Fibonacci class, answer the following question: How many times is the fibonacci method called when calculating the 5th number in the sequence?

View Replies


ADVERTISEMENT

Finding Nth Number In Fibonacci Sequence

Oct 30, 2014

I have to find where in the fibonacci sequence a at number belongs, using a if or while loop.

Example

>55 is a Fibonacci number whose order in the sequence is 11
>35 is not a Fibonacci number. However, it lies between Fibonacci numbers 34 (order: 10) and 55 (order: 11)

import java.util.Scanner;
public class While {
public static void main(String[] args) {
System.out.println("Welcome to the Fibonacci Sequence Detector");
Scanner in = new Scanner(System.in);

[Code] .....

View Replies View Related

Calculate Nth Fibonacci Number And Returns That To User

May 1, 2014

I have to create a program that calculates the nth Fibonacci number and returns that to the user. Fibonacci said his number sequence would describe the ideal breeding patterns of immortal rabbits. So, you are going to make this vision a reality.

First, take in a numeric value from the user and calculate that value in the fibonacci series. Next, find an image of a rabbit and display the image on a GUI (put the image as an icon on a label!) the number of times returned by the algorithm (Put all the aforementioned labels on one panel with FlowLayout!).

You need to remove the old images from the Panel. Probably the easiest way to do this is to create a whole new panel and remove the old one (hint: the remove method of JPanel should come in handy)

You could use an array of JLabels
You will need to create a new JLabel and add each member of the array to the panel
I should be able to scroll to see any images that are off screen

I am having difficultly on to making the array list for JLabel, and getting the Fibonacci sequence to show the pictures of rabbits. Below is my current code.

import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rabbit extends JFrame

[Code] ....

View Replies View Related

YES / NO Loop In Fibonacci Sequence

Mar 23, 2014

Fibonacci Sequence Code:

public class FibSeqByIanNeumann {
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int ctr, num1, num2, fib, maxTimes;
System.out.print("How many sequences do you want?: ");
maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
 
[code]....

how to do a simple YES/NO Loop so I can try to have it work on my code if I want to try to do the Fibonacci Sequence again.Now I think it might have something to do with a do/while loop.

View Replies View Related

Fibonacci Sequence Using Dynamic Arrays

Feb 12, 2014

public class Fibonacci {
public static void main(String[] args) {
int[] numbers;
numbers = new int[20];
numbers[0] = 0;
numbers[1] = 1;
System.out.println("

[Code] ....

I wrote this program for my Java class to print out the first 20 numbers of the Fibonacci series. My assignment then tells me to Rewrite your program using dynamic array. I'm not sure how to do this.

View Replies View Related

Display Values In Fibonacci Sequence From F0 To F15

Apr 15, 2014

I am attempting a programming exercise to display the values in the Fibonacci sequence from F0 to F15. I understand the concept, but, for some reason my equation is simply creating a resulting string of numbers that simply increase by 2's. As, I know it is supposed to be the sum of the previous F and the F that precedes that one to total the new F number. It seems so simple yet I seem to be far off. As usual, I have worked my code for your review.

/*
* This program calculates the "Fibonacci sequence."
* A "sentinel" is used to limit the extent the calculation.
*/

import acm.program.*;
public class bookFibonacciTest2a extends ConsoleProgram {
/* Specifies the limit value of the calculations */
private static final int SENTINEL = 16;
public void run() {
println ("This program display Fibonacci sequence numbers 0 - 15.");

[Code] .....

View Replies View Related

Finding Nth Term In Fibonacci Sequence Using While Loop

Oct 30, 2014

I have to find where in the fibonacci sequence a at number belongs, using a while loop.

Example

>55 is a Fibonacci number whose order in the sequence is 11
>35 is not a Fibonacci number. However, it lies between Fibonacci numbers 34 (order: 10) and 55 (order: 11)

import java.util.Scanner;
public class While
{
public static void main(String[] args)
{
System.out.println("Welcome to the Fibonacci Sequence Detector");

[Code] ....

View Replies View Related

Fibonacci Application - Store Its Sequence In Array

Mar 12, 2014

Modify the Improved Fibonacci application to store its sequence in an array. Do this by creating a new class to hold both the value and a boolean value that says whether the value is even, and then having an array of object references to objects of that class.

Did I just need to declaring the variable in other class (for boolean value and the value itself) or else ?

Here is the code for ImprovedFibonacci.java

Java Code:

class ImprovedFibonacci {
static final int MAX_INDEX = 9;
/**
* Print out the first few Fibonacci numbers,
* marking evens with a '*'
*/
public static void main(String[] args) {
int lo = 1;
int hi = 1;
String mark;

[Code] ....

View Replies View Related

Fibonacci Sequence - Find Sum Of Even-valued Terms

May 14, 2015

I am not sure what is wrong with my code? The code produces a negative number when I type in 4000000 into the fib method parameter.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Java Code:

public static void fib(int k) {
int result = 0;
int[] array = new int[k];
array[0] = 1;
array[1] = 2;
for (int i = 2; i < k; i++) {

[Code] ....

View Replies View Related

JUnit Test - Dynamic Fibonacci Rabbits Sequence

May 28, 2014

I'm facing a Problem with the JUnit Test for a Fibonacci rabbits sequence. The JUnit Test should test if the function dynFib(int x) completes the calucation in time. The time given is 100ms. The sequence I wanted to be printed is 0 1 1 2 3 4 6 8 11 15 and I got it but the calculation takes more than 100ms. How can I make it calculate faster without using a loop?

I want to do it recursively and dynamically, I kept trying lots of methods but they did not work.

This is my Code:

public class TestFib {
private static int dynFib(int x, Integer[] array) {
array = new Integer[x + 1];
if (x < 0) {
throw new IllegalArgumentException();

[Code] .....

View Replies View Related

Program That Calculates Fibonacci Number

Nov 19, 2014

Its a program that calculates Fibonacci number.This program uses recursion.

import java.util.Scanner;
public class FibonacciMemoization{ 
static int[] fib = new int[60];
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a Number :");
int number = input.nextInt();

[code]...

View Replies View Related

Fibonacci Number - Recursive Function

Jul 8, 2014

I wrote this tail recursive function that mirrors the iterative version, except that the loop in the iterative version is replaced by an if statement here and then a recursive call. Is this truly recursive? I have seen the fibo(n-1) + fibo(n - 2) version, but is this also an acceptable recursive solution? Why is it never solved this way?

public class FiboRecursive {
public static int fibo (int n) {
int sum = 0;
int n1 = 1;
int n2 = 1;
if (n == 1 || n == 2) {
sum = 1;

[Code] ...

View Replies View Related

Determine If User Input Is Fibonacci Number Or Not

Oct 20, 2014

Part A: While Loop Program

Write a program that detects Fibonacci numbers. Prompt the user to input a positive integer. Upon input, the program will determine if the number is either a Fibonacci number or not. If a Fibonacci number, then the order of the number in the sequence must be output. If not a Fibonacci number, then the Fibonacci numbers above and below it (including their order in the sequence) must be output. Once it finishes, the program will prompt the user for a new number. The program will exit if the user enters a string (such as “quit”) instead of an integer. Use the sample output file, fib-seq-det.txt, to view a sample session

This is my project, I wrote a programs that tells you if the input number is a fibonacci number or not. For some reason it only works for some Fibonacci numbers but not all of them.

import java.util.Scanner;
public class While
{
public static void main(String[] args) {
System.out.println("Welcome to the Fibonacci Sequence Detector
");
Scanner in = new Scanner(System.in);
System.out.print("Please input a number for analysis: ");
int input = in.nextInt();

[Code] ....

View Replies View Related

Fibonacci Series Program - Exiting For Loop Early If Exceed Specified Number

Feb 13, 2015

I'm trying to change the code on a Fibonacci series program that would allow me to exit the loop early if I exceed a specified number. The user enters any 2 random numbers (which will be the 1st 2 no.'s of the Fibonacci sequence printed to screen) and then continues up to a 'limit' on the number of numbers set in code. Here's the code:

int[] array = new int[limit]; //Define an array whose length is set by an int value for limit!!
array[0] = x; //User supplies a int value for x which takes the 1st position in the array!!
array[1] = y; //...and an int value for y in the 2nd position!!
 for (int i = 2; i < limit; i++) //Start from the 3rd position of the array when carrying out calculations!! {
array[i] = array[i-1] + array[i-2];

[Code] ....

To exit the code/ 'limit' early if the array prints a number higher than 100, I tried putting a 'while' condition before the last line, as follows:

while (array[i] < 100)
System.out.print(array[i] + " ");

Can I even use a 'while' loop within an array, or is there some other way I need to integrate it?

View Replies View Related

Create Array That Produces Triangular Number Sequence?

Nov 12, 2014

My code complies but it won't run and I'm not sure why. ArrayIndexOutOfBoundsException.

I'm trying to create an array that produces the Triangular number sequence:1, 3, 6, 10, 15, 21, 28, 36, 45, ...

Attached image(s)

View Replies View Related

Calculate Total Length Of N Number Of Bamboo Trees In M Number Of Seasons

Feb 20, 2014

In a forest, there are some bamboo trees .The length of each tree get doubled during winter and increases by one unit in summer , write a Java program to calculate the total length of n number of bamboo trees in M number of seasons. The season always starts with winter.

import java.util.Scanner;
public class Tree {
public static void main(String args[]) {
int length;
int season;

[Code] ....

View Replies View Related

Calculate SUM And AVERAGE Of ODD Number Digits Between 25 And 75

Jun 19, 2014

You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive).

The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.

The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately

a) Write a sequential (non-parallel) program that will accomplish above task.
b) Write a concurrent (parallel) program that will produce the result of the above task.

View Replies View Related

Calculate The Factors Of A Positive Integer Number

Mar 16, 2014

lines 7, 8, &12 "primes" are underline in red (prime cannot be resolved) is what pops up when i hover over the x's.

i don't get why that is.

package assignment7;
public class Exercise3
{
public static void main(String[] args)
{
Prime.setSize(1000);
for (int p = Primes.next(); p < 30; p = Primes.next())

[Code]...

View Replies View Related

Calculate Factors Of Positive Integer Number

Mar 17, 2014

Lines 7, 8, &12 "primes" are underline in red (prime cannot be resolved) is what pops up when i hover over the x's.

I don't get why that is.

Java Code :

public class Exercise3
{
public static void main(String[] args) {
Prime.setSize(1000);
for (int p = Primes.next(); p < 30; p = Primes.next()) {
int n = (int)Math.round(Math.pow(2,p)) - 1;
System.out.printf("%d 2^%d-1%d", p, p, n);
if (Primes.isPrime(n))

[Code] ....

View Replies View Related

Java Code To Calculate Variance Of A Set Of Number?

May 2, 2014

I'm trying to write a java code that calculates the varince of a set of number and I wanted to know if this is the proper way to do it.

for (int i = 0; i <= (myArray.length - 1); i++){
variance = Math.pow((myArray[i] - average), 2);

}

View Replies View Related

Get Certain Number Of Items And Then Calculate How Much Every Item Cost

Jan 4, 2015

This is supposed to get a certain number of items, and then calculate how much every item costs. It adds up the sum of those prices and checks if it is over 150$, if it is, it returns true, if it's not, it returns false.

import java.util.*;
class Cust
{
public static boolean DelPay (int a) {
Scanner reader=new Scanner (System.in);
int b,s=0;
for (int i=1;i<=a;i++) {
b=reader.nextInt();
s=s+b;
}
if (s>150)
return true;
return false;
}
}

View Replies View Related

Parallel Program - Calculate SUM And AVERAGE Of Odd Number Digits Between 25 And 75

Jun 11, 2014

You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive). The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.

The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately

a) Write a sequential (non-parallel) program that will accomplish above task.
b) Write a concurrent (parallel) program that will produce the result of the above task.

View Replies View Related

Unable To Print Out Results Of Program That Calculate Number Of Seats

Nov 11, 2014

I'm trying to print out the results of a program that calculates the number of seats the parties will get in an election.I have to print the partial results and the national results.

I can print te number of seats per party in each constituency, but how can i sum all seats per party in each constituency and print the national results?I'm working with vectors, which I know it might not be the best option, but everything is working, except the fact that I can't loop throuhg the vector and retrieve the total sum per party.Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.Is it possible, or doIhave to create a method in Parties class to solve the problem?

This is what I have now.

for (Parties p : h.geral) {
show += String.format("Constituency - %5s - %5s - %d%n",
p.getConstituency(), p.getParty(), p.getNum_seats());
}

View Replies View Related

Calculate Number Of Square Feet In A Tract Of Land With 3.5 Acres

Jan 27, 2014

1. Land Calculation

One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of square feet in a tract of land with 3.5 acres. Hint: Multiply the size of the tract of land (in acres) by the number of square feet in an acre to get the number of square feet in the tract.

a) You must have a variable called nrAcres where you will store the number of acres in the track.

b) Output must look as follows:

Size of land in acres: 3.5
Size of land in square feet: 152460

c) if the value of the variable nrAcres is changed the output should change accordingly. In other words: it would be wrong to have the following statement:

System.out.println("Size of land in square feet: 152460");

previous statement will print the correct output but will not change if you change the value of nrAcres.

View Replies View Related

Calculate Number Of Square Feet In A Tract Of Land With 3.5 Acres

Jan 27, 2014

Land Calculation

One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of square feet in a tract of land with 3.5 acres. Hint: Multiply the size of the tract of land (in acres) by the number of square feet in an acre to get the number of square feet in the tract.

a) You must have a variable called nrAcres where you will store the number of acres in the track.
b) Output must look as follows:

Size of land in acres: 3.5 Size of land in square feet: 152460

c) if the value of the variable nrAcres is changed the output should change accordingly. In other words: it would be wrong to have the following statement:

System.out.println("Size of land in square feet: 152460");

previous statement will print the correct output but will not change if you change the value of nrAcres.

this is what i got so far

public class Land
{
public static void main(String[] args){
double nrAcres = 3.5;
int feetInAcre = 43560;
double feetInTract = nrAcres * feetInAcre;
System.out.println("Size of land in acres: " + nrAcres + "
Size of land in square feet: " + feetInTract);
}
}

i tried to compile it but it says

error: class names, Land, are only accepted if annotation is explicitly requested 1 error

what is wrong with my code

View Replies View Related

User Enter A Number For Width And Length - Calculate Area And Perimeter

Apr 27, 2014

I am trying to make a program so that the user has to enter a number for the width and length and it will give the area and perimeter:

import java.util.* ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class jframe extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 300;

[Code] ....

It is giving me an error saying that the ExitButtonHandler and ebHandler do not have classes but I don't understand why.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved