Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Empty file removed src/problem1/.keep
Empty file.
107 changes: 107 additions & 0 deletions src/problem1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Problem 1: Sum to N

This module provides three different implementations to calculate the sum of all positive integers from 1 to n, with **BigInt support** for handling large numbers safely.

## Functions

### `sum_to_n_a(n)`

**Mathematical formula approach** - Uses the arithmetic series formula.

- Time Complexity: O(1) - Constant time
- Space Complexity: O(1)
- Most efficient implementation
- Returns BigInt for safe handling of large numbers

```javascript
sum_to_n_a(5); // Returns: 15n
sum_to_n_a(100); // Returns: 5050n
sum_to_n_a(10000000000n); // Returns: 50000000005000000000n
```

### `sum_to_n_b(n)`

**Iterative approach** - Uses a loop to sum values.

- Time Complexity: O(n) - Linear time
- Space Complexity: O(1)
- Returns BigInt for safe handling of large numbers

```javascript
sum_to_n_b(5); // Returns: 15n
sum_to_n_b(100); // Returns: 5050n
sum_to_n_b(1000n); // Returns: 500500n
```

### `sum_to_n_c(n)`

**Recursive approach** - Uses recursion to sum values.

- Time Complexity: O(n) - Linear time
- Space Complexity: O(n) - Due to call stack
- Returns BigInt for safe handling of large numbers

```javascript
sum_to_n_c(5); // Returns: 15n
sum_to_n_c(100); // Returns: 5050n
sum_to_n_c(50n); // Returns: 1275n
```

## Input Validation

All functions validate that the input parameter is:

- A **number** (finite, integer, positive) OR
- A **BigInt** (positive, >= 1n)
- For numbers exceeding `Number.MAX_SAFE_INTEGER`, a warning is logged

Invalid inputs will:

- Log an error message to the console
- Return `0n` (BigInt zero)

## Examples

```javascript
// Valid inputs - regular numbers
sum_to_n_a(10); // Returns: 55n
sum_to_n_b(3); // Returns: 6n
sum_to_n_c(1); // Returns: 1n

// Valid inputs - BigInt
sum_to_n_a(100n); // Returns: 5050n
sum_to_n_b(1000n); // Returns: 500500n

// Large numbers - automatic BigInt conversion with warning
sum_to_n_a(9007199254740992); // Logs warning, returns BigInt result

// Invalid inputs
sum_to_n_a(-5); // Logs error, returns: 0n
sum_to_n_b(3.14); // Logs error, returns: 0n
sum_to_n_c("10"); // Logs error, returns: 0n
sum_to_n_a(NaN); // Logs error, returns: 0n
sum_to_n_a(Infinity); // Logs error, returns: 0n
```

## Performance Comparison

For calculating sum from 1 to n:

| Implementation | Time Complexity | Space Complexity | Best Use Case |
| -------------- | --------------- | ---------------- | ------------------------------- |
| sum_to_n_a | O(1) | O(1) | Production use - fastest |
| sum_to_n_b | O(n) | O(1) | Educational - iterative pattern |
| sum_to_n_c | O(n) | O(n) | Educational - recursion pattern |

## Key Features

- **BigInt Support**: All functions automatically handle BigInt inputs and return BigInt values, preventing integer overflow issues
- **Safety Warnings**: Numbers exceeding `Number.MAX_SAFE_INTEGER` trigger warnings
- **Input Validation**: Comprehensive validation using `isValidSumInput()` function that checks for:
- Valid number types (finite, integer, positive)
- Valid BigInt types (positive, >= 1n)
- Rejects strings, NaN, Infinity, floats, and negative values

## Recommendation

For production use, **`sum_to_n_a`** is recommended as it provides constant time complexity regardless of the input size.
73 changes: 73 additions & 0 deletions src/problem1/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function isValidSumInput(n) {
if (typeof n === "bigint") {
return n >= 1n;
}

if (typeof n === "number") {
if (!Number.isFinite(n) || !Number.isInteger(n) || n <= 0) {
return false;
}
if (n > Number.MAX_SAFE_INTEGER) {
console.warn(
`Input ${n} exceeds MAX_SAFE_INTEGER. Using BigInt for safety.`
);
}
return true;
}

return false;
}

var sum_to_n_a = function (n) {
if (!isValidSumInput(n)) {
console.error(
"Invalid input for sum_to_n_a: Parameter must be a positive whole number."
);
return 0n;
}

const largeN = BigInt(n);

// Constant time complexity O(1)
return (largeN * (largeN + 1n)) / 2n;
};

var sum_to_n_b = function (n) {
if (!isValidSumInput(n)) {
console.error(
"Invalid input for sum_to_n_b: Parameter must be a positive whole number."
);
return 0n;
}

// Linear time complexity O(n)
const largeN = BigInt(n);
let sum = 0n;

for (let i = 1n; i <= largeN; i++) {
sum += i;
}
return sum;
};

var sum_to_n_c = function (n) {
// 1. Validation Check
if (!isValidSumInput(n)) {
console.error(
"Invalid input for sum_to_n_c: Parameter must be a positive whole number."
);
return 0n;
}

const largeN = BigInt(n);

// Base case
if (largeN === 1n) return 1n;

// Linear time complexity O(n), but with O(n) space for the call stack
return largeN + sum_to_n_c(largeN - 1n);
};

// console.log(sum_to_n_a(9007199254740991n));
// console.log(sum_to_n_b(9007199254740991n));
// console.log(sum_to_n_c(9007199254740991n));
Binary file added src/problem2/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions src/problem2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dependencies
node_modules/

# Database
backend/cryptoswap.db
Loading