📜  佩尔编号

📅  最后修改于: 2021-04-26 18:44:20             🧑  作者: Mango

佩尔数是类似于斐波那契数的数字,由以下公式生成

Pn = 2*Pn-1 + Pn-2 
with seeds P0 = 0 and P1 = 1

前几个Pell编号为0、1、2、5、12、29、70、169、408、985、2378、5741、13860、33461等。
编写一个返回P n的函数int pell(int n)。
例子:

Input : n = 4
Output :12

Input  : n = 7
Output : 169

方法1(使用递归)

C++
// Pell Number Series using Recursion in C++
#include 
using namespace std;
 
// calculate nth pell number
int pell(int n)
{
    if (n <= 2)
        return n;
    return 2 * pell(n - 1) + pell(n - 2);
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << " " << pell(n);
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// Pell Number Series using Recursion in C
#include 
 
// calculate nth pell number
int pell(int n)
{
    if (n <= 2)
        return n;
    return 2 * pell(n - 1) + pell(n - 2);
}
 
// driver function
int main()
{
    int n = 4;
    printf("%d", pell(n));
    return 0;
}


Java
// Pell Number Series using Recursion in JAVA
class PellNumber {
 
    // calculate n-th Pell number
    public static int pell(int n)
    {
        if (n <= 2)
            return n;
        return 2 * pell(n - 1) + pell(n - 2);
    }
 
    // driver function
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(pell(n));
    }
}


Python3
# Pell Number Series using
# Recursion in Python3
 
# Calculate nth pell number
def pell(n) :
    if (n <= 2) :
        return n
    return (2 * pell(n - 1) + pell(n - 2))
     
# Driver function
n = 4;
print(pell(n))
 
# This code is contributed by Nikita Tiwari.


C#
// Pell Number Series using Recursion in C#
using System;
 
class PellNumber {
 
    // calculate n-th Pell number
    public static int pell(int n)
    {
        if (n <= 2)
            return n;
        return 2 * pell(n - 1) + pell(n - 2);
    }
 
    // Driver function
    public static void Main()
    {
        int n = 4;
        Console.Write(pell(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Iterative Pell Number Series in C++
#include 
using namespace std;
 
// Calculate nth pell number
int pell(int n)
{
    if (n <= 2)
        return n;
 
    int a = 1;
    int b = 2;
    int c, i;
    for(i = 3; i <= n; i++)
    {
       c = 2 * b + a;
       a = b;
       b = c;
    }
    return b;
}
 
// Driver Code
int main()
{
    int n = 4;
     
    cout << pell(n);
    return 0;
}
 
// This code is contributed by nidhi_biet


C
// Iterative Pell Number Series in C
#include 
 
// calculate nth pell number
int pell(int n)
{
    if (n <= 2)
        return n;
 
    int a = 1;
    int b = 2;
    int c, i;
    for (i = 3; i <= n; i++) {
        c = 2 * b + a;
        a = b;
        b = c;
    }
    return b;
}
 
// driver function
int main()
{
    int n = 4;
    printf("%d", pell(n));
    return 0;
}


Java
// Iterative Pell Number Series in Java
class PellNumber {
 
    // calculate nth pell number
    public static int pell(int n)
    {
        if (n <= 2)
            return n;
        int a = 1;
        int b = 2;
        int c;
        for (int i = 3; i <= n; i++) {
            c = 2 * b + a;
            a = b;
            b = c;
        }
        return b;
    }
     
    // driver function
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(pell(n));
    }
}


Python
# Iterative Pell Number
# Series in Python 3
 
# calculate nth pell number
def pell(n) :
    if (n <= 2) :
        return n
  
    a = 1
    b = 2
    for i in range(3, n+1) :
        c = 2 * b + a
        a = b
        b = c
     
    return b
     
# driver function
n = 4
print(pell(n))
 
# This code is contributed by Nikita Tiwari.


C#
// Iterative Pell Number Series in C#
using System;
class PellNumber {
 
    // calculate nth pell number
    public static int pell(int n)
    {
        if (n <= 2)
            return n;
        int a = 1;
        int b = 2;
        int c;
        for (int i = 3; i <= n; i++) {
            c = 2 * b + a;
            a = b;
            b = c;
        }
        return b;
    }
     
    // Driver function
    public static void Main()
    {
        int n = 4;
        Console.Write(pell(n));
    }
}
 
// This code is contributed by vt_m.


PHP


输出 :

12

方法2(迭代)

C++

// Iterative Pell Number Series in C++
#include 
using namespace std;
 
// Calculate nth pell number
int pell(int n)
{
    if (n <= 2)
        return n;
 
    int a = 1;
    int b = 2;
    int c, i;
    for(i = 3; i <= n; i++)
    {
       c = 2 * b + a;
       a = b;
       b = c;
    }
    return b;
}
 
// Driver Code
int main()
{
    int n = 4;
     
    cout << pell(n);
    return 0;
}
 
// This code is contributed by nidhi_biet

C

// Iterative Pell Number Series in C
#include 
 
// calculate nth pell number
int pell(int n)
{
    if (n <= 2)
        return n;
 
    int a = 1;
    int b = 2;
    int c, i;
    for (i = 3; i <= n; i++) {
        c = 2 * b + a;
        a = b;
        b = c;
    }
    return b;
}
 
// driver function
int main()
{
    int n = 4;
    printf("%d", pell(n));
    return 0;
}

Java

// Iterative Pell Number Series in Java
class PellNumber {
 
    // calculate nth pell number
    public static int pell(int n)
    {
        if (n <= 2)
            return n;
        int a = 1;
        int b = 2;
        int c;
        for (int i = 3; i <= n; i++) {
            c = 2 * b + a;
            a = b;
            b = c;
        }
        return b;
    }
     
    // driver function
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(pell(n));
    }
}

Python

# Iterative Pell Number
# Series in Python 3
 
# calculate nth pell number
def pell(n) :
    if (n <= 2) :
        return n
  
    a = 1
    b = 2
    for i in range(3, n+1) :
        c = 2 * b + a
        a = b
        b = c
     
    return b
     
# driver function
n = 4
print(pell(n))
 
# This code is contributed by Nikita Tiwari.

C#

// Iterative Pell Number Series in C#
using System;
class PellNumber {
 
    // calculate nth pell number
    public static int pell(int n)
    {
        if (n <= 2)
            return n;
        int a = 1;
        int b = 2;
        int c;
        for (int i = 3; i <= n; i++) {
            c = 2 * b + a;
            a = b;
            b = c;
        }
        return b;
    }
     
    // Driver function
    public static void Main()
    {
        int n = 4;
        Console.Write(pell(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


输出:

12