UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

20 lines (19 loc) 611 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.collatzSequence = void 0; /** * Generates the Collatz sequence for a given starting number. * For example, collatzSequence(6) returns [6, 3, 10, 5, 16, 8, 4, 2, 1]. * @author @dailker * @param {number} n - The starting number. * @returns {number[]} The Collatz sequence as an array. */ function collatzSequence(n) { const result = [n]; while (n !== 1) { n = n % 2 === 0 ? n / 2 : 3 * n + 1; result.push(n); } return result; } exports.collatzSequence = collatzSequence;