give me a fibonnaci function in ts
- User
give me a fibonnaci function in ts
Assistant
Sure! Here is a Fibonacci function in TypeScript:
codefunction fibonacci(n: number): number { if (n <= 1) { return n; } return fibonacci(n - 1) + fibonacci(n - 2); }This function uses a recursive approach to calculate the nth Fibonacci number. It takes a single parameter
nand returns the corresponding Fibonacci number.Here's an example of how to use the function:
codeconsole.log(fibonacci(5)); // Output: 5 console.log(fibonacci(8)); // Output: 21Note that this function has a time complexity of O(2^n), which means it can become very slow for large values of n. For larger inputs, you may want to use a different approach, such as an iterative version of the Fibonacci sequence or a memoized version of the function
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.