Check if any large number is divisible by 17 or not in Python

Checking if a large number is divisible by 17 can be efficiently done using a repeated subtraction method. Instead of performing direct division, we extract the last digit and subtract it multiplied by 5 from the remaining number until we get a manageable two-digit number.

The algorithm works because of the mathematical property that a number is divisible by 17 if and only if the number formed by removing the last digit and subtracting 5 times the last digit is also divisible by 17.

Algorithm Steps

To solve this problem, we follow these steps:

  • While the number has more than two digits (number ? 100):
    • Extract the last digit: last_digit = number % 10
    • Remove the last digit: number = number // 10
    • Subtract 5 times the last digit: number = number - last_digit * 5
  • Check if the final two-digit number is divisible by 17

Implementation

Here's the Python implementation of the divisibility check:

def is_divisible_by_17(number):
    while number >= 100:
        last_digit = number % 10
        number //= 10
        number -= last_digit * 5
    return number % 17 == 0

# Test with the given example
number = 99943
if is_divisible_by_17(number):
    print("Divisible")
else:
    print("Not Divisible")
Divisible

Step-by-Step Trace

Let's trace through the algorithm with number 99943:

def trace_divisibility_check(number):
    print(f"Checking if {number} is divisible by 17:")
    original = number
    
    step = 1
    while number >= 100:
        last_digit = number % 10
        number //= 10
        number -= last_digit * 5
        print(f"Step {step}: Last digit = {last_digit}, New number = {number}")
        step += 1
    
    result = number % 17 == 0
    print(f"Final number: {number}")
    print(f"{original} is {'divisible' if result else 'not divisible'} by 17")
    return result

trace_divisibility_check(99943)
Checking if 99943 is divisible by 17:
Step 1: Last digit = 3, New number = 9979
Step 2: Last digit = 9, New number = 952
Step 3: Last digit = 2, New number = 85
Final number: 85
99943 is divisible by 17

Testing Multiple Examples

Let's test the function with various numbers to verify its correctness:

def is_divisible_by_17(number):
    while number >= 100:
        last_digit = number % 10
        number //= 10
        number -= last_digit * 5
    return number % 17 == 0

# Test multiple numbers
test_numbers = [99943, 17, 34, 51, 100, 123, 170]

for num in test_numbers:
    result = is_divisible_by_17(num)
    # Verify with direct division
    actual = num % 17 == 0
    status = "?" if result == actual else "?"
    print(f"{num}: {result} {status}")
99943: True ?
17: True ?
34: True ?
51: True ?
100: False ?
123: False ?
170: True ?

How It Works

The algorithm is based on the mathematical theorem that for divisibility by 17, we can use the recurrence relation: If a number N = 10a + b (where b is the last digit), then N ? a - 5b (mod 17).

This means that N is divisible by 17 if and only if (a - 5b) is divisible by 17. By repeatedly applying this transformation, we reduce the number to a manageable size for direct checking.

Conclusion

The repeated subtraction method provides an efficient way to check divisibility by 17 for large numbers without performing actual division. This algorithm reduces computational complexity and works reliably for numbers of any size.

Updated on: 2026-03-25T14:18:55+05:30

779 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements