📜  佩林数字程序

📅  最后修改于: 2021-05-06 21:34:48             🧑  作者: Mango

Perrin数字是以下整数序列中的数字。
3、0、2、3、2、5、5、7、10、12、17、22、29、39…
用数学术语来说,Perrin数的序列p(n)由递归关系定义

P(n) = P(n-2) + P(n-3) for n > 2, 

with initial values
    P(0) = 3, P(1) = 0, P(2) = 2. 

编写一个函数int per(int n)返回p(n)。例如,如果n = 0,则per()应返回3。如果n = 1,则应返回0如果n = 2,则应返回2。对于n> 2,应返回p(n-2 )+ p(n-3)

方法1(使用递归:指数)
下面是上述公式的简单递归实现。

C++
// n'th perrin number using Recursion'
#include 
using namespace std;
 
int per(int n)
{
    if (n == 0)
        return 3;
    if (n == 1)
        return 0;
    if (n == 2)
        return 2;
    return per(n - 2) + per(n - 3);
}
 
// Driver code
int main()
{
    int n = 9;
    cout << per(n);
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
// n'th perrin number using Recursion'
#include 
int per(int n)
{
    if (n == 0)
        return 3;
    if (n == 1)
        return 0;
    if (n == 2)
        return 2;
    return per(n - 2) + per(n - 3);
}
 
// Driver code
int main()
{
    int n = 9;
    printf("%d", per(n));
    return 0;
}


Java
// Java code for n'th perrin number
// using Recursion'
import java.io.*;
 
class GFG {
 
    static int per(int n)
    {
        if (n == 0)
            return 3;
        if (n == 1)
            return 0;
        if (n == 2)
            return 2;
        return per(n - 2) + per(n - 3);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int n = 9;
 
        System.out.println(per(n));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 code for n'th perrin
# number using Recursion'
 
# function return n'th
# perrin number
def per(n):
 
    if (n == 0):
        return 3;
    if (n == 1):
        return 0;
    if (n == 2):
        return 2;
    return per(n - 2) + per(n - 3);
 
# Driver Code
n = 9;
print(per(n));
     
# This code is contributed mits


C#
// C# code for n'th perrin number
// using Recursion'
using System;
 
class GFG {
 
    static int per(int n)
    {
        if (n == 0)
            return 3;
        if (n == 1)
            return 0;
        if (n == 2)
            return 2;
        return per(n - 2) + per(n - 3);
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 9;
 
        Console.Write(per(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Optimized C++ program for n'th perrin number
#include 
using namespace std;
int per(int n)
{
    int a = 3, b = 0, c = 2, i;
    int m;
    if (n == 0)
        return a;
    if (n == 1)
        return b;
    if (n == 2)
        return c;
    while (n > 2) {
        m = a + b;
        a = b;
        b = c;
        c = m;
        n--;
    }
    return m;
}
 
// Driver code
int main()
{
    int n = 9;
    cout << per(n);
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
// Optimized C program for n'th perrin number
#include 
int per(int n)
{
    int a = 3, b = 0, c = 2, i;
    int m;
    if (n == 0)
        return a;
    if (n == 1)
        return b;
    if (n == 2)
        return c;
    while (n > 2) {
        m = a + b;
        a = b;
        b = c;
        c = m;
        n--;
    }
    return m;
}
 
// Driver code
int main()
{
    int n = 9;
    printf("%d", per(n));
    return 0;
}


Java
// Optimized Java program for n'th perrin number
import java.io.*;
 
class GFG {
 
    static int per(int n)
    {
        int a = 3, b = 0, c = 2, i;
        int m = 0;
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        if (n == 2)
            return c;
        while (n > 2) {
            m = a + b;
            a = b;
            b = c;
            c = m;
            n--;
        }
        return m;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 9;
 
        System.out.println(per(n));
    }
}
 
// This code is contributed by vt_m.


C#
// Optimized C# program for n'th perrin number
using System;
 
class GFG {
 
    static int per(int n)
    {
        int a = 3, b = 0, c = 2;
 
        // int i;
        int m = 0;
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        if (n == 2)
            return c;
 
        while (n > 2) {
            m = a + b;
            a = b;
            b = c;
            c = m;
            n--;
        }
 
        return m;
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 9;
 
        Console.WriteLine(per(n));
    }
}
 
// This code is contributed by vt_m.


PHP
 2)
    {
        $m = $a + $b;
        $a = $b;
        $b = $c;
        $c = $m;
        $n--;
    }
    return $m;
}
 
    // Driver code
    $n = 9;
    echo per($n);
     
// This code is contributed by ajit
?>


Javascript


输出:

12

我们看到,在此实现中,以下递归树中进行了大量重复的工作。

per(8)   
                       /           \     
               per(6)             per(5)   
              /      \             /     \
        per(4)      per(3)        per(3)    per(2)
       /     \        /    \        /  \  
   per(2)   per(1)  per(1) per(0) per(1) per(0)

方法2 :(优化:线性)

C++

// Optimized C++ program for n'th perrin number
#include 
using namespace std;
int per(int n)
{
    int a = 3, b = 0, c = 2, i;
    int m;
    if (n == 0)
        return a;
    if (n == 1)
        return b;
    if (n == 2)
        return c;
    while (n > 2) {
        m = a + b;
        a = b;
        b = c;
        c = m;
        n--;
    }
    return m;
}
 
// Driver code
int main()
{
    int n = 9;
    cout << per(n);
    return 0;
}
 
// This code is contributed
// by Akanksha Rai

C

// Optimized C program for n'th perrin number
#include 
int per(int n)
{
    int a = 3, b = 0, c = 2, i;
    int m;
    if (n == 0)
        return a;
    if (n == 1)
        return b;
    if (n == 2)
        return c;
    while (n > 2) {
        m = a + b;
        a = b;
        b = c;
        c = m;
        n--;
    }
    return m;
}
 
// Driver code
int main()
{
    int n = 9;
    printf("%d", per(n));
    return 0;
}

Java

// Optimized Java program for n'th perrin number
import java.io.*;
 
class GFG {
 
    static int per(int n)
    {
        int a = 3, b = 0, c = 2, i;
        int m = 0;
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        if (n == 2)
            return c;
        while (n > 2) {
            m = a + b;
            a = b;
            b = c;
            c = m;
            n--;
        }
        return m;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 9;
 
        System.out.println(per(n));
    }
}
 
// This code is contributed by vt_m.

C#

// Optimized C# program for n'th perrin number
using System;
 
class GFG {
 
    static int per(int n)
    {
        int a = 3, b = 0, c = 2;
 
        // int i;
        int m = 0;
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        if (n == 2)
            return c;
 
        while (n > 2) {
            m = a + b;
            a = b;
            b = c;
            c = m;
            n--;
        }
 
        return m;
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 9;
 
        Console.WriteLine(per(n));
    }
}
 
// This code is contributed by vt_m.

的PHP

 2)
    {
        $m = $a + $b;
        $a = $b;
        $b = $c;
        $c = $m;
        $n--;
    }
    return $m;
}
 
    // Driver code
    $n = 9;
    echo per($n);
     
// This code is contributed by ajit
?>

Java脚本


输出:

12