My Prime Library is a comprehensive TypeScript/React package for prime number operations and visualization. This library provides optimized algorithms for prime checking, generation, factorization, and advanced prime analysis with a feature-rich React com
npm install my-prime-librarybash
npm install my-prime-library
`
---
Usage
$3
Fast primality testing with memoization:
`javascript
import { isPrime } from 'my-prime-library';
console.log(isPrime(7)); // true
console.log(isPrime(10)); // false
console.log(isPrime(7919)); // true (large prime)
`
$3
Generate all prime numbers up to a limit using optimized Sieve of Eratosthenes:
`javascript
import { generatePrimes } from 'my-prime-library';
console.log(generatePrimes(10)); // [2, 3, 5, 7]
console.log(generatePrimes(30)); // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
`
$3
Complete prime factorization for any integer:
`javascript
import { primeFactorization } from 'my-prime-library';
console.log(primeFactorization(12)); // [2, 2, 3]
console.log(primeFactorization(84)); // [2, 2, 3, 7]
console.log(primeFactorization(97)); // [97] (prime number)
`
$3
Find next and previous prime numbers:
`javascript
import { getNextPrime, getPreviousPrime } from 'my-prime-library';
console.log(getNextPrime(10)); // 11
console.log(getNextPrime(17)); // 19
console.log(getPreviousPrime(10)); // 7
console.log(getPreviousPrime(17)); // 13
`
$3
Analyze gaps between consecutive primes:
`javascript
import { getPrimeGaps } from 'my-prime-library';
const gaps = getPrimeGaps(20);
console.log(gaps);
// [
// { prime1: 2, prime2: 3, gap: 1 },
// { prime1: 3, prime2: 5, gap: 2 },
// { prime1: 5, prime2: 7, gap: 2 },
// { prime1: 7, prime2: 11, gap: 4 },
// { prime1: 11, prime2: 13, gap: 2 },
// { prime1: 13, prime2: 17, gap: 4 },
// { prime1: 17, prime2: 19, gap: 2 }
// ]
`
$3
Probabilistic testing for very large numbers:
`javascript
import { isPrimeMillerRabin } from 'my-prime-library';
// Test large numbers efficiently
console.log(isPrimeMillerRabin(982451653)); // true (large prime)
console.log(isPrimeMillerRabin(982451654)); // false
`
$3
Feature-rich prime rendering with multiple display modes:
`jsx
import React from 'react';
import { PrimeRenderer } from 'my-prime-library';
const App = () => {
const handlePrimeClick = (prime, event) => {
console.log(Clicked prime: ${prime});
};
const formatPrime = (prime) => {
return ๐ข ${prime};
};
return (
Prime Numbers Gallery
{/ Basic list view /}
{/ Grid view with custom styling /}
limit={100}
listType="grid"
highlightedPrimes={[2, 3, 5, 7]}
onPrimeClick={handlePrimeClick}
formatPrime={formatPrime}
/>
{/ Virtualized view for large ranges /}
limit={10000}
listType="virtualized"
pageSize={50}
containerHeight={300}
showStats={true}
showPagination={true}
/>
);
};
export default App;
`
$3
Control memory usage with cache management:
`javascript
import { clearPrimeCache } from 'my-prime-library';
// Clear all cached prime computations
clearPrimeCache();
`
---
API Reference
$3
#### isPrime(number)
- Description: Fast primality testing with memoization and optimized algorithms
- Parameters:
- number (number): The number to check (must be finite and non-negative)
- Returns:
- boolean: true if the number is prime, false otherwise
- Features: Input validation, caching, optimized 6kยฑ1 algorithm
#### generatePrimes(limit)
- Description: Generates all prime numbers up to a given limit using Sieve of Eratosthenes
- Parameters:
- limit (number): The upper limit for generating primes (must be finite and non-negative)
- Returns:
- number[]: An array of prime numbers
- Features: Memoization, efficient sieve algorithm, automatic caching
#### primeFactorization(num)
- Description: Complete prime factorization for any integer greater than 1
- Parameters:
- num (number): The number to factorize (must be > 1)
- Returns:
- number[]: An array of prime factors in ascending order
- Throws: RangeError if number is less than or equal to 1
#### getNextPrime(num)
- Description: Finds the next prime number after the given number
- Parameters:
- num (number): The starting number (must be finite and non-negative)
- Returns:
- number: The next prime number
#### getPreviousPrime(num)
- Description: Finds the previous prime number before the given number
- Parameters:
- num (number): The starting number (must be finite and non-negative)
- Returns:
- number: The previous prime number
- Throws: RangeError if no prime exists before the given number
#### getPrimeGaps(limit)
- Description: Calculates gaps between consecutive primes up to a limit
- Parameters:
- limit (number): The upper limit for prime gap analysis
- Returns:
- PrimeGap[]: Array of objects with prime1, prime2, and gap properties
#### isPrimeMillerRabin(num, k)
- Description: Probabilistic primality test for large numbers using Miller-Rabin algorithm
- Parameters:
- num (number): The number to test
- k (number): Number of witness rounds (default: 5)
- Returns:
- boolean: true if probably prime, false if definitely composite
- Features: Automatically uses regular isPrime for numbers < 1,000,000
#### clearPrimeCache()
- Description: Clears all cached prime computations to free memory
- Parameters: None
- Returns: void
$3
####
A comprehensive React component for rendering prime numbers with multiple display modes and customization options.
Props:
- limit (number): The upper limit for generating primes (required)
- listType ('list' | 'grid' | 'virtualized'): Display mode (default: 'list')
- itemHeight (number): Height of each item in virtualized mode (default: 40)
- containerHeight (number): Container height for virtualized mode (default: 400)
- pageSize (number): Number of items per page in virtualized mode (default: 100)
- asyncThreshold (number): Threshold for async processing (default: 10000)
- onPrimeClick (function): Click handler for prime items
- formatPrime (function): Custom formatting function for prime display
- className (string): Additional CSS class names
- showStats (boolean): Show statistics panel (default: true)
- showPagination (boolean): Show pagination controls (default: true)
- highlightedPrimes (number[]): Array of primes to highlight
- customStyledPrimes (number[]): Array of primes with custom styling
TypeScript Interfaces:
`typescript
interface PrimeGap {
prime1: number;
prime2: number;
gap: number;
}
interface PrimeRendererProps {
limit: number;
listType?: 'list' | 'grid' | 'virtualized';
itemHeight?: number;
containerHeight?: number;
pageSize?: number;
asyncThreshold?: number;
onPrimeClick?: (prime: number, event: React.MouseEvent) => void;
formatPrime?: (prime: number) => string;
className?: string;
showStats?: boolean;
showPagination?: boolean;
highlightedPrimes?: number[];
customStyledPrimes?: number[];
}
`
---
Development
To work on the library locally:
1. Clone the repository:
`bash
git clone https://github.com/your-username/my-prime-library.git
cd my-prime-library
`
2. Install dependencies:
`bash
npm install
`
3. Build the library:
`bash
npm run build
`
4. Run tests:
`bash
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests with UI
npm run test:ui
# Watch mode for development
npm run test:watch
`
5. Type checking:
`bash
# One-time type check
npm run type-check
# Watch mode for type checking
npm run type-check:watch
`
6. Bundle analysis:
`bash
# Build and analyze bundle size
npm run build:analyze
`
7. Test the library locally:
`bash
npm link
`
8. Use it in another project:
`bash
npm link my-prime-library
`
$3
`
my-prime-library/
โโโ src/
โ โโโ components/
โ โ โโโ PrimeRenderer.tsx # React component
โ โโโ utils/
โ โ โโโ primes.ts # Core prime algorithms
โ โโโ test/
โ โโโ primes.test.js # Core function tests
โ โโโ PrimeRenderer.test.jsx # Component tests
โ โโโ edge-cases.test.js # Edge case testing
โ โโโ performance.test.js # Performance benchmarks
โ โโโ setup.js # Test configuration
โโโ dist/ # Built distributions
โโโ scripts/
โ โโโ analyze-bundle.js # Bundle analysis script
โโโ package.json # Dependencies and scripts
โโโ rollup.config.js # Build configuration
โโโ vitest.config.js # Test configuration
โโโ README.md # This file
`
$3
The library uses Rollup for bundling with the following outputs:
- ESM: dist/index.esm.js - Modern ES modules
- CommonJS: dist/index.cjs.js - Node.js compatible
- UMD: dist/index.umd.js - Browser compatible
- Type Definitions: dist/index.d.ts - TypeScript support
$3
Comprehensive test suite including:
- Unit tests for all core functions
- Component testing with React Testing Library
- Edge case validation
- Performance benchmarks
- Memory leak detection
- Type safety validation
---
Performance
$3
- isPrime(): O(โn) with 6kยฑ1 optimization
- generatePrimes(): O(n log log n) using Sieve of Eratosthenes
- primeFactorization(): O(โn) in worst case
- isPrimeMillerRabin(): O(k logยณn) where k is witness count
$3
Typical performance on modern hardware:
- Check primality of numbers up to 1,000,000: < 1ms
- Generate primes up to 100,000: < 10ms
- Generate primes up to 1,000,000: < 100ms
- Factorize numbers up to 1,000,000: < 5ms
$3
- Automatic caching for frequently accessed primes
- Configurable cache clearing with clearPrimeCache()
- Virtualized rendering for large prime lists in React component
- Memory-efficient sieve implementation
Browser Support
- React: 16.8+ (hooks support required)
- Modern Browsers: ES2018+ support recommended
- Node.js: 14+ for CommonJS/ESM modules
- TypeScript: 4.5+ for full type support
Version History
$3
- Added Miller-Rabin primality test for large numbers
- Enhanced PrimeRenderer with virtualized rendering
- Improved caching algorithms and memory management
- Added comprehensive TypeScript definitions
- Extended test coverage with performance benchmarks
$3
- Initial release with core prime functions
- Basic React component implementation
- Sieve of Eratosthenes implementation
- Essential primality testing
---
License
This project is licensed under the MIT License. See the LICENSE file for details.
---
Contributing
Contributions are welcome! Please follow these guidelines:
1. Fork the repository
2. Create a feature branch (git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)