The Wayback Machine - https://web.archive.org/web/20240606000720/https://www.geeksforgeeks.org/padovan-sequence/
Open In App

Padovan Sequence

Last Updated : 24 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Padovan Sequence similar to Fibonacci sequence with similar recursive structure. The recursive formula is, 
 

  P(n) = P(n-2) + P(n-3)
  P(0) = P(1) = P(2) = 1 

Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…… 
Spiral of squares with side lengths which follow the Fibonacci sequence. 
 

fibonacci-tiles1

Padovan Sequence: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37,….. 
Spiral of equilateral triangles with side lengths which follow the Padovan sequence. 
 

Padovan_triangles_(1)

Examples: 
 

For Padovan Sequence:
P0 = P1 = P2 = 1 ,
P(7) = P(5) + P(4)
     = P(3) + P(2) + P(2) + P(1)
     = P(2) + P(1) + 1 + 1 + 1
     = 1 + 1 + 1 + 1 + 1 
     = 5

 

Recommended Practice

 

C++




// C++ program to find n'th term in Padovan Sequence
// using Dynamic Programming
#include<iostream>
using namespace std;
 
/* Function to calculate padovan number P(n) */
int pad(int n)
{
    /* 0th ,1st and 2nd number of the series are 1*/
    int pPrevPrev = 1, pPrev = 1, pCurr = 1, pNext = 1;
 
    for (int i=3; i<=n; i++)
    {
        pNext = pPrevPrev + pPrev;
        pPrevPrev = pPrev;
        pPrev = pCurr;
        pCurr = pNext;
    }
 
    return pNext;
}
 
/* Driver Program */
int main()
{
    int n = 12;
    cout << pad(n);
    return 0;
}


Java




// Java program to find n'th term
// in Padovan Sequence using
// Dynamic Programming
import java.io.*;
 
class GFG {
     
    /* Function to calculate
    padovan number P(n) */
    static int pad(int n)
    {
       int []padv=new int[n]; //create array to store padovan values
       padv[0]=padv[1]=padv[2]=1;
        for (int i = 3; i <= n; i++) {
         padv[i]=padv[i-2]+padv[i-3];  
        }
        return padv[n-1];
 
         
    }
 
    /* Driver Program */
    public static void main(String args[])
    {
        int n = 12;
        System.out.println(pad(n));
    }
}
 
/*This code is contributed by Kanjam Bhat Lidhoo.*/


Python3




# Python program to find n'th term in Padovan
# Sequence using Dynamic Programming
 
# Function to calculate padovan number P(n)
def pad(n):
 
    # 0th ,1st and 2nd number of the series are 1
    pPrevPrev, pPrev, pCurr, pNext = 1, 1, 1, 1
 
    # Find n'th Padovan number using recursive
    # formula.
    for i in range(3, n+1):
        pNext = pPrevPrev + pPrev
        pPrevPrev = pPrev
        pPrev = pCurr
        pCurr = pNext
 
    return pNext
 
# Driver Code
print (pad(12))


C#




// C# program to find n'th term
// in Padovan Sequence using
// Dynamic Programming
using System;
 
class GFG {
 
    /* Function to calculate
    padovan number P(n) */
    static int pad(int n)
    {
         
        /* 0th, 1st and 2nd number
        of the series are 1*/
        int pPrevPrev = 1, pPrev = 1,
            pCurr = 1, pNext = 1;
 
        for (int i = 3; i <= n; i++) {
            pNext = pPrevPrev + pPrev;
            pPrevPrev = pPrev;
            pPrev = pCurr;
            pCurr = pNext;
        }
 
        return pNext;
    }
 
    /* Driver Program */
    public static void Main()
    {
        int n = 12;
         
        Console.WriteLine(pad(n));
    }
}
 
/*This code is contributed by vt_m.*/


PHP




<?php
// PHP program to find n'th
// term in Padovan Sequence
// using Dynamic Programming
 
// Function to calculate
// padovan number P(n)
function pad($n)
{
     
    // 0th ,1st and 2nd number
    // of the series are 1
    $pPrevPrev = 1; $pPrev = 1;
    $pCurr = 1; $pNext = 1;
 
    for ($i = 3; $i <= $n; $i++)
    {
        $pNext = $pPrevPrev + $pPrev;
        $pPrevPrev = $pPrev;
        $pPrev = $pCurr;
        $pCurr = $pNext;
    }
 
    return $pNext;
}
 
// Driver Code
$n = 12;
echo(pad($n));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
// Javascript program to find n'th
// term in Padovan Sequence
// using Dynamic Programming
 
// Function to calculate
// padovan number P(n)
function pad(n) {
 
    // 0th ,1st and 2nd number
    // of the series are 1
    let pPrevPrev = 1;
    let pPrev = 1;
    let pCurr = 1;
    let pNext = 1;
 
    for (let i = 3; i <= n; i++) {
        pNext = pPrevPrev + pPrev;
        pPrevPrev = pPrev;
        pPrev = pCurr;
        pCurr = pNext;
    }
 
    return pNext;
}
 
// Driver Code
let n = 12;
document.write(pad(n));
 
// This code is contributed by gfgking.
</script>


Output: 
 

21

 



Previous Article
Next Article

Similar Reads

Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
Given a sequence of integers of even length 'n', the task is to find the minimum number of operations required to convert the sequence to follow the rule a[i]=a[i+2] where 'i' is the index. The operation here is to replace any element of the sequence with any element. Examples: Input : n=4 ; Array : 3, 1, 3, 2 Output : 1 If we change the last eleme
11 min read
Convert an unbalanced bracket sequence to a balanced sequence
Given an unbalanced bracket sequence of '(' and ')', convert it into a balanced sequence by adding the minimum number of '(' at the beginning of the string and ')' at the end of the string. Examples: Input: str = ")))()" Output: "((()))()" Input: str = "())())(())())" Output: "(((())())(())())" Method 1: Approach: Initialize an empty stack.Iterate
13 min read
Find original sequence from Array containing the sequence merged many times in order
Given a number N and an array arr[] that consist of merging N length sequence of distinct integers any number of times maintaining the relative order of elements in the initial sequence. The task is to find the initial sequence of length N maintaining the right order. Examples: Input: N = 4, arr[] = {1, 13, 1, 24, 13, 24, 2, 2}Output: 1 13 24 2 Exp
10 min read
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
Given a string S of valid parentheses sequence of length N and an even integer K, the task is to find the valid parentheses sequence of length K which is also a subsequence of the given string. Note: There can be more than one valid sequence, print any of them. Examples: Input: S = "()()()", K = 4Output: ()()Explanation:The string "()()" is a subse
7 min read
Generate a sequence X from given sequence Y such that Yi = gcd(X1, X2 , ... , Xi)
Given a sequence Y of size N where: Yi = gcd(X1, X2, X3, . . ., Xi ) of some sequence X. The task is to find such a sequence X if any such X is possible. Note: If X exists there can be multiple value possible for X. Generating any one is sufficient. Examples: Input: N = 2, Y = [4, 2]Output: [4, 2]Explanation: Y0 = gcd(X0) = X0 = 4. And gcd(4, 2) =
7 min read
k-th missing element in increasing sequence which is not present in a given sequence
Given two sequences, one is increasing sequence a[] and another a normal sequence b[], find the K-th missing element in the increasing sequence which is not present in the given sequence. If no k-th missing element is there output -1 Examples: Input: a[] = {0, 2, 4, 6, 8, 10, 12, 14, 15}; b[] = {4, 10, 6, 8, 12}; k = 3 Output: 14 Explanation : The
6 min read
Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1). Example: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1} Output: Index 9 Assuming array index starts from 0, replacing 0 with 1 at index 9 causes the maximum continu
9 min read
Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1). Examples: Input : arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1} Output : Index 9 Assuming array index starts from 0, replacing 0 with 1 at index 9 causes the maximum cont
24 min read
Juggler Sequence | Set 2 (Using Recursion)
Juggler Sequence is a series of integer number in which the first term starts with a positive integer number a and the remaining terms are generated from the immediate previous term using the below recurrence relation : [Tex]a_{k+1}=\begin{Bmatrix} \lfloor a_{k}^{1/2} \rfloor &amp; for \quad even \quad a_k\\ \lfloor a_{k}^{3/2} \rfloor &amp; for \q
4 min read
Thue-Morse sequence
Thue–Morse sequence, or Prouhet–Thue–Morse sequence, is an infinite binary sequence of 0s and 1s. The sequence is obtained by starting with 0 and successively appending the Boolean complement of the sequence obtained so far. First few steps : Start with 0 Append complement of 0, we get 01 Append complement of 01, we get 0110 Append complement of 01
6 min read