# 5 Best Ways to Check if Any Large Number is Divisible by 19 in Python

Rate this post

π‘ Problem Formulation: Checking divisibility by a particular number, such as 19, can be a common necessity in various computer science and mathematics problems. In Python, largenumbers can be handed off using different methods to ascertain if they are divisible by 19. For instance, if the input is 361, the desired output would be True because 361 is a multiple of 19.

## Method 1: Using Modulo Operator

This method involves the modulo operator, which computes the remainder of a division. If a number is divisible by another, the remainder of their division is zero. In this context, to check if a number is divisible by 19, we simply check if number % 19 == 0. This method is straightforward and efficient for any size of number.

Here’s an example:

large_number = 951
result = large_number % 19 == 0
print(result)

Output: True

The code snippet assigns the value 951 to the variable large_number and tests if it is divisible by 19. The expression large_number % 19 evaluates to zero if it’s divisible, leading the variable result to be True, which gets printed.

## Method 2: Using divmod Function

The divmod() function in Python returns a tuple containing the quotient and the remainder of the division. To check if the number is divisible by 19, we can test if the remainder is zero. This method has the extra benefit of providing the quotient directly should it be needed.

Here’s an example:

large_number = 3620
quotient, remainder = divmod(large_number, 19)
print(remainder == 0)

Output: True

By using divmod on large_number and 19, the code calculates both the quotient and remainder in one go. It checks if the remainder equals zero to determine divisibility by 19, with the result showing that 3620 is indeed divisible by 19.

## Method 3: Using Recursion

Recursion can be used to chunk down the number by subtracting multiples of 19 and check divisibility on a smaller number. This is more theoretical in nature, but it showcases an algorithmic approach to the problem, utilizing the function call stack.

Here’s an example:

def is_divisible_by_19(n):
if n < 19:
return False
if n == 19:
return True
return is_divisible_by_19(n - 19)

large_number = 798
print(is_divisible_by_19(large_number))

Output: True

The function is_divisible_by_19 subtracts 19 repeatedly from the input number until it either becomes less than 19, not divisible, or equals 19, indicating divisibility. The function calls itself recursively to perform this check, here confirming that 798 is divisible by 19.

## Method 4: Using Iterative Subtraction

Similar to the recursive method, an iterative approach involves continuously subtracting 19 from the number until what remains is less than 19. This approach replaces the recursive calls with a loop which some may find clearer and which avoids potential stack overflow issues with recursion on very large numbers.

Here’s an example:

large_number = 19457
while large_number >= 19:
large_number -= 19

print(large_number == 0)

Output: True

This snippet iteratively subtracts 19 from large_number until it is less than 19. If the final value of large_number is zero after the loop ends, it indicates divisibility. In this case, 19457 is divisible by 19.

## Bonus One-Liner Method 5: Using Lambda and Modulo

For those who prefer a concise, functional approach, a lambda function can be used. This one-liner defines a lambda that takes a number and returns True if it’s divisible by 19 and False otherwise, using the modulo operator.

Here’s an example:

is_divisible_by_19 = lambda x: x % 19 == 0
print(is_divisible_by_19(1900))

Output: True

By assigning a lambda to is_divisible_by_19, we create a function in a single line that checks divisibility. The example confirms that 1900 is divisible by 19 using this compact notation.

## Summary/Discussion

• Method 1: Using Modulo Operator. Simple and direct. Best for most cases. Efficiency can vary with the size of numbers.
• Method 2: Using divmod Function. Provides additional quotient. Slightly less straightforward than method 1 but useful when the quotient is also required.
• Method 3: Using Recursion. Illustrative of an algorithmic approach. Not very practical for large numbers due to recursion depth limits.
• Method 4: Using Iterative Subtraction. More practical and safer for large numbers than recursion. More verbose and less efficient than the modulo approach.
• Bonus One-Liner Method 5: Using Lambda and Modulo. Concise for one-off checks. Not as readable for those unfamiliar with lambda syntax. Less efficient if called repeatedly due to lambda redefinition.