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.