- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding nth element of the Padovan sequence using JavaScript
Padovan Sequence
The Padovan sequence is the sequence of integers P(n) defined by the initial values −
P(0) = P(1) = P(2) = 1
and the recurrence relation,
P(n) = P(n-2) + P(n-3)
The first few values of P(n) are
1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, …
Problem
We are required to write a JavaScript function that takes in a number n and return the nth term of the Padovan sequence.
Example
Following is the code −
const num = 32; const padovan = (num = 1) => { let secondPrev = 1, pPrev = 1, pCurr = 1, pNext = 1; for (let i = 3; i <= num; i++){ pNext = secondPrev + pPrev; secondPrev = pPrev; pPrev = pCurr; pCurr = pNext; }; return pNext; }; console.log(padovan(num));
Output
5842
- Related Articles
- Finding nth element of an increasing sequence using JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding the longest non-negative sum sequence using JavaScript
- Finding Fibonacci sequence in an array using JavaScript
- Finding the nth digit of natural numbers JavaScript
- Finding sum of sequence upto a specified accuracy using JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the nth prime number in JavaScript
- Finding the longest "uncommon" sequence in JavaScript
- Nth element of the Fibonacci series JavaScript
- Finding one missing number in a scrambled sequence using JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript

Advertisements