Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
This sequence is also known as the Collatz conjecture or 3n+1 problem. It's conjectured that all positive integers eventually reach 1 following these rules.
Example Sequences
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
Implementation
We'll create a JavaScript function that takes a number and returns the complete Ulam sequence starting with that number:
const generateUlam = num => {
const res = [num];
// Validate input: must be positive integer
if (num && num === Math.abs(num) && isFinite(num)) {
while (num !== 1) {
if (num % 2) {
// Odd: multiply by 3 and add 1
num = 3 * num + 1;
} else {
// Even: divide by 2
num /= 2;
}
res.push(num);
}
} else {
return false;
}
return res;
};
// Test with different numbers
console.log("Ulam sequence for 7:");
console.log(generateUlam(7));
console.log("\nUlam sequence for 3:");
console.log(generateUlam(3));
console.log("\nUlam sequence for 12:");
console.log(generateUlam(12));
Ulam sequence for 7: [ 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ] Ulam sequence for 3: [ 3, 10, 5, 16, 8, 4, 2, 1 ] Ulam sequence for 12: [ 12, 6, 3, 10, 5, 16, 8, 4, 2, 1 ]
How It Works
The function follows these steps:
- Input validation: Checks if the input is a positive finite number
- Sequence generation: Applies the Ulam rules until reaching 1
- Storage: Each step is stored in an array and returned
Alternative Implementation with Step Count
Here's a version that also returns the number of steps taken:
const generateUlamWithSteps = num => {
const sequence = [num];
let steps = 0;
let current = num;
while (current !== 1) {
if (current % 2 === 0) {
current = current / 2;
} else {
current = 3 * current + 1;
}
sequence.push(current);
steps++;
}
return {
sequence: sequence,
steps: steps,
maxValue: Math.max(...sequence)
};
};
const result = generateUlamWithSteps(7);
console.log("Sequence:", result.sequence);
console.log("Steps to reach 1:", result.steps);
console.log("Maximum value reached:", result.maxValue);
Sequence: [ 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ] Steps to reach 1: 16 Maximum value reached: 52
Conclusion
The Ulam sequence (Collatz conjecture) demonstrates an interesting mathematical pattern where all positive integers eventually converge to 1. This JavaScript implementation efficiently generates and tracks the complete sequence for any given starting number.
