001package algs11;
002import stdlib.*;
003
004public class MyFibonacci {
005        /*
006  Assignment1:
007
008  -----------------------------------------------------------------------
009  1.1.16 Give the value of exR1(6):
010
011    public static String exR1(int n)
012    {
013       if (n <= 0) return "";
014       return exR1(n-3) + n + exR1(n-2) + n;
015    }
016
017  ANSWER:
018
019  -----------------------------------------------------------------------
020  1.1.18 Consider the following recursive function:
021
022    public static int mystery(int a, int b)
023    {
024       if (b == 0)     return 0;
025       if (b % 2 == 0) return mystery(a+a, b/2);
026       return mystery(a+a, b/2) + a;
027    }
028
029  What are the values of mystery(2, 25) and mystery(3, 11)?
030
031  ANSWER:
032
033  Given positive integers a and b, describe what value mystery(a, b) computes.
034
035  ANSWER:
036
037  Answer the same question, but replace + with * and replace return 0 with return 1.
038
039  ANSWER:
040
041  -----------------------------------------------------------------------
042  1.1.19 Run the function runF, below, on your computer.  Let it run for one hour.
043  You will get the printout for the first few values of N very quickly, but it
044  will slow down after a while.  What is the largest value of N that is printed
045  after one hour?
046
047  ANSWER:
048
049  Develop a better implementation of F(N) that saves computed values in an array of type "long[]".
050
051  ANSWER THIS PROBLEM BY COMPLETING THE FUNCTION myF, BELOW.
052
053         */
054
055        public static long F(int N) {
056                if (N == 0) return 0;
057                if (N == 1) return 1;
058                return F(N-1) + F(N-2);
059        }
060        public static void runF() {
061                for (int N = 0; N < 100; N++)
062                        StdOut.println(N + " " + F(N));
063        }
064
065        public static long myF(int N) {
066                // TODO
067                return 0;
068        }
069        public static void runMyF() {
070                for (int N = 0; N < 100; N++)
071                        StdOut.println(N + " " + myF(N));
072        }
073
074        // to run a single function, just comment out the others using two slashes
075        public static void main(String[] args) {
076                runF ();
077                // runMyF ();
078        }
079}
080