📜  卢卡斯数字

📅  最后修改于: 2021-04-24 14:27:20             🧑  作者: 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 lucas(int n) n作为参数,并返回第n个卢卡斯数。
例子 :

Input : 3
Output : 4

Input : 7
Output : 29

方法1(递归解决方案)
以下是基于简单递归公式的递归实现。

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

方法2(迭代解决方案)
上述实现的时间复杂度是指数的。我们可以使用迭代优化它以在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.

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


Java脚本


输出 :

76