A TypeScript library for calculating square roots of arbitrary-precision numbers using the `bn.js` library.
npm install sqrt-bn-enhancedbn.js library.
bn.js
bash
npm install sqrt-enhanced
`
Quick Start
`typescript
import { sqrt_bn, sqrt_bn_enhanced, BN } from 'sqrt-enhanced';
// Basic usage
const result = await sqrt_bn(16);
console.log(result.toString()); // "4"
// With big numbers
const bigNumber = new BN('123456789012345678901234567890');
const sqrt = await sqrt_bn(bigNumber);
console.log(sqrt.toString());
// Enhanced version with metadata
const enhanced = await sqrt_bn_enhanced(100, { verbose: true });
console.log(Result: ${enhanced.result});
console.log(Iterations: ${enhanced.iterations});
console.log(Execution time: ${enhanced.executionTime}ms);
`
API Reference
$3
Calculates the integer square root of a number using the Babylonian method.
Parameters:
- input: The number to find the square root of (string, number, or BN)
Returns: Promise that resolves to the square root as a BN instance
Example:
`typescript
const result = await sqrt_bn(25); // Returns BN(5)
const result2 = await sqrt_bn('1000000'); // Returns BN(1000)
const result3 = await sqrt_bn(new BN('123456789')); // Returns BN(11111)
`
$3
Enhanced version with configurable options and metadata.
Parameters:
- input: The number to find the square root of
- options: Configuration options (optional)
Options:
- verbose: Whether to log results to console (default: false)
- maxIterations: Maximum number of iterations (default: 1000)
- tolerance: Precision tolerance for convergence (default: BN(1))
Returns: Promise that resolves to an object with result and metadata
Example:
`typescript
const result = await sqrt_bn_enhanced(100, {
verbose: true,
maxIterations: 500,
tolerance: new BN(1)
});
console.log(result.result); // BN(10)
console.log(result.iterations); // Number of iterations performed
console.log(result.converged); // Whether algorithm converged
console.log(result.executionTime); // Execution time in milliseconds
`
Utility Functions
$3
Validates if a value can be converted to a valid BN.
$3
Converts a value to BN with validation.
$3
Checks if a number is a perfect square.
$3
Formats a BN number with commas for better readability.
$3
Calculates the number of digits in a BN.
$3
Rounds a BN to a specified number of decimal places.
Types
`typescript
type SqrtInput = string | number | BN;
type SqrtResult = BN;
type EnvVars = Record;
interface SqrtOptions {
verbose?: boolean;
maxIterations?: number;
tolerance?: BN;
}
interface SqrtResultWithMetadata {
result: BN;
iterations: number;
converged: boolean;
executionTime: number;
}
`
Development
$3
- Node.js (v14 or higher)
- npm or yarn
$3
`bash
Clone the repository
git clone
cd sqrt-enhanced
Install dependencies
npm install
Build the project
npm run build
Run tests
npm test
Development mode (watch for changes)
npm run dev
`
$3
- npm run build: Compile TypeScript to JavaScript
- npm run test: Run the test script
- npm run dev: Watch mode for development
Algorithm
The library uses the Babylonian method (also known as Newton's method) for calculating square roots:
1. Start with an initial guess: x = n >> (bitLength/2)
2. Iteratively refine the guess: x = (n/x + x) / 2
3. Continue until convergence: x < y`