Question

In: Computer Science

hi

hi

Solutions

Expert Solution

The first recursive algorithm which gorws exponentially is as follows:

​
public static int fibonacciArray[]=new int[50];
         // Traditional Fibonacci Algorithm
         public static int FibExponential(int x) {
                if(x==0 || x==1)
                        return x;
                return FibExponential(x-1) + FibExponential(x-2);
        }

​

The second recursive algorithm which grows in linear time is as follows:

public static int fibonacciArray[]=new int[50];​
​fibonacciArray[0]=1;
fibonacciArray[1]=1;

// Linear Fibonacci Algorithm
public static int FibLinear(int n){
                int fibValue=0;
                if(n==0 ){
                   return 0;
                }else if(n==1){
                   return 1;
                }else if(fibonacciArray[(int)n]!=0){
                   return fibonacciArray[(int)n];
                }else{
                   fibValue=FibLinear(n-1)+FibLinear(n-2);
                   fibonacciArray[(int) n]=fibValue;
                   return fibValue;
                }
        }

​

​

In order to run the code we need a Class which have a main method, The complete code which can be run is as follows:

import java.util.*;
class Demo {
         public static int fibonacciArray[]=new int[50];
         // Traditional Fibonacci Algorithm
         public static int FibExponential(int x) {
                if(x==0 || x==1)
                        return x;
                return FibExponential(x-1) + FibExponential(x-2);
        }

        // Linear Fibonacci Algorithm
        public static int FibLinear(int n){
                int fibValue=0;
                if(n==0 ){
                   return 0;
                }else if(n==1){
                   return 1;
                }else if(fibonacciArray[n]!=0){
                   return fibonacciArray[n];
                }else{
                   fibValue=FibLinear(n-1)+FibLinear(n-2);
                   fibonacciArray[n]=fibValue;
                   return fibValue;
                }
        }

        public static void main(String args[]) {
                fibonacciArray[0]=1;
                fibonacciArray[1]=1;
                int[] nlist = {5,10, 15, 20, 25, 30, 35, 40, 45};
                int index = 0;
                int[] fibarray1 = new int[nlist.length]; // to store the fibonacci numbers from FibLinear
                int[] fibarray2 = new int[nlist.length]; // to store the fibonacci numbers from FibExponential
                for (int n : nlist) {
                        if(index < nlist.length) {
                                fibarray1[index] = (int)FibLinear(n);
                                fibarray2[index] = FibExponential(n);
                                index++; 
                        }
                }
                System.out.println(Arrays.toString(fibarray1) + "\n" + Arrays.toString(fibarray2));
        }

}

Hope this works, Thank you


Related Solutions

