JavaScript Program to Print Nth Non Fibonacci Number Last Updated : 08 May, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript program to print the nth non-Fibonacci number. Non-Fibonacci numbers are integers that are not part of the Fibonacci sequence. Below are the approaches to print the Nth non-Fibonacci number: Table of Content Using a LoopUsing RecursionUsing a LoopWe are using a while loop to find the nth non-Fibonacci number. we iterate through Fibonacci numbers and we skip them in the count until we reach the nth non-Fibonacci number. We do this by maintaining three variables (prev1, prev2, and cur) to track the current Fibonacci numbers. We decrement N by the difference between the current Fibonacci number and the previous Fibonacci number minus 1 to skip over Fibonacci numbers. After the loop, we adjust N back to get the correct non-Fibonacci number. Example: To demonstrate printing the nth Non Fibonacci number using a loop. JavaScript function findNthNonFibonacci(N) { let prev1 = 2, prev2 = 1, cur = 3; while (N > 0) { let next = prev2 + prev1; prev2 = prev1; prev1 = next; cur = prev2 + prev1; N = N - (cur - prev1 - 1); } N = N + (cur - prev1 - 1); return prev1 + N; } const n = 9; let nthNonFib = findNthNonFibonacci(n); console.log(nthNonFib); Output15 Using RecursionThis recursive method finds the nth non-Fibonacci number by continuously calculating Fibonacci numbers. It keeps track of the count of non-Fibonacci numbers between consecutive Fibonacci numbers to determine the position of the nth non-Fibonacci number accurately. The function adjusts its search range based on this count and it stops when it finds the nth non-Fibonacci number. Example: To demonstrate printing the nth Non Fibonacci number using recursion. JavaScript function findNthNonFibonacci(N, prev1 = 2, prev2 = 1, cur = 3) { if (N === 0) { return prev1 - 1; // Found the nth non-Fibonacci number } let next = prev2 + prev1; prev2 = prev1; prev1 = next; cur = prev2 + prev1; let nonFibCount = cur - prev1 - 1; if (N <= nonFibCount) { return prev1 + N; } else { return findNthNonFibonacci(N - nonFibCount, prev1, prev2, cur); } } const n = 5; const nthNonFib = findNthNonFibonacci(n); console.log(nthNonFib); Output10 Comment More infoAdvertise with us Next Article JavaScript Program to Print Nth Non Fibonacci Number G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies JavaScript Programs Similar Reads JavaScript Program to print Fibonacci Series The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. The recurrence relation defines the sequence Fn of Fibonacci numbers:Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1Examples:Input : 5 4 min read JavaScript Program to Sum of Fibonacci Numbers at Even Indexes up to N Terms Fibonacci numbers are a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In this article, we will explore how to calculate the sum of Fibonacci numbers at even indexes up to N terms using JavaScript. Sum of Fibonacci Numbers at Even Indexes up to N T 2 min read R Program to Print the Fibonacci Sequence The Fibonacci sequence is a series of numbers in which each number (known as a Fibonacci number) is the sum of the two preceding ones. The sequence starts with 0 and 1, and then each subsequent number is the sum of the two previous numbers. The Fibonacci sequence has many applications in various fie 2 min read How to calculate the Fibonacci series in JavaScript ? Fibonacci series is a number series that contains integers in the following pattern. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ..In terms of mathematics, the general formula for calculating the Fibonacci series is fn = fn-1 + fn-2 , where n ⥠2Here, f0 = 0 and f1 = 1. We need to calculate n Fibonacci num 4 min read Fibonacci Sequence: Lesson for Kids Fibonacci sequence is a type series where each number is the sum of the two numbers before it. It starts from 0 and 1 usually. The Fibonacci sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on. The numbers in the Fibonacci sequence are also called Fibonacci numbers.In this a 7 min read Print the pattern 2 2 1 1 $2 1 Given the number N, the task is to print a pattern such that in each line all the digits from N to 1 are present in decreasing order and the frequency of the elements in ith line is N-i (the lines are 0 based i.e., i varies in the range [0, N-1]). Note: Instead of printing a new line print a "$" wit 3 min read JavaScript Program to Find n-th Fibonacci Number The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Given an integer n, the task is to find the n-th Fibonacci number. These are the following met 2 min read PHP Program for Nth Fibonacci Number The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. In this article, we will write a PHP program to find the Nth Fibonacci number. Table of Content Iterative MethodRecursive MethodIterative MethodFirst, we define a func 2 min read JavaScript Program to Display Fibonacci Sequence Using Recursion In this article, we will explore how to display the Fibonacci sequence using recursion in JavaScript. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. Recursion is a powerful technique in programming that involves a function calling itself. A recu 3 min read Fibonacci Series In Python Using For Loop' The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. This series is not only intriguing due to its inherent mathematical properties but also widely 3 min read Like