Skip to content

Conversation

Siddhram
Copy link

Overview

This PR introduces a comprehensive and well-documented implementation of the Self-Dividing Numbers problem in R, featuring efficient digit extraction and divisibility checking.

A self-dividing number is a number that is divisible by every digit it contains and does not contain the digit 0 (since division by zero is undefined).
The algorithm efficiently examines each number in a given range by extracting its digits and validating divisibility.


Features

  • Optimized O(m × log₁₀(n)) algorithm
    • For each number in the given range, extract digits using modulo (%) and division (/)
    • Skip immediately if any digit is 0 or if original_number %% digit != 0
    • Return TRUE only if all digits divide the number evenly
  • Multiple implementation variants:
    • is_self_dividing_number() — Checks an individual number
    • self_dividing_numbers() — Finds all self-dividing numbers in range [left, right]
    • self_dividing_numbers_vectorized() — Vectorized version for improved performance
    • analyze_number_digits() — Debug helper for visualizing digit breakdown
  • Comprehensive input validation and error handling
  • Extensive test suite covering edge cases and performance benchmarks
  • Consistent roxygen2 documentation matching other scripts in the mathematics module

Complexity

  • Time Complexity: O(m × log₁₀(n))
    where m = right - left + 1, and log₁₀(n) is the number of digits in each number.
  • Space Complexity: O(m) — for storing the result list.

Directory

  • Updated DIRECTORY.md
    Added "Self Dividing Numbers" entry under Mathematics

Demonstration

Run the script to execute built-in examples and validations:

PowerShell

Rscript "mathematics/self_dividing_numbers.r"

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.

1 participant