Prompt
Answer
Function: Fibonacci
This function generates a list of Fibonacci numbers using recursion.
Parameters
n
(required): An integer specifying the number of Fibonacci numbers to generate.
Return Type
list of integers
: A list containing the Fibonacci numbers.
Exceptions
ValueError
: Raised if the input parametern
is not a positive integer.
Code
let
Fibonacci = (n as number) as list =>
let
result =
if n = 1 then
{0}
else if n = 2 then
{0, 1}
else if n > 2 then
let
previous = Fibonacci(n-1),
second_previous = Fibonacci(n-2),
last_number = List.Last(previous) + List.Last(second_previous),
updated_result = previous & {last_number}
in
updated_result
else
error "Input parameter 'n' must be a positive integer."
in
result
in
Fibonacci
Usage Example
- Call the
Fibonacci
function with the desired number of Fibonacci numbers to generate. - The function will return a list containing the Fibonacci numbers.
Code
let
result = Fibonacci(10)
in
result
Output
{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
In this example, the Fibonacci
function is called with the parameter 10
. The function generates a list containing the first 10 Fibonacci numbers and returns it as the output. The resulting list is {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
.
Description
This function, called Fibonacci Generator, generates a list of Fibonacci numbers using recursion. It takes an input parameter, n, which specifies the number of Fibonacci numbers to generate. The function returns a list of integers, where each integer is a Fibonacci number. The Fibonacci numbers are generated recursively, with the base case being n = 1, where the function returns {0}. For n = 2, the function returns {0, 1}. For n > 2, the function calculates the previous Fibonacci numbers by calling itself recursively, adds the last numbers of the previous Fibonacci numbers, and returns the updated result. If the input parameter n is not a positive integer, a ValueError exception is raised.
More Code Generators
![Apache Flink Code Generator](https://fs.enterprisedna.co/library/icons/flink.png)
![Apache Pig Code Generator](https://fs.enterprisedna.co/library/icons/apache_pig.png)
![Azure Data Factory Code Generator](https://fs.enterprisedna.co/library/icons/azure-data-factory.png)
![C/C++ Code Generator](https://fs.enterprisedna.co/library/icons/c_c_plus.png)
![CouchDB Code Generator](https://fs.enterprisedna.co/library/icons/couchdb.png)
![DAX Code Generator](https://fs.enterprisedna.co/library/icons/power-bi.png)
![Excel Code Generator](https://fs.enterprisedna.co/library/icons/excel.png)
![Firebase Code Generator](https://fs.enterprisedna.co/library/icons/firebase.png)
![Google BigQuery Code Generator](https://fs.enterprisedna.co/library/icons/google_bigquery.png)
![Google Sheets Code Generator](https://fs.enterprisedna.co/library/icons/google_sheets.png)
![GraphQL Code Generator](https://fs.enterprisedna.co/library/icons/graphql.png)
![Hive Code Generator](https://fs.enterprisedna.co/library/icons/hive.png)
![Java Code Generator](https://fs.enterprisedna.co/library/icons/java.png)
![JavaScript Code Generator](https://fs.enterprisedna.co/library/icons/javascript.png)
![Julia Code Generator](https://fs.enterprisedna.co/library/icons/julia.png)
![Lua Code Generator](https://fs.enterprisedna.co/library/icons/lua.png)
![M (Power Query) Code Generator](https://fs.enterprisedna.co/library/icons/m.png)
![MATLAB Code Generator](https://fs.enterprisedna.co/library/icons/matlab.png)
![MongoDB Code Generator](https://fs.enterprisedna.co/library/icons/mongodb.png)
![Oracle Code Generator](https://fs.enterprisedna.co/library/icons/oracle.png)
![PostgreSQL Code Generator](https://fs.enterprisedna.co/library/icons/postgresql.png)
![Power BI Code Generator](https://fs.enterprisedna.co/library/icons/power-bi.png)
![Python Code Generator](https://fs.enterprisedna.co/library/icons/python.png)
![R Code Generator](https://fs.enterprisedna.co/library/icons/r.png)
![Redis Code Generator](https://fs.enterprisedna.co/library/icons/redis.png)
![Regex Code Generator](https://fs.enterprisedna.co/library/icons/regex.png)
![Ruby Code Generator](https://fs.enterprisedna.co/library/icons/ruby.png)
![SAS Code Generator](https://fs.enterprisedna.co/library/icons/sas.png)
![Scala Code Generator](https://fs.enterprisedna.co/library/icons/scala.png)
![Shell Code Generator](https://fs.enterprisedna.co/library/icons/shell.png)
![SPSS Code Generator](https://fs.enterprisedna.co/library/icons/spss.png)
![SQL Code Generator](https://fs.enterprisedna.co/library/icons/sql.png)
![SQLite Code Generator](https://fs.enterprisedna.co/library/icons/sqlite.png)
![Stata Code Generator](https://fs.enterprisedna.co/library/icons/stata.png)
![Tableau Code Generator](https://fs.enterprisedna.co/library/icons/tableau.png)
![VBA Code Generator](https://fs.enterprisedna.co/library/icons/vba.png)