Question

In: Computer Science

Complete the following methods in java: Consult the rest of the methods of BigInteger from its...

Complete the following methods in java:

Consult the rest of the methods of BigInteger from its API documentation as needed to solve the following problems.

public static List<BigInteger> fibonacciSum(BigInteger n)

The unique breakdown into Fibonacci numbers can be constructed with a greedy algorithm that simply finds the largest Fibonacci number f that is less than or equal to n. Add this f to the result list, and break down the rest of the number given by the expression n-f the same way. This method should create and return some kind of List that contains the selected Fibonacci numbers in descending order. For example, when called with n = 1000000, this method would return a list that prints as [832040, 121393, 46368, 144, 55].

Your method must remain efficient even if n contains thousands of digits. To achieve this, maintain a list of Fibonacci numbers that you have generated so far initialized with

private static List<BigInteger> fibs = new ArrayList<>();

static { fibs.add(BigInteger.ONE); fibs.add(BigInteger.ONE); }

and then whenever the last Fibonacci number in this list is not big enough for your present needs, extend the list with the next Fibonacci number that you get by adding the last two known Fibonacci numbers. Keeping all your Fibonacci numbers that you have discovered so far in one sorted list would also allow you to do things such as using Collections.binarySearch to quickly determine if something is a Fibonacci number.

Solutions

Expert Solution

Code

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.List;

public class Temp {

    

    public static List<BigInteger> fibonacciSum(BigInteger n, ArrayList<BigInteger>fibList){

        if(n.compareTo(new BigInteger("0"))<=0)

            return new ArrayList<>();

        for(int i=fibList.size()-1;i>=0;i--){

            if(n.compareTo(fibList.get(i))>=0){

                List<BigInteger> prev = fibonacciSum(n.subtract(fibList.get(i)), fibList);

                prev.add(fibList.get(i));

                return prev;

            }

        }

        return new ArrayList<>();

    }

    public static void main(String[] args) {

        BigInteger n = new BigInteger("1000000");

        BigInteger a = new BigInteger("0");

        BigInteger b = new BigInteger("1");

        BigInteger c = new BigInteger("1");

        ArrayList<BigInteger>fibList = new ArrayList<>();

        while(c.compareTo(n)<=0){

            c = a.add(b);

            fibList.add(c);

            a = b;

            b = c;

        }

        List<BigInteger>fs = fibonacciSum(n,fibList);

        for(BigInteger bx:fs){

            System.out.println(bx.toString());

        }

    }

}


Related Solutions

In Java: Complete the following methods in the template by adhering to the comments: // TO...
In Java: Complete the following methods in the template by adhering to the comments: // TO DO: add your implementation and JavaDoc public class BetterArray<T> { private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array, you MUST use this for full credit // ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public BetterArray() { //constructor //initial capacity of the array should be DEFAULT_CAPACITY } @SuppressWarnings("unchecked") public BetterArray(int initialCapacity) { // constructor...
Methods – Compute Grade Please write a complete Java program, given the following information about (a...
Methods – Compute Grade Please write a complete Java program, given the following information about (a few lines of code in) main: projectAverage = getAverage(”Project”); // returns average of 2 grades testAverage = getAverage(”Test”); // returns average of 2 grades displayGrade(projectAverage, testAverage); // test are 70% & projects 30%
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from...
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from some input text file: Total number of characters used,without counting spaces and punctuation, total number of words used; Number of words, counting each word only once; Total number of punctuation characters;Number of words that are of size six or more;Number of words that are used only once
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over the idea of a search. * * @author liberato * * @param <T> : generic type. */ public abstract class Searcher<T> { protected final SearchProblem<T> searchProblem; protected final List<T> visited; protected List<T> solution; /**    * Instantiates a searcher.    *    * @param searchProblem    *            the search problem for which this searcher will find and   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT