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

90.2K

Verified Solution

Question

Programming

Complete the following methods in java:

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

public static List fibonacciSum(BigIntegern)

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

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

private static List fibs = newArrayList<>();

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

and then whenever the last Fibonacci number in this list is notbig enough for your present needs, extend the list with the nextFibonacci number that you get by adding the last two knownFibonacci numbers. Keeping all your Fibonacci numbers that you havediscovered so far in one sorted list would also allow you to dothings such as using Collections.binarySearch to quickly determineif something is a Fibonacci number.

Answer & Explanation Solved by verified expert
4.1 Ratings (757 Votes)
Codeimport javamathBigIntegerimport javautilArrayListimport javautilListpublic class Temp public    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students