Posted on by Kalkicode
Code Number

# Perrin number program

Perrin number is a sequence of integers that appears in mathematics, specifically in number theory. The sequence is named after the French mathematician Charles Perrin, who studied it in the late 19th century.

The Perrin sequence is defined recursively as follows:

• P(0) = 3
• P(1) = 0
• P(2) = 2

For n > 2:

• P(n) = P(n-2) + P(n-3)

The first few terms of the sequence are: 3, 0, 2, 3, 2, 5, 5, 7, 10, 12, 17, 22, 29, 39, 51, 68, 90, 119, 158, 209, 277, 367, 486, 644, 853, 1130, 1497, 1983, 2627, 3480, 4610, 6107, 8090, 10717, 14203, 18837, 24910, 33004, 43721, 57918, 76725, 101902, 135301, 179007, 237210, 314122, 416020, 551123, 730083, 967834, 1282123, 1698557, 2250583, 2986070, 3951736, 5236318, 6935377, 9180849, 12157656, 16100072, 21323463, 28284473, 37442160, 49639326, 65726167, 87019735, 115305688, 152751042, 202307126, 268447617, 355912506, 472373137, 625616516, 828884731, 1098700566, 1455486358, 1929001539, 2554747854, 3383569450, 4482276411, 5937758937, 7865847430, 10420033462, 13793400701, 18290633953, 24245408866, 32101107782, 42522842609, 56288613171, 74543513762, 98743656438, 130862161913, 173402488753, 229483821666, 304006671738, 402204423401, 532356902962, 705812928710, 934997752601, 1237940039288, 1640303518417, 2172200787705, 2876683704570, 3800757770178, 5039544497095, 6672812386743, 8833137197226, 11699186156569, 15504723619579, 20536423309722, 27188132120184, 35972724122707, 47620480621250, 63088692241530, 83693904761817, 110888162911419, 146897848727370, 194498979882950, 257792463251581, 341273443377963, 452386975711536, 599042064660210, 793358558240113, 1050747522956746, 1390519850341930, 1841356366466217, 2438427434438667, 3227682943051158, .. etc

## Perrin number program in java

``````/*
Java program for
Display perrin number sequence
*/
public class Number
{
public int perrin(int n)
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return perrin(n - 2) + perrin(n - 3);
}
// Print n perrin number
public void printPerrin(int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print("  " + perrin(i));
}
}
public static void main(String[] args)
{
// Test
}
}``````
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in c++

``````// Include header file
#include <iostream>

//  Stdc++11 program for
//  Display perrin number sequence
class Number
{
public:
int perrin(int n)
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return perrin(n - 2) + perrin(n - 3);
}
// Print n perrin number
void printPerrin(int n)
{
for (int i = 0; i < n; ++i)
{
std::cout << "  " << perrin(i);
}
}
};
int main(int argc, char **argv){
// Test
return 0;
};``````
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in c

``````// Include header file
#include <stdio.h>
#include <stdlib.h>
//  C program for
//  Display perrin number sequence

int perrin (int n)
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return perrin(n - 2) + perrin(n - 3);
}
// Print n perrin number
void  printPerrin (int n)
{
for (int i = 0; i < n; i++)
{
printf("%d  " , perrin(i));
}
}

int main()
{
// Test
printPerrin(20);
return 0;
}``````
Output
``3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in golang

``````package main
import "fmt"

//  Golang program for
//  Display perrin number sequence

func perrin(n int)int {
// Base case
if (n < 0) {
return 0;
} else if (n == 0) {
return 3;
} else if (n == 1) {
return 0;
} else if (n == 2) {
return 2;
}
// Return next parrin number
return perrin(n - 2) + perrin(n - 3);
}
// Print n perrin number
func printPerrin(n int) {
for  i  := 0; i < n; i++ {
fmt.Printf("%d  ",perrin(i));
}
}
func main() {
// Test
printPerrin(20);
}``````
Output
``````3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209
``````

## Perrin number program in c#

``````// Include namespace system
using System;
//  C# program for
//  Display perrin number sequence
public class Numbers
{
public int perrin(int n)
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return this.perrin(n - 2) + this.perrin(n - 3);
}
// Print n perrin number
public void printPerrin(int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write("  " + this.perrin(i));
}
}
public static void Main(String[] args)
{
// Test
}
}``````
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in vb.net

``````' Include namespace system
Imports System
'  Vb.net program for
'  Display perrin number sequence
public Class Numbers
Public Function  perrin(ByVal n As Integer) As Integer
' Base case
if (n < 0) Then
Return  0
ElseIf (n = 0) Then
Return  3
ElseIf (n = 1) Then
Return  0
ElseIf (n = 2) Then
Return  2
End If
' Return next parrin number
Return  Me.perrin(n - 2) + Me.perrin(n - 3)
End Function
' Print n perrin number
Public Sub printPerrin(ByVal n As Integer)
With Nothing
Dim i As Integer = 0
While i < n
Console.Write("{0}  ", Me.perrin(i))
End While
End With
End Sub
Public Shared Sub Main(ByVal args As String())
Dim task As Numbers = New Numbers()
' Test
End Sub
End Class``````
Output
``3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in php

