Skip to content

Instantly share code, notes, and snippets.

@shelajev
Created August 10, 2021 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelajev/9660a76a68cbb7ac78a80095a3b98616 to your computer and use it in GitHub Desktop.
Save shelajev/9660a76a68cbb7ac78a80095a3b98616 to your computer and use it in GitHub Desktop.
package com.example;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
@Controller("/")
public class FibonacciController {
private static final StringBuilder LOG = new StringBuilder();
@Get(uri = "/nthFibonacci/{nth}", produces = MediaType.TEXT_PLAIN)
public String nthFibonacci(Integer nth) {
long[] counter = new long[] {0};
long start = System.currentTimeMillis();
LOG.append("Fibonacci number #").append(nth).append(" is ");
LOG.append(computeNthFibonacci(nth, counter));
LOG.append(" (computed in ").append(System.currentTimeMillis() - start).append(" ms, ").append(counter[0]).append(" steps)\n");
return LOG.toString();
}
private static long computeNthFibonacci(int nth, long[] counter) {
counter[0]++;
if (nth == 0 || nth == 1) return nth;
return computeNthFibonacci(nth - 1, counter) + computeNthFibonacci(nth - 2, counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment