askvity

What is a Beautiful Number?

Published in Number Theory 2 mins read

A number is considered "beautiful" if for every digit x present in the number, there are exactly x occurrences of that digit within the number itself.

Explanation:

In essence, a beautiful number is self-descriptive in terms of the frequency of its digits. To determine if a number is beautiful, you need to count how many times each unique digit appears. If the count of each digit matches the digit's value, then the number is beautiful.

Examples:

  • 1: This is a beautiful number. The digit 1 appears once.
  • 3133: This is a beautiful number. The digit 1 appears once, and the digit 3 appears three times.
  • 224: This is not a beautiful number. The digit 2 appears twice, which is correct, but the digit 4 appears only once, not four times.
  • 42124: This is a beautiful number. The digit 1 appears once, the digit 2 appears twice, and the digit 4 appears four times.

How to Check if a Number is Beautiful:

You can programmatically determine if a number is beautiful using the following steps:

  1. Convert the number to a string. This allows easy iteration through the digits.
  2. Create a dictionary or hash map to store the counts of each digit. Iterate through the string, and for each digit, increment its count in the dictionary.
  3. Iterate through the dictionary. For each digit and its count, check if the digit is equal to its count. If any digit fails this check, the number is not beautiful.
  4. If all digits pass the check, the number is beautiful.

Example in Python:

def is_beautiful(number):
  """
  Checks if a number is beautiful.

  Args:
    number: The number to check (as an integer).

  Returns:
    True if the number is beautiful, False otherwise.
  """
  number_str = str(number)
  digit_counts = {}
  for digit in number_str:
    digit = int(digit)  # Convert digit back to integer for counting
    digit_counts[digit] = digit_counts.get(digit, 0) + 1

  for digit, count in digit_counts.items():
    if digit != count:
      return False

  return True

# Example Usage:
print(is_beautiful(1))      # True
print(is_beautiful(3133))   # True
print(is_beautiful(224))     # False
print(is_beautiful(42124))    #True

Related Articles