|
| 1 | +/** |
| 2 | + * Iteratively computes the sum from 1 to n using a loop. |
| 3 | + * |
| 4 | + * Time Complexity: O(n) — Iterates through all numbers from 1 to n. |
| 5 | + * Space Complexity: O(1) — Uses constant extra space for sum variable. |
| 6 | + */ |
| 7 | +export const sumToNWithLoop = (n: number): number => { |
| 8 | + /** Check if sum will exceed Number.MAX_SAFE_INTEGER */ |
| 9 | + if (n > 0 && (n * (n + 1)) / 2 > Number.MAX_SAFE_INTEGER) { |
| 10 | + throw new Error('Result exceeds Number.MAX_SAFE_INTEGER'); |
| 11 | + } |
| 12 | + let sum = 0; |
| 13 | + for (let i = 1; i <= n; i++) { |
| 14 | + sum += i; |
| 15 | + } |
| 16 | + return sum; |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Computes the sum from 1 to n using the mathematical formula. |
| 22 | + * |
| 23 | + * Time Complexity: O(1) — Direct calculation, no iteration. |
| 24 | + * Space Complexity: O(1) — No extra space required. |
| 25 | + * |
| 26 | + * This is the most optimal approach for this problem. |
| 27 | + */ |
| 28 | +export const sumToNWithFormula = (n: number): number => { |
| 29 | + /** Check if sum will exceed Number.MAX_SAFE_INTEGER */ |
| 30 | + if (n > 0 && (n * (n + 1)) / 2 > Number.MAX_SAFE_INTEGER) { |
| 31 | + throw new Error('Result exceeds Number.MAX_SAFE_INTEGER'); |
| 32 | + } |
| 33 | + return (n * (n + 1)) / 2; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * Recursively computes the sum from 1 to n. |
| 39 | + * |
| 40 | + * Time Complexity: O(n) — Recursion depth is n. |
| 41 | + * Space Complexity: O(n) — Call stack grows linearly with n. |
| 42 | + * |
| 43 | + * Not recommended for large n due to stack overflow risk. |
| 44 | + */ |
| 45 | +export const sumToNWithRecursion = (n: number): number => { |
| 46 | + /** Check if sum will exceed Number.MAX_SAFE_INTEGER */ |
| 47 | + if (n > 0 && (n * (n + 1)) / 2 > Number.MAX_SAFE_INTEGER) { |
| 48 | + throw new Error('Result exceeds Number.MAX_SAFE_INTEGER'); |
| 49 | + } |
| 50 | + if (n === 0) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + return n + sumToNWithRecursion(n - 1); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Memoized recursive computation of sum from 1 to n. |
| 58 | + * |
| 59 | + * Time Complexity: O(n) — Each value from 1 to n is computed once. |
| 60 | + * Space Complexity: O(n) — Stores results for each n in memoization map and call stack. |
| 61 | + * |
| 62 | + * Memoization is used to avoid redundant calculations in recursion, improving efficiency. |
| 63 | + * This technique is especially useful in problems with overlapping sub-problems. |
| 64 | + */ |
| 65 | +const memo = new Map<number, number>(); |
| 66 | + |
| 67 | +export function sumToNMemoRecursion(n: number): number { |
| 68 | + /** Check if sum will exceed Number.MAX_SAFE_INTEGER */ |
| 69 | + if (n > 0 && (n * (n + 1)) / 2 > Number.MAX_SAFE_INTEGER) { |
| 70 | + throw new Error('Result exceeds Number.MAX_SAFE_INTEGER'); |
| 71 | + } |
| 72 | + if (n <= 0) return 0; |
| 73 | + |
| 74 | + if (memo.has(n)) { |
| 75 | + return memo.get(n)!; |
| 76 | + } |
| 77 | + |
| 78 | + const result = n + sumToNMemoRecursion(n - 1); |
| 79 | + memo.set(n, result); |
| 80 | + |
| 81 | + return result; |
| 82 | +} |
0 commit comments