
- 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
How to make Ulam number sequence in JavaScript?
A mathematician Ulam proposed generating a sequence of numbers from any positive integer n (n>0) as follows −
If n is 1, it will stop. if n is even, the next number is n/2. if n is odd, the next number is 3 * n + 1. continue with the process until reaching 1.
Here are some examples for the first few integers −
2->1 3->10->5->16->8->4->2->1 4->2->1 6->3->10->5->16->8->4->2->1 7->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1
We are required to write a JavaScript function that takes in a number and returns the Ulam sequence starting with that number.
Example
The code for this will be −
const num = 7; const generateUlam = num => { const res = [num]; if(num && num === Math.abs(num) && isFinite(num)){ while (num !== 1) { if(num % 2){ num = 3 * num + 1 }else{ num /= 2; }; res.push(num); }; }else{ return false; }; return res; }; console.log(generateUlam(num)); console.log(generateUlam(3));
Output
And the output in the console will be −
[ 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ] [ 3, 10, 5, 16, 8, 4, 2, 1 ]
- Related Articles
- How to get sequence number in loops with JavaScript?
- Counting steps to make number palindrome in JavaScript
- Finding one missing number in a scrambled sequence using JavaScript
- Finding the missing number in an arithmetic progression sequence in JavaScript
- How to get almost increasing sequence of integers in JavaScript ?
- Finding the nth element of the lucas number sequence in JavaScript
- How to find the number of runs in a sequence in R?
- Removing least number of elements to convert array into increasing sequence using JavaScript
- The Fibonacci sequence in Javascript
- Fibonacci like sequence in JavaScript
- Program to check we can make arithmetic progression from sequence in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Validating push pop sequence in JavaScript
- How to make a setInterval() stop after some time or after a number of actions in JavaScript?
- Strictly increasing sequence JavaScript

Advertisements