Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if any large number is divisible by 19 or not in Python
In mathematics, divisibility helps us determine whether a number can be divided by another without leaving a remainder. For example, 6 is divisible by 2 because 6/2 = 3 with no remainder.
In this article, we will learn how to check if any large number is divisible by 19 in Python using the modulus operator.
Why Python Handles Large Numbers Well
Checking divisibility with small numbers is straightforward using standard arithmetic operations. However, when dealing with very large numbers (containing 20, 30, or even 100 digits), many programming languages encounter issues due to integer size limits.
Python has a built-in int data type that can handle arbitrarily large integers. This means we can input and process extremely large numbers in Python without any special libraries or configurations.
Problem Examples
Scenario 1
Input: 133 Output: True Explanation: The number 133 is divisible by 19 (133 ÷ 19 = 7, remainder 0).
Scenario 2
Input: 9988688236837413373 Output: False Explanation: The large number leaves a remainder when divided by 19, so it is not divisible.
Using the Modulus Operator (%)
The modulus operator returns the remainder of division between two numbers. It is denoted by the symbol '%'.
Syntax
number % divisor
To check if a number is divisible by 19, we use the condition number % 19 == 0. If the remainder is zero, the number is divisible by 19.
Example 1: Small Number
Let's check whether the number 133 is divisible by 19 ?
def is_divisible_by_19(number):
return number % 19 == 0
result = is_divisible_by_19(133)
print(f"133 is divisible by 19: {result}")
print(f"133 ÷ 19 = {133 // 19}, remainder = {133 % 19}")
133 is divisible by 19: True 133 ÷ 19 = 7, remainder = 0
Example 2: Large Number
Now let's test with a very large integer ?
def is_divisible_by_19(number):
return number % 19 == 0
large_number = 9988688236837413373
result = is_divisible_by_19(large_number)
print(f"{large_number} is divisible by 19: {result}")
print(f"Remainder when divided by 19: {large_number % 19}")
9988688236837413373 is divisible by 19: False Remainder when divided by 19: 1
Example 3: Testing Multiple Large Numbers
Let's test several large numbers to demonstrate Python's capability ?
def check_divisibility_by_19(numbers):
for num in numbers:
is_divisible = num % 19 == 0
remainder = num % 19
print(f"{num}: Divisible = {is_divisible}, Remainder = {remainder}")
# Test with various large numbers
test_numbers = [
123456789012345678901234567890,
999999999999999999999999999999,
19 * 1000000000000000000000, # This should be divisible
987654321098765432109876543210
]
check_divisibility_by_19(test_numbers)
123456789012345678901234567890: Divisible = False, Remainder = 15 999999999999999999999999999999: Divisible = False, Remainder = 9 19000000000000000000000: Divisible = True, Remainder = 0 987654321098765432109876543210: Divisible = False, Remainder = 16
Key Points
| Aspect | Details |
|---|---|
| Operator | % (modulus operator) |
| Condition | number % 19 == 0 |
| Python Advantage | Handles arbitrarily large integers |
| Time Complexity | O(1) for the operation itself |
Conclusion
Python's built-in integer type makes checking divisibility by 19 straightforward for numbers of any size. The modulus operator (%) efficiently determines if a number is divisible by 19 by checking if the remainder equals zero.
