find the nth lucas number in csharp

main.cs
public static int LucasNumber(int n) {
    if (n == 0) {
        return 2;
    } 
    if (n == 1) {
        return 1;
    } 
    return LucasNumber(n-1) + LucasNumber(n-2);
}
175 chars
10 lines

Explanation:

  • Lucas Number is a sequence similar to Fibonacci where each term is the sum of the previous two terms.
  • The first two terms of the Lucas Number sequence are 2 and 1.
  • The function LucasNumber takes an integer n and returns the nth Lucas Number.
  • The first two if statements check for the base cases where n is 0 or 1.
  • If n is not 0 or 1, then the function calls itself recursively to calculate the nth Lucas Number.

gistlibby LogSnag