Skip to content

Conversation

@forfudan
Copy link
Owner

@forfudan forfudan commented Jul 9, 2025

This PR implements the Burnikel-Ziegler fast recursive division algorithm which significantly improves the performance of large-number divisions (particularly when there are thousands of words). It achieves a realized time complexity of O(n^1.585) (3x time when digits are doubled) instead of the O(n^2) (4x time when digits are doubled).

Major Division Algorithm Improvements

1. Burnikel-Ziegler Algorithm Implementation

  • New fast recursive division algorithm for large numbers
  • Added floor_divide_burnikel_ziegler() function with cutoff at 64 words
  • Implemented supporting functions:
    • floor_divide_two_by_one()
    • floor_divide_three_by_two()
    • floor_divide_three_by_two_uint32()
    • floor_divide_four_by_two_uint32()

2. Division Strategy Refactoring

  • Renamed floor_divide_general()floor_divide_school()
  • Smart algorithm selection in floor_divide():
    • Small numbers (≤64 words): Use schoolbook division
    • Large numbers (>64 words): Use Burnikel-Ziegler algorithm
  • Extracted normalization logic into calculate_normalization_factor() helper function

Benchmark and Testing Enhancements

3. Extended Test Coverage

  • Increased test range from 2^16 to 2^18 words (262,144 words)
  • Added validation to compare Mojo vs Python results
  • Reduced iterations for large number tests (100 → 10)
  • Added new test cases for extremely large divisions

4. Improved Benchmarking

  • Extended Python string limit from 1M to 10M digits
  • Enhanced logging with result validation
  • Better error handling for mismatched results

Code Quality Improvements

5. Constructor Safety

  • Fixed BigUInt constructor to handle empty word lists
  • Added validation to prevent zero-length word arrays

6. Build Process

  • Improved formatting command to target specific directories
  • Better project organization

Performance Impact

The main achievement is dramatically improved performance for large number division:

  • O(n²) → O(n^1.585) complexity reduction using Burnikel-Ziegler
  • Automatic algorithm selection based on input size
  • Maintains compatibility with existing API

This is a significant algorithmic improvement that should provide substantial speedups for large number operations while maintaining correctness for all input sizes.

@forfudan forfudan merged commit b30cddc into main Jul 9, 2025
1 check passed
@forfudan forfudan deleted the div branch July 9, 2025 13:03
forfudan added a commit that referenced this pull request Jul 9, 2025
forfudan added a commit that referenced this pull request Jul 9, 2025
forfudan added a commit that referenced this pull request Jul 11, 2025
forfudan added a commit that referenced this pull request Jul 14, 2025
forfudan added a commit that referenced this pull request Jul 15, 2025
forfudan added a commit that referenced this pull request Jul 18, 2025
forfudan added a commit that referenced this pull request Jul 24, 2025
forfudan added a commit that referenced this pull request Jul 26, 2025
forfudan added a commit that referenced this pull request Jul 31, 2025
forfudan added a commit that referenced this pull request Aug 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants