''' A module for playing around with number theory. ''' import itertools import gradio as gr def find_divisors(number: int) -> list[int]: """ Find the divisors of a given positive integer. number: int - The number to find divisors for, must be between 1 and 1 billion. Returns a sorted list of divisors. """ if not isinstance(number, int) or number <= 0 or number > 1000000000: gr.Warning("Please enter a positive integer between 1 and 1 billion.") return [] divisors = set() for i in range(1, int(number**0.5) + 1): if number % i == 0: divisors.add(i) divisors.add(number // i) # Convert the set to a list and sort it. return sorted(list(divisors)) def is_semi_perfect(divisors: list[int], number: int) -> bool: """ Check if the number is semi-perfect (i.e., some subset of its proper divisors sums to the number). """ proper_divisors = [d for d in divisors if d != number] # Check all non-empty subsets for r in range(1, len(proper_divisors) + 1): for subset in itertools.combinations(proper_divisors, r): if sum(subset) == number: return True return False def is_happy_number(number: int) -> bool: """ Determine if a number is a happy number. A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers). """ seen = set() while number != 1 and number not in seen: seen.add(number) number = sum(int(digit) ** 2 for digit in str(number)) return number == 1 def get_number_types(divisors: list[int], number: int) -> list[str]: """ Determine the types of a given number based on its divisors. Returns a list of types: "Prime", "Perfect", "Abundant", "Deficient", "Semi-Perfect", "Weird". divisors: list[int] - The divisors of the number. number: int - The number to classify. Returns a list of number types. """ types = [] if len(divisors) == 2 and number > 1: types.append("Prime") sum_divisors = sum(d for d in divisors if d != number) is_abundant = sum_divisors > number is_semi = is_semi_perfect(divisors, number) if sum_divisors == number: types.append("Perfect") elif is_abundant: types.append("Abundant") else: types.append("Deficient") if is_semi: types.append("Semi-Perfect") if is_abundant and not is_semi: types.append("Weird") # Happy/Unhappy logic if is_happy_number(number): types.append("Happy") else: types.append("Unhappy") return types def calculate_output(number: int) -> tuple[str, list[str]]: """ Calculate the divisors and types of a given number. number: int - The number to process, must be between 1 and 1 million. Returns a tuple containing a string of divisors and a list of types. """ divisors = find_divisors(number) divisors_str = ", ".join(map(str, divisors)) if divisors else "" types = get_number_types(divisors, number) return divisors_str, types with gr.Blocks() as demo: gr.Markdown("## Number Processor") number_input = gr.Number(label="Enter a positive integer between 1 and 1 billion", value=1) output = gr.Textbox(label= "Divisors", lines=2) type_checkboxes = gr.CheckboxGroup( ["Abundant", "Deficient", "Perfect", "Prime", "Semi-Perfect", "Weird", "Happy", "Unhappy"], label="Type(s) of number", info="Your number is..." ) btn_calc = gr.Button("Investigate my number") with gr.Accordion("Definitions for number types", open=False): gr.Markdown(""" **Abundant**: A number for which the sum of its proper divisors is greater than the number itself. **Deficient**: A number for which the sum of its proper divisors is less than the number itself. **Perfect**: A number for which the sum of its proper divisors is equal to the number itself. **Prime**: A number greater than 1 that has no positive divisors other than 1 and itself. **Semi-Perfect**: A number for which some subset of its proper divisors sums to the number itself. **Weird**: A number that is abundant but not semi-perfect. **Happy**: A number that eventually reaches 1 when replaced by the sum of the squares of its digits. **Unhappy**: A number that does not reach 1 (it becomes a continuous cycle e.g. 4 > 16 > 37 > 58 > 89 > 145 > 42 > 20 > 4). **Proper Divisors**: The divisors of a number excluding the number itself. """) btn_calc.click( fn=calculate_output, inputs=number_input, outputs=[output, type_checkboxes] ) demo.launch()