Nth Pentanacci Number

Here given code implementation process.

// C program for
// Nth Pentanacci Number
#include <stdio.h>


void nthPentanacciNo(int n)
{
    if(n <= 0)
    {
        return;
    }

    int result = 0;

    if(n == 5)
    {
        result = 1;
    }
    else if(n > 5)
    {
        
        int auxiliary[5];

        // Set Initial five pentanacci number
        auxiliary[0] = 0;
        auxiliary[1] = 0;
        auxiliary[2] = 0;
        auxiliary[3] = 0;
        auxiliary[4] = 1;

        // Executing the loop from 5..n
        for (int i = 5; i < n; ++i)
        {   
            // Sum the current 5 elements
            result = auxiliary[0] +
                    auxiliary[1] +
                    auxiliary[2] +
                    auxiliary[3] +
                    auxiliary[4];
            // Change series element
            auxiliary[0] = auxiliary[1];
            auxiliary[1] = auxiliary[2];
            auxiliary[2] = auxiliary[3];
            auxiliary[3] = auxiliary[4];
            auxiliary[4] = result;
        } 
    }
    // Display calculated result
    printf("\n %d-th Pentanacci No is : %d",n,result);
}   

int main(int argc, char const *argv[])
{
    // Test Cases
    /*
        Pentanacci Series
        -----------------
        0  0  0  0  1  2  4  8  16  31  61  120  236  
        464  912 1793  3525  6930  13624  26784  52656  
        103519  203513  400096  786568  1546352  
        3040048  5976577  11749641 ... 
    */
    nthPentanacciNo(30);
    nthPentanacciNo(8);
    nthPentanacciNo(10);
    nthPentanacciNo(15);
    return 0;
}

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
/*
    Java Program
    Nth Pentanacci Number
*/
public class PentanacciNo
{
	public void nthPentanacciNo(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int result = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			int[] auxiliary = new int[5];
			// Set Initial five pentanacci number
			auxiliary[0] = 0;
			auxiliary[1] = 0;
			auxiliary[2] = 0;
			auxiliary[3] = 0;
			auxiliary[4] = 1;
			// Executing the loop from 5..n
			for (int i = 5; i < n; ++i)
			{
				// Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + auxiliary[2] + auxiliary[3] + auxiliary[4];
				// Change series element
				auxiliary[0] = auxiliary[1];
				auxiliary[1] = auxiliary[2];
				auxiliary[2] = auxiliary[3];
				auxiliary[3] = auxiliary[4];
				auxiliary[4] = result;
			}
		}
		// Display calculated result
		System.out.print("\n " + n + "-th Pentanacci No is : " + result);
	}
	public static void main(String[] args)
	{
		PentanacciNo task = new PentanacciNo();
		/*
		    Pentanacci Series
		    -----------------
		    0  0  0  0  1  2  4  8  16  31  61  120  236  
		    464  912 1793  3525  6930  13624  26784  52656  
		    103519  203513  400096  786568  1546352  
		    3040048  5976577  11749641 ... 
		*/
		// Test Cases
		task.nthPentanacciNo(30);
		task.nthPentanacciNo(8);
		task.nthPentanacciNo(10);
		task.nthPentanacciNo(15);
	}
}

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Nth Pentanacci Number
*/
class PentanacciNo
{
	public: void nthPentanacciNo(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int result = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			int auxiliary[5];
			// Set Initial five pentanacci number
			auxiliary[0] = 0;
			auxiliary[1] = 0;
			auxiliary[2] = 0;
			auxiliary[3] = 0;
			auxiliary[4] = 1;
			// Executing the loop from 5..n
			for (int i = 5; i < n; ++i)
			{
				// Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + 
                  auxiliary[2] + auxiliary[3] + auxiliary[4];
				// Change series element
				auxiliary[0] = auxiliary[1];
				auxiliary[1] = auxiliary[2];
				auxiliary[2] = auxiliary[3];
				auxiliary[3] = auxiliary[4];
				auxiliary[4] = result;
			}
		}
		// Display calculated result
		cout << "\n " << n << "-th Pentanacci No is : " << result;
	}
};
int main()
{
	PentanacciNo *task = new PentanacciNo();
	/*
	    Pentanacci Series
	    -----------------
	    0  0  0  0  1  2  4  8  16  31  61  120  236  
	    464  912 1793  3525  6930  13624  26784  52656  
	    103519  203513  400096  786568  1546352  
	    3040048  5976577  11749641 ... 
	*/
	// Test Cases
	task->nthPentanacciNo(30);
	task->nthPentanacciNo(8);
	task->nthPentanacciNo(10);
	task->nthPentanacciNo(15);
	return 0;
}

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
// Include namespace system
using System;
/*
    Csharp Program
    Nth Pentanacci Number
*/
public class PentanacciNo
{
	public void nthPentanacciNo(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int result = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			int[] auxiliary = new int[5];
			// Set Initial five pentanacci number
			auxiliary[0] = 0;
			auxiliary[1] = 0;
			auxiliary[2] = 0;
			auxiliary[3] = 0;
			auxiliary[4] = 1;
			// Executing the loop from 5..n
			for (int i = 5; i < n; ++i)
			{
				// Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + 
                  auxiliary[2] + auxiliary[3] + auxiliary[4];
				// Change series element
				auxiliary[0] = auxiliary[1];
				auxiliary[1] = auxiliary[2];
				auxiliary[2] = auxiliary[3];
				auxiliary[3] = auxiliary[4];
				auxiliary[4] = result;
			}
		}
		// Display calculated result
		Console.Write("\n " + n + "-th Pentanacci No is : " + result);
	}
	public static void Main(String[] args)
	{
		PentanacciNo task = new PentanacciNo();
		/*
		    Pentanacci Series
		    -----------------
		    0  0  0  0  1  2  4  8  16  31  61  120  236  
		    464  912 1793  3525  6930  13624  26784  52656  
		    103519  203513  400096  786568  1546352  
		    3040048  5976577  11749641 ... 
		*/
		// Test Cases
		task.nthPentanacciNo(30);
		task.nthPentanacciNo(8);
		task.nthPentanacciNo(10);
		task.nthPentanacciNo(15);
	}
}

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
<?php
/*
    Php Program
    Nth Pentanacci Number
*/
class PentanacciNo
{
	public	function nthPentanacciNo($n)
	{
		if ($n <= 0)
		{
			return;
		}
		$result = 0;
		if ($n == 5)
		{
			$result = 1;
		}
		else if ($n > 5)
		{
			$auxiliary = array_fill(0, 5, 0);
			// Set Initial five pentanacci number
			$auxiliary[0] = 0;
			$auxiliary[1] = 0;
			$auxiliary[2] = 0;
			$auxiliary[3] = 0;
			$auxiliary[4] = 1;
			// Executing the loop from 5..n
			for ($i = 5; $i < $n; ++$i)
			{
				// Sum the current 5 elements
				$result = $auxiliary[0] + $auxiliary[1] + 
                  $auxiliary[2] + $auxiliary[3] + $auxiliary[4];
				// Change series element
				$auxiliary[0] = $auxiliary[1];
				$auxiliary[1] = $auxiliary[2];
				$auxiliary[2] = $auxiliary[3];
				$auxiliary[3] = $auxiliary[4];
				$auxiliary[4] = $result;
			}
		}
		// Display calculated result
		echo("\n ".$n.
			"-th Pentanacci No is : ".$result);
	}
}

