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


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

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

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 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

Finding Longest Zig-Zag Sequence In Array

Oct 26, 2014

From a given array of positive and negative numbers, I have to find the longest SUB-Array which represents a Zig-Zag sequence...

A Zig-Zag sequence means that one number is possitive, the next one negative, the next one possitive, and so on and so on...

Like this: -1, 4, -5, 6, -9, 2, -9 etc....

So that means, if the array looks like this:

1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3

the longest sub-array which fulfills the requirement is (the part in bold):

1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3

and I only need it's length, which in this case is: 8

View Replies View Related

Finding Longest Increasing Sequence Not Giving Proper Result

Jan 28, 2015

Okay so I need to be able to read a file and then be able to find the longest sequence of increasing numbers.

So if the file was this, (this is Ass1Q2_test4.txt)

97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24

the output should be,

(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96

Greatest path is of length 10.

Now, the code that I have works, kind of. Instead of recurring several times at each point, it only recurs once.

So say I'm looking at (1,1). With (1,1) being 57. The area around it looks like this.

97 47 56
35 57 41
89 36 98

Now when I look at it, there are several paths it can take. It can go 57, 97 or 57, 89 or 57, 98. However, I'm pretty sure that it just uses the first one that corresponds with the first if statement that is valid. So I start checking north of the value, then northeast, then east, then southeast, which at southeast is where I find my first greater than value. After it finds it's first valid number, it then continues from that number, instead of checking if there are other longer paths stemming from the original value.

In conjunction with that, you can see that the printout just returns all paths from each value. Which isn't what I want. I need a way to store the longest current path, then check each path after to see if it's longer. If it is, it's replaced, if not, it stays the same.

I've also attached Ass1Q2_test4.txt

import java.util.*;
import java.io.*;
public class MaxIncreasingSub {

[Code].....

View Replies View Related

For Loop For Fibonacci Code

Sep 22, 2014

I need to write a For Loop that prints out the first 12 Fibonacci numbers:1 1 2 3 5 8 13 21 34 55 89 144.The problem I am having is that I can not get the first two 1 numbers. I only get 1.2.3.5.8.....

int a=1;
int b=1;
for (int i=1; i<12;i++)
{
System.out.print(a+" ");
a=a+b;
b=a-b;
}

Do I need to add another For Loop that sub-tracks so that I can get the first digit of 1?

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

Major Scale And Sequence Loop

Jan 30, 2015

I'm trying to figure out how to print the Major Scale of any give note in music. It works as follows:

There are 12 notes and each note is assigned a number

C = 0, C# = 1, D = 2, D# = 3, E = 4, F = 5, F# = 6, G = 7, G# = 8, A = 9, Bb = 10, B = 11.

After picking a random note, say F, the idea is to add the following sequence to move through the scale:

0, +2, +2, +1, +2, +2, +2, +1.

So, with F being 5, we move through the scale as 5, 7, 9, 10, 0, 2, 4, 5.

The theory is unimportant but, as you can see, the sequence loops back on itself, rather than continue past 11. This is where I'm stuck, though. I'm not clear on how to loop my numbers around in code. I'll show my attempt here:

public static void main(String[] scale) {
majorScaleOf(0); //0 represents C!!
majorScaleOf(5); //5 represents F!!
majorScaleOf(10); //Bb = B flat!!

[Code] ....

For the sake of simplicity I haven't shown the entire script of 'if' statements, but you get the idea.

The print out reads as:

0, 2, 4 (For C)
5, 7, 9 (For F)
10 (For Bb)

The last line demonstrates the problem, as I need it read as 10, 0, 2.

I've tried different approaches, but wanted to convey the basic problem with this post.

View Replies View Related

Finding Array Index Number Through For Loop

Jan 29, 2014

int[] a = new int[7];
a[0] = 0;
a[1] = 10;
a[2] = 256;
a[3] = 57;
a[4] = 33;
a[5] = -154;
a[6] = 168;

[code]....

What program needs to find is the most biggest number. It does the job, but another task of the program is to find the index of that number . The second loop should do just that, but for some reason, as the loop goes further, it passes through the if statement even though answer "a[i]" is not equal to "answer". The idea is that if a[i] and answer are equal, the "i" should represent the index number.

View Replies View Related

Finding Most Common Character In A String Using For Loop

Feb 7, 2007

import javax.swing.*;
public class mostFrequent{
public static void main(String args[]){
char index;
String s;
s = JOptionPane.showInputDialog("Enter String here");
int currFrequency = 0;
for(index = 0; index<s.length(); index = index++){
int i = 0;
for(i = 'A'; i<='Z'; i++){
if(i==s.charAt(index)){
currFrequency = currFrequency + 1;
}
}
}
System.out.println("end");
}
}
//my code so far

View Replies View Related

Output Term Frequency-inverse Document (TFIDF) Matrix

Apr 7, 2014

I have this code that outputs the tfidf for all words in each file in the directory. I'm trying to transfer this to a matrix where each row correspond to each file in the directory and each column to all words in the files and I have some difficulty in doing it . Here is my try

public class TestTF_IDF {
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException{
//Test code for TfIdf
TfIdf tf = new TfIdf("E:/Thesis/ThesisWork/data1");
//Contains file name being processed
String file;
tf.buildAllDocuments();

[Code] .....

View Replies View Related

Fibonacci Code In Java

Nov 30, 2014

how do we get the following numbers (1 2 3 5 8 13 21 34 55 89) out of the following code? I can't get my head around this loop....

HTML Code:

public class Fabnoci{
public static void main(String[] args)
{
int n=10,i,f0=1,f1=1,f2=1;
for(i=1;i<=n;i++)
{
f2=f0+f1;

[code]....

View Replies View Related

Break Down Fibonacci Method

Jan 21, 2014

public class E09_Fibonacci {
static int fib(int n) {
if (n <= 2)
return 1;
return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
// Get the max value from the command line:
int n = Integer.parseInt(args[0]);
if(n < 0) {
System.out.println("Cannot use negative numbers");
return;
}
for(int i = 1; i <= n; i++)
System.out.print(fib(i) + ", ");
}
}

please break down fib method shown above. I can't understand how the magic is happening inside that recursion.

View Replies View Related

How To Write First 30 Fibonacci Numbers

Jun 29, 2014

I want to write the first 30 Fibonacci numbers so wrote this script:

Java Code:

int num1 = 1;
int num2 = 1;
int num3;
String x = num1 + ", " + num2;
for (int i=1; i==28; i++){
num3 = num1 + num2;
x += ", " + num3;
num1 = num2;
num2 = num3;
}
System.out.println(x); mh_sh_highlight_all('java');

This script outputs this only: 1, 1

View Replies View Related

How To Start Fibonacci Series In Particular Range

Jun 11, 2014

How to print 10 numbers in Fibonacci serious aftet given number?

Like 13 21 34...

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

Fibonacci Series Using Java Multithreading

Feb 8, 2014

Program to generate Fibonacci series program using java multithreading.

View Replies View Related

Write A Program To Print Fibonacci Series Up To 100

Dec 31, 2014

My code:

public class Fibonacci {
/**
* @param args
*/
public static void main(String[] args) {
int f[]=new int[100];
for(int j=0; j<100;j++){
f[j]=j;

[Code] ....

How can I improve my code?

View Replies View Related

Print Fibonacci Series Till User Wants It

Jun 12, 2014

I wrote java program to print fibonacci series till user wants it! But I'm getting this compilation error -

fibo.java:Incompatible types
found:java,lang.String
required:int
n=s.readline();

import java.awt.*;
import java.io.*;
import java.util.*;
class fibo {
public static void main(String args[])throws IOException {
int arr[]=new int[100];

[Code] ......

View Replies View Related







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