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...
Design a complete JAVA program with the following methods:- In the main method : 3 variables...
Design a complete JAVA program with the following methods:- In the main method : 3 variables are declared :- employee id, salary and status. A method that will allow user to input employee id and salary. A method that will assign status to “Taxable’ if employee’s salary is more than 2500RM or “Not Taxable” if salary is less and equal to 2500RM. A method that will display the employee’s id , salary and its status. Write the above Using Java.
Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size);...
Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size); public boolean isEmpty(); public boolean isFull(); public int size(); // Return number of elements in the Collection. public String toString(); public boolean add(T element); // Add an element into the Collection, return true if successful public boolean remove(T target); // Remove the target from Collection, return true if successful public boolean removeAll(T target); // Remove all occurrences of Target, return if successful public void...
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%
complete all methods in java! /* Note:   Do not add any additional methods, attributes.         Do...
complete all methods in java! /* Note:   Do not add any additional methods, attributes.         Do not modify the given part of the program.         Run your program against the provided Homework2Driver.java for requirements. */ /* Hint:   This Queue implementation will always dequeue from the first element of         the array i.e, elements[0]. Therefore, remember to shift all elements         toward front of the queue after each dequeue. */ public class QueueArray<T> {     public static int CAPACITY = 100;...
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 the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
Java. Complete the methods for infixToPostfix, postfixToInfix, solvePostfix. import java.util.Stack; public class B1Work {    /*...
Java. Complete the methods for infixToPostfix, postfixToInfix, solvePostfix. import java.util.Stack; public class B1Work {    /*    * Grading:    * Correctly converts infix to postfix - 3pts    */    public static String infixToPostfix(String infix) {        /*        * Convert an infix math formula to postfix format        * Infix format will always include parens around any two values and a symbol        * You will not need to imply any PEMDAS rules besides...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT