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.

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.

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
C++
#include<iostream>
using namespace std;
int pad( int n)
{
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;
}
int main()
{
int n = 12;
cout << pad(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int pad( int n)
{
int []padv= new int [n];
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 ];
}
public static void main(String args[])
{
int n = 12 ;
System.out.println(pad(n));
}
}
|
Python3
def pad(n):
pPrevPrev, pPrev, pCurr, pNext = 1 , 1 , 1 , 1
for i in range ( 3 , n + 1 ):
pNext = pPrevPrev + pPrev
pPrevPrev = pPrev
pPrev = pCurr
pCurr = pNext
return pNext
print (pad( 12 ))
|
C#
using System;
class GFG {
static int pad( int n)
{
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;
}
public static void Main()
{
int n = 12;
Console.WriteLine(pad(n));
}
}
|
PHP
<?php
function pad( $n )
{
$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 ;
}
$n = 12;
echo (pad( $n ));
?>
|
Javascript
<script>
function pad(n) {
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;
}
let n = 12;
document.write(pad(n));
</script>
|
Output:
21
This article is contributed by Shivam Pradhan(anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.