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
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 0-9.
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 Solvers569
Suggested Problems
-
2299 Solvers
-
615 Solvers
-
Make a random, non-repeating vector.
9787 Solvers
-
201 Solvers
-
2073 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!