Problem 42405. Divisible by 3
Pursuant to the first problem in this series, this one involves checking for divisibility by 3.
Write a function to determine if a number is divisible by 3. This can be checked by at least two different methods:
- Sum the digits in the number. If the resulting sum is divisible by 3, then so is the original number. This routine can be applied recursively until the resulting sum is a single digit.
- From the wikipedia reference page: "Subtract the quantity of the digits 2, 5, and 8 in the number from the quantity of the digits 1, 4, and 7 in the number." If the resulting sum (absolute value) is a multiple of 3, then so is the original number.
Previous problem: divisible by 2. Next problem: divisible by 4.
Solution Stats
Problem Comments
-
7 Comments
Also, building a general "divisible by n" routine is a good idea and I had thought about posting that at the end of the group of problems. I just think that it's too big for a standalone problem. There are many problems on Cody that are extremely simple and require no thought (like the recent string of beginner problems). There are also quite a few that require significant time, thought, and/or programming to solve, but they are all worth the same number of points. I prefer to break more difficult problems into simpler modules (just like is done in programming) to provide an encouraging approach, especially to those not already expert in Matlab. There are similarities between many of the problems in this set, but some numbers require quite different methods, such as 7 and 11.
Solved problems before reading comments and must agree with Alfonso. "divisible by N" approach worked nicely through all of them.
Thanks for the comment. I'll be adding a few more problems to this set in the near future to round out and finish up the "divisible by N" approach.
Solution Comments
Show commentsProblem Recent Solvers402
Suggested Problems
-
Arrange Vector in descending order
11821 Solvers
-
Convert a numerical matrix into a cell array of strings
2061 Solvers
-
1305 Solvers
-
Matlab Basics - y as a function of x
503 Solvers
-
5236 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!