Problem 42409. Divisible by 7
Pursuant to the first problem in this series, this one involves checking for divisibility by 7.
Write a function to determine if a number is divisible by 7. This can be done by a variety of methods. Some are:
 Multiply each digit in the candidate number (from right to left) by the digit in the corresponding position in this pattern: 1, 3, 2, 1, 3, 2 (1 applies to the ones digit, 3 to the tens digit, etc.). This pattern should be repeated beyond the hundredthousands digit. The resulting sum will be a smaller number. This method can be applied recursively to the absolute values of the resulting sum until a single digit results. Then, check that number for divisibility by seven.
 The previous method can also be used applying a slightly different pattern: 1, 3, 2, 6, 4, 5 (1 applies to the ones digit again, etc.). The absolute value of the resulting sum is not necessary as none of the factors are negative.
 Multiply the last (ones) digit by two and subtract the result from the remaining number. Apply recursively, as noted in methods above.
 Similar to the previous method, multiply the last digit by five and add to the remaining number. Apply recursively, as noted in methods above.
 Double the last digit and subtract it from the remaining number (original number except for the ones digit). Apply this recursively until a single digit results. If that number is divisible by seven, then so is the original number.
 Similar to the previous method, multiply the ones digit by five and add it to the remaining number. Apply this recursively until a single digit results. If that number is divisible by seven, then so is the original number.
 Along similar lines, take the last three digits of the number and subtract that number from the remaining number. Once you reach a number less than 1000, another method can be applied to further reduce the number to check for divisibility by seven.
 Etc. (there are others)
The restriction for multiplication has been lifted for this specific problem.
Previous problem: divisible by 6. Next problem: divisible by 8.
Solution Stats
Problem Comments

2 Comments
This one is dumb, as you need to use all the digits in the number. You might as well just use normal long division.
Plus, I have to implement modular arithmetic, which uses mod, which is banned in this problem.
i keep struggling with timeout issues or the truncation/rounding of large numbers to scientific notation
Solution Comments
Show commentsProblem Recent Solvers170
Suggested Problems

Similar Triangles  find the height of the tree
269 Solvers

Matrix indexing with two vectors of indices
700 Solvers

Determine the number of odd integers in a vector
649 Solvers

Calculate Amount of Cake Frosting
21145 Solvers

Find x rows where the sum of the numbers is the maximum
399 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!