VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-large-number-divisible-13-not/

⇱ Check if a larger number is divisible by 13 or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a larger number is divisible by 13 or not

Last Updated : 12 Aug, 2025

Given a number s represented as a string, determine whether the integer it represents is divisible by 13 or not.

Examples :

Input: s = "2911285"
Output: true
Explanation: 2911285 / 13 = 223945, which is a whole number with no remainder.

Input: s = "27"
Output: false
Explanation: 27 / 13 ≈ 2.0769..., which is not a whole number (there is a remainder).

[Naive Approach] Modulo Division

If the given number is small, we can easily check whether it is divisible by 13 by computing s % 13 and verifying if the result is 0.

Note: This method only works when the number fits within standard integer data types. For very large numbers (such as those represented as strings with hundreds or thousands of digits), this approach may lead to overflow or be unsupported, so a string-based method is required.


Output
true

Time Complexity: O(n), n is length of s
Auxiliary Space: O(1)

[Expected Approach 1] Alternating Sum of 3-Digit Blocks

A number is divisible by 13 if and only if the alternating sum of its 3-digit blocks, taken from right to left, is divisible by 13.

Step by Step approach -

  • Pad the number so its length is a multiple of 3
    -> If the number of digits is not a multiple of 3, append zeros to the right so each block has exactly 3 digits.
    -> Example: "2911285""291128500" (after padding with two zeros).
  • Split into 3-digit blocks from right to left
    -> Example: "291128500" → blocks: 500, 128, 291 (right to left order).
  • Apply alternating signs starting with + on the rightmost block
    -> Pattern from right to left: + block , - block, + block, …
    -> Example: +500 - 128 + 291.
  • Sum the results
    -> Example: 500 - 128 + 291 = 663.
  • Check divisibility by 13
    -> If the sum is divisible by 13, the original number is divisible by 13.
    -> Example: 663 % 13 == 0 → divisible.

Output
true

Time Complexity: O(n), n is length of s
Auxiliary Space: O(1)

[Expected Approach 2] String-Based Modulo

We process the number digit by digit from left to right, maintaining the remainder modulo 13 at each step using the formula:
rem = (rem * 10 + digit) % 13.

Step by Step Approach -

  • Initialize remainder:
    -> rem = 0
  • Process each digit from left to right:
    -> Digit '2': rem = (0 * 10 + 2) % 13 = 2
    -> Digit '9': rem = (2 * 10 + 9) % 13 = 29 % 13 = 3
    -> Digit '1': rem = (3 * 10 + 1) % 13 = 31 % 13 = 5
    -> Digit '1': rem = (5 * 10 + 1) % 13 = 51 % 13 = 12
    -> Digit '2': rem = (12 * 10 + 2) % 13 = 122 % 13 = 5
    -> Digit '8': rem = (5 * 10 + 8) % 13 = 58 % 13 = 6
    -> Digit '5': rem = (6 * 10 + 5) % 13 = 65 % 13 = 0
  • Since final rem = 0, the number 2911285 is divisible by 13.

Output
true

Time Complexity: O(n), n is length of s
Auxiliary Space: O(1)

Comment