Created
August 10, 2021 19:29
-
-
Save shelajev/9660a76a68cbb7ac78a80095a3b98616 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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