If Hicksian demand function hi(p, ubar) > 0, then hi(p,ubar) is strictly decreasing in Pi.
If Hicksian demand function hi(p, ubar) > 0, then hi(p,ubar) is strictly decreasing in Pi.
Hi-Volt Components You are the IT manager at Hi-Voltage Components, a medium-sized firm that makes specialized...
Hi-Volt Components You are the IT manager at Hi-Voltage Components, a medium-sized firm that makes specialized circuit boards. Hi-Voltage's largest customer: Green Industries, recently installed a computerized purchasing system. If Hi-Voltage connects to the purchasing system, Green Industries will be able to submit purchase orders electronically. Although Hi-Voltage has a computerized accounting system, that system is not capable of handling EDI. Tasks 1. What options does Hi-Voltage have for developing a system to connect with Green Industries Purchasing system? 2....
The second order decomposition of HI has a rate constant of 1.80*10^-3/MS A. How much HI...
The second order decomposition of HI has a rate constant of 1.80*10^-3/MS A. How much HI remains after 72.5 s if the initial concentration of HI is 4.78m B. How long does it take to reduce the concentration by 25% if the initial concentration is 4.78m C. What is the half life at 4.78m initial concentration
Hi Tech Products manufactures three (3) types of CD players: Cheap, Econo and Deluxe. Hi Tech...
Hi Tech Products manufactures three (3) types of CD players: Cheap, Econo and Deluxe. Hi Tech uses an activity-based product costing system. The company has identified five (5) activities. Each activity, its cost and related activity driver are identified below: Activity                                                    Activity Cost                                            Activity Driver Material handling                                  $225 000                                                   Number of parts Material insertion                                 $2 475 000                                               Number of parts Automated machinery                          $840 000                                                   Machine hours Finishing                                                   $170 000                                                   Labour hours Packaging                                                 $170 000                                                   Orders shipped Total manufacturing cost                    $3...
Question 3 (30 marks) a. (15 marks) The finance director of Hi-Quality Productions (Hi-Q) is reviewing...
Question 3 a. The finance director of Hi-Quality Productions (Hi-Q) is reviewing the working capital management of the company. He is particularly concerned about the laxity in the accounts receivable collection process. The current credit terms of Hi-Q require customers to settle their bills within 30 days, but its customers are taking an average of 60 days to pay their bills. In addition, out of the total sales of $30m per year, the company suffers bad debts of $900,000 per...
Horst Inc. (HI) prepares personal loan pay-off plans for recent college graduates. HI has the following...
Horst Inc. (HI) prepares personal loan pay-off plans for recent college graduates. HI has the following budget for next year: Prepare 1000 plans. Incur costs as follows: Office rent for the year is $6000 Travel costs are estimated at $30 per plan Software costs $2000 per year plus $25 per plan. Printing and binding costs are $15 per plan. 1. How much will HI have to charge for each plan just to break-even for a year. Round to the nearest...
Marcus Lim, the cost accountant for Hi-Power Mower Company, recently installed activity-based costing2 at Hi-Power's St.
 Ethics in Managerial Accounting Marcus Lim, the cost accountant for Hi-Power Mower Company, recently installed activity-based costing2 at Hi-Power's St. Louis lawn tractor (riding mower) plant where three models, - the 8- horsepower Bladerunner, the 12-horsepower Quickcut, and the 18-horsepower Supercut- are manufactured. Marcus's new product costs for those three models show that the company's traditional costing system had been significantly under costing the 18-horsepower Supercut. This was due primarily to the lower sales volume of the Supercut compared to the Bladerunner...
Curtis Rich, the cost accountant for Hi-Power Mower Company, recently installed activity-based costing at Hi-Power's St....
Curtis Rich, the cost accountant for Hi-Power Mower Company, recently installed activity-based costing at Hi-Power's St. Louis lawn tractor (riding mower) plant where three models—the 8-horsepower Bladerunner, the 12-horsepower Quickcut, and the 18-horsepower Supercut—are manufactured. Curtis's new product costs for these three models show that the company's traditional costing system had been significantly undercutting the 18-horsepower Supercut. This was due primarily to the lower sales volume of the Supercut compared to the Bladerunner and the Quickcut. Before completing his analysis...
Bright Day Company produces two beverages, Hi-Voltage and EasySlim. Data about these products follow. Hi-Voltage EasySlim...
Bright Day Company produces two beverages, Hi-Voltage and EasySlim. Data about these products follow. Hi-Voltage EasySlim Production volume 10,000 bottles 240,000 bottles Liquid materials 800 gallons 31,000 gallons Dry materials 720 pounds 8,000 pounds Bottles 10,000 bottles 240,000 bottles Labels 3 labels per bottle 2 label(s) per bottle Machine setups 1,000 setups 550 setups Machine hours 130 MH 4,200 MH Additional data from its two production departments follow. Department Driver Cost Mixing department Liquid materials Gallons $ 1,908 Dry materials...
Bright Day Company produces two beverages, Hi-Voltage and EasySlim. Data about these products follow. Hi-Voltage EasySlim...
Bright Day Company produces two beverages, Hi-Voltage and EasySlim. Data about these products follow. Hi-Voltage EasySlim Production volume 10,000 bottles 280,000 bottles Liquid materials 1,000 gallons 37,000 gallons Dry materials 920 pounds 5,000 pounds Bottles 10,000 bottles 280,000 bottles Labels 3 labels per bottle 1 label(s) per bottle Machine setups 400 setups 600 setups Machine hours 220 MH 3,300 MH Additional data from its two production departments follow. Department Driver Cost Mixing department Liquid materials Gallons $ 2,280 Dry materials...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT