Lucas numbers are a sequence of numbers similar to the Fibonacci sequence. The sequence starts with 2 and 1 and then each number is the sum of its two preceding numbers. In Rust, you can write a recursive function to find the nth Lucas number using the formula:
L_n = L_{n-1} + L_{n-2}
where L_0 = 2 and L_1 = 1.
Here's an implementation of the recursive function:
main.rs118 chars8 lines
This function takes an argument n
which is the number of the Lucas number to find. It returns the nth Lucas number.
To use this function, you can simply call it with the desired value of n
:
main.rs116 chars6 lines
This will output:
main.rs30 chars2 lines
gistlibby LogSnag