function main()
{
	$task = new PentanacciNo();
	/*
	    Pentanacci Series
	    -----------------
	    0  0  0  0  1  2  4  8  16  31  61  120  236  
	    464  912 1793  3525  6930  13624  26784  52656  
	    103519  203513  400096  786568  1546352  
	    3040048  5976577  11749641 ... 
	*/
	// Test Cases
	$task->nthPentanacciNo(30);
	$task->nthPentanacciNo(8);
	$task->nthPentanacciNo(10);
	$task->nthPentanacciNo(15);
}
main();

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
/*
    Node JS Program
    Nth Pentanacci Number
*/
class PentanacciNo
{
	nthPentanacciNo(n)
	{
		if (n <= 0)
		{
			return;
		}
		var result = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			var auxiliary = Array(5).fill(0);
			// Set Initial five pentanacci number
			auxiliary[0] = 0;
			auxiliary[1] = 0;
			auxiliary[2] = 0;
			auxiliary[3] = 0;
			auxiliary[4] = 1;
			// Executing the loop from 5..n
			for (var i = 5; i < n; ++i)
			{
				// Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + 
                  auxiliary[2] + auxiliary[3] + auxiliary[4];
				// Change series element
				auxiliary[0] = auxiliary[1];
				auxiliary[1] = auxiliary[2];
				auxiliary[2] = auxiliary[3];
				auxiliary[3] = auxiliary[4];
				auxiliary[4] = result;
			}
		}
		// Display calculated result
		process.stdout.write("\n " + n + "-th Pentanacci No is : " + result);
	}
}

