Python icon indicating copy to clipboard operation
Python copied to clipboard

Add Kaprekar number checker to special_numbers

Open Sean-Randall opened this issue 8 months ago • 5 comments

This PR adds a new function is_kaprekar_number(n) under maths/special_numbers/.

The function determines whether a number is a Kaprekar number based on digit-splitting logic, where the square of a number is divided into two parts that sum to the original number. It includes inline doctests to demonstrate usage and ensure correctness.

✔️ This contribution follows all repository contribution guidelines:

  • Type hints included (n: int -> bool)
  • File and function names follow lowercase conventions
  • Clean formatting per PEP8
  • Validated locally using doctest.testmod()

🎯 Educational Value: This function enhances the repository's coverage of special number classifications and provides a clean, beginner-friendly example of digit-based number theory in Python.

Tested and ready for review. Thank you for maintaining this valuable resource!

Sean-Randall avatar May 11 '25 01:05 Sean-Randall

Shouldn't 10 be a Kaprekar number, since 10^2 = 100, and "100" = "10" + "0" gives 10?

https://cs.uwaterloo.ca/journals/JIS/VOL3/iann2a.html#:~:text=For%20each%20n%20%3E%3D%201%20%2C,)%20when%20N%20%3D%2010n%20.

10 is Disallowed, see above.

Sean-Randall avatar May 11 '25 23:05 Sean-Randall

Shouldn't 10 be a Kaprekar number, since 10^2 = 100, and "100" = "10" + "0" gives 10?

https://cs.uwaterloo.ca/journals/JIS/VOL3/iann2a.html#:~:text=For%20each%20n%20%3E%3D%201%20%2C,)%20when%20N%20%3D%2010n%20.

10 is Disallowed, see above.

Thanks for the link. Then, the definition should be clarified in the code function description to be: "Kaprekar numbers: positive numbers n such that n = q+r and n^2 = q*10^m+r, for some m >= 1, q >= 0 and 0 <= r < 10^m, with n != 10^a, a >= 1." (https://oeis.org/A006886)?

mindaugl avatar May 12 '25 02:05 mindaugl

Shouldn't 10 be a Kaprekar number, since 10^2 = 100, and "100" = "10" + "0" gives 10?

https://cs.uwaterloo.ca/journals/JIS/VOL3/iann2a.html#:~:text=For%20each%20n%20%3E%3D%201%20%2C,)%20when%20N%20%3D%2010n%20. 10 is Disallowed, see above.

Thanks for the link. Then, the definition should be clarified in the code function description to be: "Kaprekar numbers: positive numbers n such that n = q+r and n^2 = q*10^m+r, for some m >= 1, q >= 0 and 0 <= r < 10^m, with n != 10^a, a >= 1." (https://oeis.org/A006886)?

Updated the function to align with the strict definition of Kaprekar numbers per OEIS A006886 and Iannucci (1997). Powers of 10 are now explicitly excluded. Let me know if there's anything else to improve!

Sean-Randall avatar May 12 '25 02:05 Sean-Randall

Just added a comment in code, as the check for int(right) == 0 seems not to be necessary any more after adding explicit check for powers of 10.

Thanks! I removed the int(right) == 0 check as suggested. The new logic excludes powers of 10 directly. Let me know if anything else needs a tweak.

Sean-Randall avatar May 13 '25 02:05 Sean-Randall

All checks are now passing, including tests, Ruff style checks, and namespace packaging. The implementation follows the strict Kaprekar definition and excludes powers of 10 as required. Everything should be ready for merge. Thanks again for the helpful feedback!

Sean-Randall avatar May 13 '25 02:05 Sean-Randall