📜  卢卡斯数字

📅  最后修改于: 2021-09-22 10:10:26             🧑  作者: Mango

卢卡斯数类似于斐波那契数。卢卡斯数也被定义为其前两项之和。但这里的前两项是 2 和 1,而在斐波那契数列中,前两项分别是 0 和 1。
在数学上,卢卡斯数可以定义为:
{\displaystyle L_{n}:={\begin{cases}2&{\text{if }}n=0;\\1&{\text{if }}n=1;\\L_{n-1}+L_{n-2}&{\text{if }}n>1.\\\end{cases}}}
卢卡斯数字采用以下整数序列:
2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123 ………..
写一个函数int卢卡斯(INT n)的N作为参数并返回第n Lucas数。
例子 :

Input : 3
Output : 4

Input : 7
Output : 29

方法一(递归求解)
下面是一个基于简单递归公式的递归实现。

C++
// Recursive C/C++ program
// to find n'th Lucas number
#include 
 
// recursive function
int lucas(int n)
{
    // Base cases
    if (n == 0)
        return 2;
    if (n == 1)
        return 1;
 
    // recurrence relation
    return lucas(n - 1) +
        lucas(n - 2);
}
 
// Driver Code
int main()
{
    int n = 9;
    printf("%d", lucas(n));
    return 0;
}


Java
// Recursive Java program to
// find n'th Lucas number
 
class GFG
{
 
    // recursive function
    public static int lucas(int n)
    {
 
        // Base cases
        if (n == 0)
            return 2;
        if (n == 1)
            return 1;
 
        // recurrence relation
        return lucas(n - 1) +
               lucas(n - 2);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 9;
        System.out.println(lucas(n));
    }
}
// This code is contributed
// by Nikita Tiwari.
Python3 # Recursive Python 3 program 
# to find n'th Lucas number

# recursive function
def lucas(n) :
    
    # Base cases 
    if (n == 0) :
        return 2
    if (n == 1) :
        return 1

    # recurrence relation 
    return lucas(n - 1) + lucas(n - 2) 


# Driver code
n = 9
print(lucas(n))

# This code is contributed by Nikita Tiwari.


C#
// Recursive C# program to
// find n'th Lucas number
using System;
 
class GFG {
 
    // recursive function
    public static int lucas(int n)
    {
 
        // Base cases
        if (n == 0)
            return 2;
        if (n == 1)
            return 1;
 
        // recurrence relation
        return lucas(n - 1) + lucas(n - 2);
    }
 
    // Driver program
    public static void Main()
    {
 
        int n = 9;
 
        Console.WriteLine(lucas(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Iterative C/C++ program
// to find n'th Lucas Number
#include 
 
// Iterative function
int lucas(int n)
{
    // declaring base values
    // for positions 0 and 1
    int a = 2, b = 1, c, i;
 
    if (n == 0)
        return a;
 
    // generating number
    for (i = 2; i <= n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}
 
// Driver Code
int main()
{
    int n = 9;
    printf("%d", lucas(n));
    return 0;
}


Java
// Iterative Java program to
// find n'th Lucas Number
class GFG
{
    // Iterative function
    static int lucas(int n)
    {
        // declaring base values
        // for positions 0 and 1
        int a = 2, b = 1, c, i;
 
        if (n == 0)
            return a;
 
        // generating number
        for (i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 9;
        System.out.println(lucas(n));
    }
}
 
// This code is contributed
// by Nikita tiwari.


Python3
# Iterative Python 3 program
# to find n'th Lucas Number
 
# Iterative function
def lucas(n) :
 
    # declaring base values
    # for positions 0 and 1
    a = 2
    b = 1
     
    if (n == 0) :
        return a
  
    # generating number
    for i in range(2, n + 1) :
        c = a + b
        a = b
        b = c
     
    return b
     
  
# Driver Code
n = 9
print(lucas(n))
 
# This code is contributed
# by Nikita tiwari.


C#
// Iterative C# program to
// find n'th Lucas Number
using System;
 
class GFG {
 
    // Iterative function
    static int lucas(int n)
    {
 
        // declaring base values
        // for positions 0 and 1
        int a = 2, b = 1, c, i;
 
        if (n == 0)
            return a;
 
        // generating number
        for (i = 2; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
 
        return b;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 9;
 
        Console.WriteLine(lucas(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

76

方法二(迭代求解)
上述实现的时间复杂度是指数级的。我们可以使用迭代优化它以在 O(n) 时间内工作。

C++

// Iterative C/C++ program
// to find n'th Lucas Number
#include 
 
// Iterative function
int lucas(int n)
{
    // declaring base values
    // for positions 0 and 1
    int a = 2, b = 1, c, i;
 
    if (n == 0)
        return a;
 
    // generating number
    for (i = 2; i <= n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}
 
// Driver Code
int main()
{
    int n = 9;
    printf("%d", lucas(n));
    return 0;
}

Java

// Iterative Java program to
// find n'th Lucas Number
class GFG
{
    // Iterative function
    static int lucas(int n)
    {
        // declaring base values
        // for positions 0 and 1
        int a = 2, b = 1, c, i;
 
        if (n == 0)
            return a;
 
        // generating number
        for (i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 9;
        System.out.println(lucas(n));
    }
}
 
// This code is contributed
// by Nikita tiwari.

蟒蛇3

# Iterative Python 3 program
# to find n'th Lucas Number
 
# Iterative function
def lucas(n) :
 
    # declaring base values
    # for positions 0 and 1
    a = 2
    b = 1
     
    if (n == 0) :
        return a
  
    # generating number
    for i in range(2, n + 1) :
        c = a + b
        a = b
        b = c
     
    return b
     
  
# Driver Code
n = 9
print(lucas(n))
 
# This code is contributed
# by Nikita tiwari.

C#

// Iterative C# program to
// find n'th Lucas Number
using System;
 
class GFG {
 
    // Iterative function
    static int lucas(int n)
    {
 
        // declaring base values
        // for positions 0 and 1
        int a = 2, b = 1, c, i;
 
        if (n == 0)
            return a;
 
        // generating number
        for (i = 2; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
 
        return b;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 9;
 
        Console.WriteLine(lucas(n));
    }
}
 
// This code is contributed by vt_m.

PHP


Javascript


输出 :

76