Determine Divisibility by 8 for Large Numeric Strings
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Evaluate whether a very large non-negative integer string is divisible by 8 without converting the entire string into an integer.
Given a non-negative integer represented as a string s, determine whether the integer is evenly divisible by 8.
Because s may be exceptionally long, you should avoid parsing the entire string into a numerical type (e.g., calling int(s) on the full input is not permitted).
Return True if the number is divisible by 8, and False otherwise.
Constraints
1 <= len(s) <= 10^6sconsists exclusively of digit characters ('0'through'9').smay contain leading zeros (for example,"00024").- Direct conversion of the full string
sto an integer is prohibited.
Examples
Example 1
Input: is_divisible_by_8('16')
Output: True
Example 2
Input: is_divisible_by_8('123')
Output: False
Example 3
Input: is_divisible_by_8('1000')
Output: True
Hints
Hint 1
Recall that 1000 is evenly divisible by 8 ($1000 = 8 \times 125$). What does this imply about higher positional powers of 10?
Hint 2
Since any multiple of 1000 contributes zero to the remainder modulo 8, only a small suffix of the string affects divisibility.
Hint 3
Consider how many trailing digits you actually need to examine, including when the input length is less than 3.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To test whether a large integer $N$ is divisible by 8, we do not need to process every digit in the string representation.
Any integer $N$ can be expressed as: $$N = 1000 \cdot q + r$$ where $r = N \pmod{1000}$ represents the value formed by the last three decimal digits of $N$.
Because $1000 = 8 \times 125$, the term $1000 \cdot q$ is always a multiple of 8. Therefore: $$N \pmod 8 = (1000 \cdot q + r) \pmod 8 = r \pmod 8$$
Thus, $N$ is divisible by 8 if and only if the integer formed by its last three digits is divisible by 8.
- Take the trailing substring of up to 3 characters (
s[-3:]). - Convert only this substring (at most 3 digits) to an integer.
- Check whether this numerical value is divisible by 8 (
value % 8 == 0).
Correctness & Edge Cases
- Strings with length $< 3$: Python's slice syntax
s[-3:]safely extracts the full string if its length is 1 or 2 (e.g.,"16"[-3:]yields"16"). The remainder of the whole number is tested directly. - Leading Zeros: Extra leading zeros do not alter the arithmetic value (e.g.,
"00016"yields"016", which parses to16). - All Zeros: Strings like
"0"or"0000"parse to0, which is divisible by 8 ($0 \pmod 8 = 0$).
Complexity Analysis
- Time Complexity: $\mathcal{O}(1)$. Extracting at most 3 characters and performing a modulo check takes constant time regardless of the total length of
s. - Space Complexity: $\mathcal{O}(1)$. Only a small substring of at most 3 characters is stored.
Python Source Code
def is_divisible_by_8(s: str) -> bool:
# 1000 is divisible by 8, so only the last 3 digits affect N % 8.
# s[-3:] extracts up to 3 trailing digits safely.
return int(s[-3:]) % 8 == 0