function main()
{
	var task = new PentanacciNo();
	/*
	    Pentanacci Series
	    -----------------
	    0  0  0  0  1  2  4  8  16  31  61  120  236  
	    464  912 1793  3525  6930  13624  26784  52656  
	    103519  203513  400096  786568  1546352  
	    3040048  5976577  11749641 ... 
	*/
	// Test Cases
	task.nthPentanacciNo(30);
	task.nthPentanacciNo(8);
	task.nthPentanacciNo(10);
	task.nthPentanacciNo(15);
}
main();

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
#    Python 3 Program
#    Nth Pentanacci Number
class PentanacciNo :
	def nthPentanacciNo(self, n) :
		if (n <= 0) :
			return
		
		result = 0
		if (n == 5) :
			result = 1
		elif (n > 5) :
			auxiliary = [0] * (5)
			#  Set Initial five pentanacci number
			auxiliary[0] = 0
			auxiliary[1] = 0
			auxiliary[2] = 0
			auxiliary[3] = 0
			auxiliary[4] = 1
			#  Executing the loop from 5..n
			i = 5
			while (i < n) :
				#  Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + auxiliary[2] + auxiliary[3] + auxiliary[4]
				#  Change series element
				auxiliary[0] = auxiliary[1]
				auxiliary[1] = auxiliary[2]
				auxiliary[2] = auxiliary[3]
				auxiliary[3] = auxiliary[4]
				auxiliary[4] = result
				i += 1
			
		
		#  Display calculated result
		print("\n ", n ,"-th Pentanacci No is : ", result, end = "")
	

def main() :
	task = PentanacciNo()
	#    Pentanacci Series
	#    -----------------
	#    0  0  0  0  1  2  4  8  16  31  61  120  236  
	#    464  912 1793  3525  6930  13624  26784  52656  
	#    103519  203513  400096  786568  1546352  
	#    3040048  5976577  11749641 ... 
	#  Test Cases
	task.nthPentanacciNo(30)
	task.nthPentanacciNo(8)
	task.nthPentanacciNo(10)
	task.nthPentanacciNo(15)

if __name__ == "__main__": main()

input

  30 -th Pentanacci No is :  11749641
  8 -th Pentanacci No is :  4
  10 -th Pentanacci No is :  16
  15 -th Pentanacci No is :  464
#    Ruby Program
#    Nth Pentanacci Number
class PentanacciNo 
	def nthPentanacciNo(n) 
		if (n <= 0) 
			return
		end

		result = 0
		if (n == 5) 
			result = 1
		elsif (n > 5) 
			auxiliary = Array.new(5) {0}
			#  Set Initial five pentanacci number
			auxiliary[0] = 0
			auxiliary[1] = 0
			auxiliary[2] = 0
			auxiliary[3] = 0
			auxiliary[4] = 1
			#  Executing the loop from 5..n
			i = 5
			while (i < n) 
				#  Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + auxiliary[2] + 
                  auxiliary[3] + auxiliary[4]
				#  Change series element
				auxiliary[0] = auxiliary[1]
				auxiliary[1] = auxiliary[2]
				auxiliary[2] = auxiliary[3]
				auxiliary[3] = auxiliary[4]
				auxiliary[4] = result
				i += 1
			end

		end

		#  Display calculated result
		print("\n ", n ,"-th Pentanacci No is : ", result)
	end

end

def main() 
	task = PentanacciNo.new()
	#    Pentanacci Series
	#    -----------------
	#    0  0  0  0  1  2  4  8  16  31  61  120  236  
	#    464  912 1793  3525  6930  13624  26784  52656  
	#    103519  203513  400096  786568  1546352  
	#    3040048  5976577  11749641 ... 
	#  Test Cases
	task.nthPentanacciNo(30)
	task.nthPentanacciNo(8)
	task.nthPentanacciNo(10)
	task.nthPentanacciNo(15)
end

main()

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
/*
    Scala Program
    Nth Pentanacci Number
*/
class PentanacciNo()
{
	def nthPentanacciNo(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		var result: Int = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			var auxiliary: Array[Int] = Array.fill[Int](5)(0);
			// Set Initial five pentanacci number
			auxiliary(0) = 0;
			auxiliary(1) = 0;
			auxiliary(2) = 0;
			auxiliary(3) = 0;
			auxiliary(4) = 1;
			// Executing the loop from 5..n
			var i: Int = 5;
			while (i < n)
			{
				// Sum the current 5 elements
				result = auxiliary(0) + auxiliary(1) + 
                  auxiliary(2) + auxiliary(3) + auxiliary(4);
				// Change series element
				auxiliary(0) = auxiliary(1);
				auxiliary(1) = auxiliary(2);
				auxiliary(2) = auxiliary(3);
				auxiliary(3) = auxiliary(4);
				auxiliary(4) = result;
				i += 1;
			}
		}
		// Display calculated result
		print("\n " + n + "-th Pentanacci No is : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PentanacciNo = new PentanacciNo();
		/*
		    Pentanacci Series
		    -----------------
		    0  0  0  0  1  2  4  8  16  31  61  120  236  
		    464  912 1793  3525  6930  13624  26784  52656  
		    103519  203513  400096  786568  1546352  
		    3040048  5976577  11749641 ... 
		*/
		// Test Cases
		task.nthPentanacciNo(30);
		task.nthPentanacciNo(8);
		task.nthPentanacciNo(10);
		task.nthPentanacciNo(15);
	}
}

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464
/*
    Swift 4 Program
    Nth Pentanacci Number
*/
class PentanacciNo
{
	func nthPentanacciNo(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		var result = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			var auxiliary = Array(repeating: 0, count: 5);
			// Set Initial five pentanacci number
			auxiliary[0] = 0;
			auxiliary[1] = 0;
			auxiliary[2] = 0;
			auxiliary[3] = 0;
			auxiliary[4] = 1;
			// Executing the loop from 5..n
			var i = 5;
			while (i < n)
			{
				// Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + 
                  auxiliary[2] + auxiliary[3] + auxiliary[4];
				// Change series element
				auxiliary[0] = auxiliary[1];
				auxiliary[1] = auxiliary[2];
				auxiliary[2] = auxiliary[3];
				auxiliary[3] = auxiliary[4];
				auxiliary[4] = result;
				i += 1;
			}
		}
		// Display calculated result
		print("\n ", n ,"-th Pentanacci No is : ", 
              result, terminator: "");
	}
}
func main()
{
	let task = PentanacciNo();
	/*
	    Pentanacci Series
	    -----------------
	    0  0  0  0  1  2  4  8  16  31  61  120  236  
	    464  912 1793  3525  6930  13624  26784  52656  
	    103519  203513  400096  786568  1546352  
	    3040048  5976577  11749641 ... 
	*/
	// Test Cases
	task.nthPentanacciNo(30);
	task.nthPentanacciNo(8);
	task.nthPentanacciNo(10);
	task.nthPentanacciNo(15);
}
main();

input

  30 -th Pentanacci No is :  11749641
  8 -th Pentanacci No is :  4
  10 -th Pentanacci No is :  16
  15 -th Pentanacci No is :  464
/*
    Kotlin Program
    Nth Pentanacci Number
*/
class PentanacciNo
{
	fun nthPentanacciNo(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		var result: Int = 0;
		if (n == 5)
		{
			result = 1;
		}
		else if (n > 5)
		{
			val auxiliary: Array < Int > = Array(5)
			{
				0
			};
			// Set Initial five pentanacci number
			auxiliary[0] = 0;
			auxiliary[1] = 0;
			auxiliary[2] = 0;
			auxiliary[3] = 0;
			auxiliary[4] = 1;
			// Executing the loop from 5..n
			var i: Int = 5;
			while (i < n)
			{
				// Sum the current 5 elements
				result = auxiliary[0] + auxiliary[1] + auxiliary[2] + 
                  auxiliary[3] + auxiliary[4];
				// Change series element
				auxiliary[0] = auxiliary[1];
				auxiliary[1] = auxiliary[2];
				auxiliary[2] = auxiliary[3];
				auxiliary[3] = auxiliary[4];
				auxiliary[4] = result;
				i += 1;
			}
		}
		// Display calculated result
		print("\n " + n + "-th Pentanacci No is : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PentanacciNo = PentanacciNo();
	/*
	    Pentanacci Series
	    -----------------
	    0  0  0  0  1  2  4  8  16  31  61  120  236  
	    464  912 1793  3525  6930  13624  26784  52656  
	    103519  203513  400096  786568  1546352  
	    3040048  5976577  11749641 ... 
	*/
	// Test Cases
	task.nthPentanacciNo(30);
	task.nthPentanacciNo(8);
	task.nthPentanacciNo(10);
	task.nthPentanacciNo(15);
}

input

 30-th Pentanacci No is : 11749641
 8-th Pentanacci No is : 4
 10-th Pentanacci No is : 16
 15-th Pentanacci No is : 464


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







© 2021, kalkicode.com, All rights reserved