Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

computer science 1000 In mathematics, the Fibonacci numbers are the numbers in t

ID: 675082 • Letter: C

Question

computer science 1000

In mathematics, the Fibonacci numbers are the numbers in the following sequence: 1, 1,2, 3, 5, 8,13,21,34, 55,... By definition, the first two numbers in the Fibonacci sequence are 1 and 1. The rest numbers can be defined as the sum of the previous two numbers. The question may arise whether a positive integer x is a Fibonacci number. This is true if and only if one (or both) of (5x^2+4) or (5x^2-4) is a perfect square. The main() method will just include a simple loop which asks users to input an integer number and call an identification method to determine whether the input integer number is a Fibonacci number or not. After each identification process the program will keep asking user to make input until user input "-1". To process each user input, write a method that will take an integer number as parameter and returns true/false if the integer is/isn't a Fibonacci number. You can use the following method declaration: public static boolean isFibNumber(int input) Sample Output Fibonacci Number Identifier (enter -1 to quite) Please enter a number: 21 [Enter] 21 is a Fibonacci number. Please enter a number: 9 [Enter] 9 is not a Fibonacci number. Please enter a number: 5 [Enter] 5 is a Fibonacci number. Please enter a number: -1 [Enter] Goodbye. :) End of processing... In mathematics, the Fibonacci numbers are the numbers in the following sequence: 1, 1,2, 3, 5, 8,13,21,34, 55,... By definition, the first two numbers in the Fibonacci sequence are 1 and 1. The rest numbers can be defined as the sum of the previous two numbers. The question may arise whether a positive integer x is a Fibonacci number. This is true if and only if one (or both) of (5x^2+4) or (5x^2-4) is a perfect square. The main() method will just include a simple loop which asks users to input an integer number and call an identification method to determine whether the input integer number is a Fibonacci number or not. After each identification process the program will keep asking user to make input until user input "-1". To process each user input, write a method that will take an integer number as parameter and returns true/false if the integer is/isn't a Fibonacci number. You can use the following method declaration: public static boolean isFibNumber(int input) Sample Output Fibonacci Number Identifier (enter -1 to quite) Please enter a number: 21 [Enter] 21 is a Fibonacci number. Please enter a number: 9 [Enter] 9 is not a Fibonacci number. Please enter a number: 5 [Enter] 5 is a Fibonacci number. Please enter a number: -1 [Enter] Goodbye. :) End of processing... In mathematics, the Fibonacci numbers are the numbers in the following sequence: 1, 1,2, 3, 5, 8,13,21,34, 55,... By definition, the first two numbers in the Fibonacci sequence are 1 and 1. The rest numbers can be defined as the sum of the previous two numbers. The question may arise whether a positive integer x is a Fibonacci number. This is true if and only if one (or both) of (5x^2+4) or (5x^2-4) is a perfect square. The main() method will just include a simple loop which asks users to input an integer number and call an identification method to determine whether the input integer number is a Fibonacci number or not. After each identification process the program will keep asking user to make input until user input "-1". To process each user input, write a method that will take an integer number as parameter and returns true/false if the integer is/isn't a Fibonacci number. You can use the following method declaration: public static boolean isFibNumber(int input) Sample Output Fibonacci Number Identifier (enter -1 to quite) Please enter a number: 21 [Enter] 21 is a Fibonacci number. Please enter a number: 9 [Enter] 9 is not a Fibonacci number. Please enter a number: 5 [Enter] 5 is a Fibonacci number. Please enter a number: -1 [Enter] Goodbye. :) End of processing...

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Fibonacci {
   public static boolean check(int val)
   {
       int a = 5*val*val+4;
       double temp = Math.sqrt(a);       
       if (temp == Math.floor(temp))
       {
           return true;
       }
       a = 5*val*val-4;
       temp = Math.sqrt(a);       
       if (temp == Math.floor(temp))
       {
           return true;
       }
       return false;
   }
   public static void main(String[] args) throws IOException
   {
       System.out.println("Fiobonacci number indentifier (enter -1 to quit)");
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       while(true)
       {
           System.out.print("Please enter an integer: ");
           String s = br.readLine();
           int val = Integer.parseInt(s);
           if(val==-1)
           {
               System.out.println("Goodbye :)");
               System.out.println("End of processing");
               break;
           }
           if(check(val))
           {
               System.out.print(val);
               System.out.println(" is a Fibonacci number.");
           }
           else
           {
               System.out.print(val);
               System.out.println(" is not a Fibonacci number.");
           }
       }
   }
}