Posted on by Kalkicode
Code Number

Nth Pentanacci Number

The problem at hand is to calculate the Nth Pentanacci number, where the Pentanacci sequence is a sequence of numbers where each term is the sum of the five preceding terms. The sequence starts with five initial terms, and each subsequent term is the sum of the previous five terms.

Here are the first few Pentanacci numbers: 0, 0, 0, 0, 1, 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, 23099186, 45411804, 89277256, 175514464, 345052351, 678355061, 1333610936,...

Problem Statement and Description

Given an integer N, the goal is to calculate the Nth Pentanacci number in the Pentanacci sequence. The Pentanacci sequence starts with five initial values (0, 0, 0, 0, 1), and each subsequent value is the sum of the five preceding values. For example, the 6th term is the sum of the previous five terms: 0 + 0 + 0 + 0 + 1 = 1. Similarly, the 7th term is the sum of the last five terms in the sequence.

Idea to Solve the Problem

To calculate the Nth Pentanacci number, we need to iteratively compute the sequence up to the Nth term by summing the previous five terms. We can use an auxiliary array to store the last five terms and update it as we calculate each new term.

in simple terms this series is calculated by following formula:

 a(n) = a(n-1) + a(n-2) + a(n-3) + a(n-4) + a(n-5)

Pseudocode

nthPentanacciNo(n):
    if n <= 0:
        return
    
    result = 0
    
    if n == 5:
        result = 1
    else if n > 5:
        Create an auxiliary array of size 5
        
        Set the initial five Pentanacci values in the auxiliary array
        
        Execute a loop from 5 to n (exclusive)
            Calculate the sum of the previous five terms
            Update the auxiliary array by shifting and updating values
        
    Print the result

main():
    # Test

Algorithm Explanation

  1. Create a function nthPentanacciNo(n) to calculate the Nth Pentanacci number.
  2. If n is less than or equal to 0, return.
  3. Set the variable result to 0.
  4. If n is equal to 5, set result to 1.
  5. If n is greater than 5, create an auxiliary array of size 5 and set it to the initial five Pentanacci values (0, 0, 0, 0, 1).
  6. Execute a loop from 5 to n (exclusive). Inside the loop:
    • Calculate the sum of the previous five terms and store it in result.
    • Update the auxiliary array by shifting the values and updating the last element.
  7. Print the calculated result.

Code Solution

// 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

Time Complexity Analysis

For a given input n, the algorithm iterates in a loop from 5 to n to calculate the Nth Pentanacci number. Within the loop, constant-time operations are performed to calculate the sum of the previous five terms and update the auxiliary array. Therefore, the time complexity of the algorithm is linear, O(n), where n is the input value.

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