简体   繁体   中英

How to print the fibonacci sequence in reverse order WITHOUT using a loop

I got this question in an interview. It's easy until the part when the interviewer wants me to not use the loop I used in the print method. number of terms is an input, when it's 7, For example: print 13 8 5 3 2 1 1. He said it's easy in Python but I can write the mechanism in Java too but I can't think of which mechanism he may be referring to. Thank you!

My Java code:

public class Fibonacci {
    private int[] a;

    private int fib(int i) {
        assert (i>=0);

        if (a[i]==0) {
            if (i==0 || i==1) {
                a[i] = 1;
            } else {
                a[i] = fib(i - 2) + fib(i - 1);
            }
        }

        return a[i];
    }

    public Fibonacci(int numberTerms) {
        if (numberTerms<2) throw new IllegalArgumentException("expect at least 2 terms for a Fibonacci sequence");
        a = new int[numberTerms];
    }

    public void print() {
        for (int i=a.length; i!=0; i--) {
            System.out.println(fib(i-1));
        }
    }

    public static void main(String[] args) {
        Fibonacci f = new Fibonacci(7);
        f.print();
    }
}
public static int f(int n){
    if (n <= 1)
        return n;
    else 
        return f(n-1) + f(n-2);
}

static void printReversedFib(int x){
    if(x <= 1)
        System.out.println(f(x));
    else{
        System.out.println(f(x));
        printReverseFib(x-1);
    }
}

Testing with printReversedFib(7); will print:

13
8
5
3
2
1
1

Presumably, you could have made a recursive print ; that is this -

public void print() {
  for (int i=a.length; i!=0; i--) {
    System.out.println(fib(i-1));
  }
}

Could have been something like,

public void print() {
  print(a.length - 1);
}

and

public void print(int i) {
  if (i > 0) {
    System.out.println(fib(i - 1));
    print(i - 1);
  }
}

When I run the above, I get the requested output of

13
8
5
3
2
1
1

I did the reverse way like that. Who could enhance it? All suggestions are welcome.

    package com.jbjares.dynamicProgramming;

    public class Fibonacci {


    public static void main(String[] args) {
            new Fibonacci().calc(317811,1);
    }

    public void calc(Integer start,Integer end){
                    Integer f = (int) (start/((1+Math.sqrt(5))/2));
                    System.out.println(f);
                    if(f==end){
                            return;
                    }
                    calc(++f,end);
    }

}

Result: 196417 121393 75025 46368 28657 17711 10946 6765 4181 2584 1597 987 610 377 233 144 89 55 34 21 13 8 5 3 2 1

Cheers!

You can solve this easily using DP (Dynamic Prog.) All you need to do is create the DP array and then you can iterate the array in reverse order. Below is the solution in Go. http://play.golang.org/p/3m1n_AUSZl

func FibPrintRev(n int) {
    space := make([]int, n+1)
    // base case
    space[1] = 1
    space[2] = 1

    // Create dp array
    for i := 3; i <= n; i++ {
      space[i] = space[i-1] + space[i-2]
    }
    // Iterate in rev. order
    for i := n; i > 0; i-- {
      fmt.Println(space[i])
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM