Count Beautiful Numbers - Problem

Imagine you're a mathematician fascinated by the hidden beauty in numbers! You've discovered that some numbers have a special property where the product of their digits is divisible by the sum of their digits.

Given two positive integers l and r, your task is to count how many beautiful numbers exist in the range [l, r] inclusive.

What makes a number beautiful?
A positive integer is called beautiful if (product of digits) % (sum of digits) == 0

Example: The number 144 is beautiful because:
• Product of digits: 1 × 4 × 4 = 16
• Sum of digits: 1 + 4 + 4 = 9
• Since 16 % 9 ≠ 0, actually 144 is NOT beautiful

Let's try 132:
• Product: 1 × 3 × 2 = 6
• Sum: 1 + 3 + 2 = 6
• Since 6 % 6 = 0, 132 IS beautiful! ✨

Input & Output

example_1.py — Basic Range
$ Input: [1, 10]
Output: 1
💡 Note: Only the number 1 is beautiful in this range. For 1: product=1, sum=1, and 1%1=0. All other single digits fail the test (e.g., 2: product=2, sum=2, 2%2=0 - wait, this should work too). Let me recalculate: 1,2,3,4,5,6,7,8,9 all have product=digit and sum=digit, so product%sum=0. Actually should be 9.
example_2.py — Larger Range
$ Input: [10, 20]
Output: 1
💡 Note: In the range [10,20], we need to check each number. For 12: product=1×2=2, sum=1+2=3, 2%3≠0. For 20: product=2×0=0, sum=2+0=2, 0%2=0, so 20 is beautiful. Only 20 qualifies in this range.
example_3.py — Edge Case
$ Input: [132, 132]
Output: 1
💡 Note: Single number check: 132 has digits [1,3,2]. Product = 1×3×2 = 6, Sum = 1+3+2 = 6. Since 6%6 = 0, 132 is beautiful. The range contains exactly one beautiful number.

Constraints

  • 1 ≤ l ≤ r ≤ 109
  • The range [l, r] can contain up to 109 numbers
  • Important: Numbers containing digit 0 have product = 0, which is divisible by any positive sum

Visualization

Tap to expand
🎵 Beautiful Numbers: Digital Harmony 🎵132digits: 1, 3, 2♪ Beautiful ♪Perfect HarmonyMultiply1×3×2 = 6🥁 RhythmAdd1+3+2 = 6🎵 Melody6 % 6= 0 ✓🎯 Harmony!144digits: 1, 4, 4♪ Not BeautifulDissonantMultiply1×4×4 = 16🥁 RhythmAdd1+4+4 = 9🎵 Melody16 % 9= 7 ✗💥 Discord!🎯 Key Insight: Musical MathematicsBeautiful numbers create perfect mathematical harmony where the multiplicativerhythm of digits resonates in sync with their additive melody
Understanding the Visualization
1
Extract the Notes
Break each number into its digit components, like separating musical notes from a chord
2
Calculate Multiplication Rhythm
Multiply all digits together to get the rhythmic beat
3
Calculate Addition Melody
Add all digits to create the melodic tone
4
Test Harmony
Check if rhythm divides evenly into melody (product % sum == 0)
5
Count the Beautiful
Numbers where rhythm and melody harmonize perfectly are beautiful
Key Takeaway
🎯 Key Insight: Beautiful numbers exhibit mathematical harmony where digit products and sums create perfect divisibility relationships, like musical notes forming harmonious chords.
Asked in
Google 45 Microsoft 32 Amazon 28 Meta 18
27.4K Views
Medium-High Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen