Problem 42418. Divisible by 16
Write a function to determine if a number is divisible by 16. This can be done by a few different methods. Here are two:
- If a number has four or more digits, take the last three digits. Add eight to it if the thousands digit in the original number is odd (zero if even). If this three-digit number is divisible by 16, so is the original number. The resulting number can also be reduced by the following method.
- Take the last two digits and add them to four times the remaining number. Apply this method recursively until a two-digit number remains. As usual, if the resulting number is divisible by 16, then so is the original number.
A few of the function restrictions have been lifted.
Previous problem: divisible by 15. Next problem: divisible by n, prime vs. composite divisors.
Solution Stats
Problem Comments
-
3 Comments
Another trick: if the last 4 digits of the number are divisible by 16, the full number is divisible by 16. So far as I know, if the last X digits of a number are divisible by 2^X, the entire number is divisible by 2^X.
@James: nice trick! (and I guess the proof arises from 10^x being always exactly divisible by 2^x, so "iff" also applies?)
perhaps less interesting but I guess you could do the same with powers of 5, iff the last X digits of a number are divisible by 5^x, then the entire number is divisible by 5^x...
Solution Comments
Show commentsProblem Recent Solvers171
Suggested Problems
-
Omit columns averages from a matrix
586 Solvers
-
1510 Solvers
-
Implement simple rotation cypher
1066 Solvers
-
9158 Solvers
-
1831 Solvers
More from this Author139
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!