
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Check whether a series of operations yields a given number with JavaScript Recursion
By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. We are required to write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number. And returns a boolean based on the fact whether or not there exists any such sequence
For example,
The number 13 could be reached by first multiplying by 3 and then adding 5 twice, so the function should return true for 13. Whereas the number 15 cannot be reached at all, so the function should return false for 15.
Approach
We will use a recursive approach, where we repeatedly try all possibilities that lead to the desired solution. The code for this approach will be −
Example
const sequenceExists = (num, curr = 1) => { if(curr > num){ return false; }; if(curr === num){ return true; }; return sequenceExists(num, curr+5) || sequenceExists(num, curr*3); }; console.log(sequenceExists(18)); console.log(sequenceExists(15)); console.log(sequenceExists(32)); console.log(sequenceExists(167)); console.log(sequenceExists(17)); console.log(sequenceExists(1119));
Output
The output in the console will be −
true false true true false true
- Related Articles
- Finding a number, when multiplied with input number yields input number in JavaScript
- Check whether a number is a Fibonacci number or not JavaScript
- Check whether a given number is Polydivisible or Not
- Check whether sum of digit of a number is Palindrome - JavaScript
- How to check whether a value is a number in JavaScript?
- JavaScript Program to Check whether all the rotations of a given number are greater than or equal to the given number or not
- Write a Golang program to check whether a given number is prime number or not
- How to check whether a process with a given PID is running
- Check whether a string ends with some other string - JavaScript
- How to check whether a Button is clicked with JavaScript?
- How to check whether a checkbox is checked with JavaScript?
- Write a Golang program to check whether a given number is a palindrome or not
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- How to check whether a number is finite or not in JavaScript?
- How to check whether a radio button is selected with JavaScript?
