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

Rate this post

π‘ Problem Formulation: Divisibility checks are a common necessity in algorithm designs and number theory problems. Specifically, we often need to determine if a larger integer is divisible by a smaller one without a remainder. For instance, checking if the number “12345678901234567890” is divisible by 17 is such a case. The output should be a simple boolean value: `True` if divisible, `False` otherwise.

## Method 1: Using Modular Arithmetic

The first method involves using modular arithmetic which is a system of arithmetic for integers, where numbers “wrap around” after they reach a certain valueβthe modulus. In Python, the modulus operator `%` returns the remainder of a division. If a number is divisible by another, the remainder is zero. This method is straightforward and efficient, well-suited for large numbers.

Here’s an example:

```def is_divisible_by_17(number):
return number % 17 == 0

print(is_divisible_by_17(12345678901234567890))
```

Output:

```False
```

In this snippet, the function `is_divisible_by_17()` returns `True` only if the given large number leaves no remainder when divided by 17. This makes it a simple and effective way to check divisibility.

## Method 2: Using Iterative Subtraction

In this method, we repeatedly subtract 17 from the number until we reach a number smaller than 17. If the final result is 0, the original number is divisible by 17. This method is not recommended for very large numbers due to its inefficiency, but it demonstrates the concept of divisibility without performing direct division.

Here’s an example:

```def is_divisible_by_17(number):
while number >= 17:
number -= 17
return number == 0

print(is_divisible_by_17(12345678901234567890))
```

Output:

```False
```

The code snippet shows an iterative process that subtracts 17 repeatedly and checks if the number eventually equals zero. While educational, it is computationally intensive for large numbers.

## Method 3: Using String Conversion

This method leverages the fact that Python can handle large integers and string conversions. We convert the large number to a string, split it into chunks that represent smaller numbers each less than 17, and then check the divisibility of the cumulative total. This approach is helpful when dealing with numbers that exceed the integer limit in other programming languages but can be slower due to the string manipulations involved.

Here’s an example:

```def is_divisible_by_17(number):
num_str = str(number)
total = 0
for i in range(0, len(num_str), 2):
total += int(num_str[i:i+2])

print(is_divisible_by_17(12345678901234567890))
```

Output:

```False
```

This example demonstrates converting a large number into a string and processing it in chunks to check for divisibility by 17. This method works with extremely large numbers but is slower due to string handling.

## Method 4: Using Integer Division

A more numerical approach is to use integer division to iteratively reduce the number by dividing by 17 and checking if the final quotient is an integer without a remainder. This method utilizes Python’s ability to work with large integer division without losing precision.

Here’s an example:

```def is_divisible_by_17(number):
while number >= 17:
number //= 17
return number == 1

print(is_divisible_by_17(12345678901234567890))
```

Output:

```False
```

This code uses the floor division operator `//` to reduce the number iteratively and checks if the final quotient equals 1, which implies that the initial number was exactly divisible by 17. It can handle large integers efficiently.

## Bonus One-Liner Method 5: Using Lambda Functions

For a compact and concise check, a one-liner using a lambda function can provide an immediate answer. This method is Pythonic and efficient but can be less readable for beginners.

Here’s an example:

```is_divisible_by_17 = lambda number : number % 17 == 0

print(is_divisible_by_17(12345678901234567890))
```

Output:

```False
```

The lambda function created here provides an anonymized and immediate check for divisibility by 17. It uses the modulus operator just as in Method 1, but in a more succinct form.

## Summary/Discussion

• Method 1: Modular Arithmetic. Strengths: Simple and efficient. Weaknesses: None, this is the optimal approach.
• Method 2: Iterative Subtraction. Strengths: Offers a tangible understanding of divisibility. Weaknesses: Highly inefficient for large numbers.
• Method 3: Using String Conversion. Strengths: Capable of handling very large numbers exceeding other language’s integer limits. Weaknesses: Slower due to string manipulation.
• Method 4: Integer Division. Strengths: Efficiently handles large numbers without precision loss. Weaknesses: Slightly less straightforward than modular arithmetic.
• Method 5: Lambda Functions. Strengths: Pythonic and concise. Weaknesses: Can be less readable to those unfamiliar with lambda functions.