Problem 42404. Divisible by 2
This is the first problem in a set of "divisible by x" problems. You will be provided a number as a string and the function you write must return whether or not that number is divisible by x.
Rather than allowing you to use an easy way out, such as the modulus function, division and checking the result against its rounded value, Java functions, etc., you get to learn and apply integer divisibility rules. For reference, some divisibility conditions for integers are listed here. Other such references are also available elsewhere on the internet.
There are two benefits to approaching divisibility in this manner: first, you learn neat divisibility tricks that you can use in your head or on paper for large numbers and, second, you program a routine capable of determining divisibility for arbitrarily large numbers, for which existing precision fails. So, to begin...
Write a function to determine if a number is divisible by 2. This will be the case if its last digit is even.
(This problem blocks a range of functions in the first test case so that you must use one of the supplied methods; some of these restrictions will be lifted for later problems in the series.)
Next problem: divisible by 3.
Solution Stats
Problem Comments

8 Comments
how about function "rem"?
Thanks for the catch. I've added that function to the outlawed list for this and all subsequent problems in this set. Let me know if there are any other functions that should be added to that list.
FYI, I had commented my code with text that included the word "remains". This triggered a false positive in the test for forbidden function "rem". Perhaps it is possible to forbid instead "rem[spaces]("?
I suggest that "factor" should be (or should have been) banned.
I added factor to the banned list. [Though, the test suite includes very large numbers (as strings) that are beyond the storage limit of single or doubleprecision variables.]
Also, that sort of string with wildcards (for capturing rem) would work if regexp were used, but I use strfind in the test suite. That way, only the direct use of those exact strings will trigger that test case to fail.
Many solutions can be made that only check 0, 2, and 6 and fail on 4 and 8, thus not checking properly. The tests should include more unique ending digits, at least one of each 09.
Your test cases only ever end in 0, 1, 2, 3, 6, and 7 and many solutions give the wrong answer on 4, 5, 8, or 9.
It shows me wrong.
Solution Comments
Show commentsProblem Recent Solvers558
Suggested Problems

Find the alphabetic word product
3312 Solvers

Omit columns averages from a matrix
583 Solvers

1406 Solvers

Remove element(s) from cell array
1491 Solvers

Is this triangle rightangled?
5675 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!