``````<?php
//  Php program for
//  Display perrin number sequence
class Numbers
{
function perrin(\$n)
{
// Base case
if (\$n < 0)
{
return 0;
}
else if (\$n == 0)
{
return 3;
}
else if (\$n == 1)
{
return 0;
}
else if (\$n == 2)
{
return 2;
}
// Return next parrin number
return \$this->perrin(\$n - 2) + \$this->perrin(\$n - 3);
}
// Print n perrin number
function printPerrin(\$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo "  ",\$this->perrin(\$i);
}
}
}
// Test
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in node js

``````//  Node Js program for
//  Display perrin number sequence
class Numbers
{
perrin(n)
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return this.perrin(n - 2) + this.perrin(n - 3);
}
// Print n perrin number
printPerrin(n)
{
for (var i = 0; i < n; ++i)
{
console.log("  " + this.perrin(i));
}
}
}

// Start program execution
// Test
Output
``````  3
0
2
3
2
5
5
7
10
12
17
22
29
39
51
68
90
119
158
209``````

## Perrin number program in typescript

``````//  Typescript program for
//  Display perrin number sequence
class Numbers
{
public number perrin(n:number)
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return this.perrin(n - 2) + this.perrin(n - 3);
}
// Print n perrin number
public  printPerrin(n:number)
{
for (var i = 0; i < n; ++i)
{
console.log("  " + this.perrin(i));
}
}
}
// Test
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/``````
Output
``````  3
0
2
3
2
5
5
7
10
12
17
22
29
39
51
68
90
119
158
209``````

## Perrin number program in python

``````#  Python 3 program for
#  Display perrin number sequence
class Numbers :
def  perrin(self, n) :
# Base case
if (n < 0) :
return 0
elif(n == 0) :
return 3
elif(n == 1) :
return 0
elif(n == 2) :
return 2
# Return next parrin number
return self.perrin(n - 2) + self.perrin(n - 3)
# Print n perrin number
def printPerrin(self, n) :
i = 0
while (i < n) :
print(self.perrin(i), end ="  ")
i += 1

if __name__=="__main__":
# Test
Output
``3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in ruby

``````#  Ruby program for
#  Display perrin number sequence
class Numbers
def perrin( n)
# Base case
if (n < 0)
return 0
elsif (n == 0)
return 3
elsif (n == 1)
return 0
elsif (n == 2)
return 2
end
# Return next parrin number
return self.perrin(n - 2) + self.perrin(n - 3)
end
# Print n perrin number
def printPerrin( n)
i = 0
while (i < n)
print("  ",  self.perrin(i))
i += 1
end
end

end
# Test
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in scala

``````//  Scala program for
//  Display perrin number sequence
class Numbers ()
{
def perrin(n : Int) : Int=
{
// Base case
if (n < 0)
{
return 0
}
else if (n == 0)
{
return 3
}
else if (n == 1)
{
return 0
}
else if (n == 2)
{
return 2
}
// Return next parrin number
return perrin(n - 2) + perrin(n - 3)
}
// Print n perrin number
def printPerrin(n : Int) : Unit=
{
var i = 0
while (i < n)
{
print("  " + perrin(i))
i += 1
}
}
}

object Main
{
def main(args : Array[String]) : Unit=
{
// Test
}
}``````
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in swift

``````import Foundation
//  Swift program for
//  Display perrin number sequence
class Numbers
{
func perrin(_ n: Int) -> Int
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return self.perrin(n - 2) + self.perrin(n - 3);
}
// Print n perrin number
func printPerrin(_ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(self.perrin(i), terminator: "  ");
i += 1;
}
}
}
// Test
Output
``3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Perrin number program in kotlin

``````//  Kotlin program for
//  Display perrin number sequence
class Numbers {
fun perrin(n : Int) : Int
{
// Base case
if (n < 0)
{
return 0;
}
else if (n == 0)
{
return 3;
}
else if (n == 1)
{
return 0;
}
else if (n == 2)
{
return 2;
}
// Return next parrin number
return this.perrin(n - 2) + this.perrin(n - 3);
}
// Print n perrin number
fun printPerrin(n : Int) : Unit
{
var i : Int = 0;
while (i < n)
{
print("  " + this.perrin(i).toString());
i += 1;
}
}
}
fun main(args : Array<String>) : Unit
{
val task : Numbers = Numbers();
// Test
}``````
Output
``  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209``

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment

Alex     64 Day ago
``````6107+8090=14197.
???? ?????????????????? ?? ????????????? OEIS ????? ????? 10717.
https://oeis.org/A001608``````
Categories
Relative Post