Perrin number program

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)
	{
		Number task = new Number();
		// Test
		task.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 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){
    Number task ;
    // Test
    task.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 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)
    {
        var task = new Numbers();
        // Test
        task.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 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))
                Threading.Interlocked.Increment(i)
            End While
        End With
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Numbers = New Numbers()
        ' Test
        task.printPerrin(20)
    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);
        }
    }
}
$task = new Numbers();
// Test
$task->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 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
var task = new Numbers();
// Test
task.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 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));
        }
    }
}
var task = new Numbers();
// Test
task.printPerrin(20);
/*
 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__":
    task = Numbers()
    # Test
    task.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 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
    task = Numbers.new()
    # Test
    task.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 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=
    {
        var task = new Numbers()
        // Test
        task.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 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;
		}
	}
}
let task: Numbers = Numbers();
// Test
task.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 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
  task.printPerrin(20);
}
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







© 2022, kalkicode.com, All rights reserved