

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
<h2>Padovan Sequence</h2><p>The Padovan sequence is the sequence of integers P(n) defined by the initial values −</p><pre class="result notransalte">P(0) = P(1) = P(2) = 1</pre><p>and the recurrence relation,</p><pre class="result notransalte">P(n) = P(n-2) + P(n-3)</pre><p>The first few values of P(n) are</p><pre class="result notransalte">1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, …</pre><h2>Problem</h2><p>We are required to write a JavaScript function that takes in a number n and return the nth term of the Padovan sequence.</p><h2>Example</h2><p>Following is the code −</p><p><a class="demo" href="http://tpcg.io/3h4qMytv" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notransalte">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));</pre><h2>Output</h2><pre class="result notransalte">5842</pre>
- Related Questions & Answers
- 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 nth digit of natural numbers JavaScript
- Finding the longest non-negative sum sequence using JavaScript
- Nth element of the Fibonacci series JavaScript
- Finding the nth prime number in JavaScript
- Finding sum of sequence upto a specified accuracy using JavaScript
- Finding Fibonacci sequence in an array using JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- Finding the nth day from today - JavaScript (JS Date)
- Finding the nth missing number from an array JavaScript
Advertisements