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 hundred-thousands 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
-
4 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
Correct me if I am wrong but shouldn't test case 8 be false. I just checked and got the result rem(test_case_8,7)==1.
@Thai Nguyen the number in test case 8 is indeed divisible by seven. However, it cannot be represented exactly as a float, and rem(..., 7) does not return the correct result. Try using the Symbolic Math Toolbox if you have access to that.
Solution Comments
Show commentsProblem Recent Solvers174
Suggested Problems
-
282 Solvers
-
Sum of odd numbers in a matrix
574 Solvers
-
1642 Solvers
-
834 Solvers
-
Given a square and a circle, please decide whether the square covers more area.
957 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!