求能被7整除但不含数字7的三位数个数的高效解法问询
Hey Angeline! I totally get it—going through every single three-digit number to check those two conditions sounds like a huge time-suck. Let me break down a much more efficient, math-based approach that avoids brute-force checking:
Step 1: Calculate total three-digit numbers divisible by 7
First, find the smallest three-digit number divisible by 7: that's 105 (since 7×15=105). The largest is 994 (7×142=994). To get the total count, subtract the multipliers and add 1 (since we’re including both endpoints):142 - 15 + 1 = 128
So there are 128 three-digit numbers divisible by 7 in total.
Step 2: Subtract numbers that are divisible by 7 and contain the digit 7
We’ll split this into three mutually exclusive cases (so no double-counting) using basic number theory:
Case 1: Hundreds digit is 7
These are numbers from 700 to 799 divisible by 7. The smallest is 700 (7×100) and largest is 798 (7×114). Count:114 - 100 + 1 = 15Case 2: Tens digit is 7, hundreds digit is NOT 7
Let the number beA7B(A ≠7, B is 0-9). This number equals100A +70 + B, which can be rewritten as7×(14A+10) + 2A + B. For the whole number to be divisible by 7,2A + Bmust be divisible by 7. Testing valid values of A (1-6,8-9), we find 8 valid numbers: 175,273,371,476,574,672,875,973.Case 3: Units digit is 7, hundreds and tens digits are NOT 7
Let the number beAB7(A≠7, B≠7). This equals100A +10B +7, or7×(14A+B+1) + 2A +3B. For divisibility by 7,2A+3Bmust be divisible by 7. Testing valid A and B values, we get 8 valid numbers:147,217,357,427,567,637,847,917.
Adding these up: 15 +8 +8 =31 total numbers that are divisible by 7 but contain the digit 7.
Step3: Get the final count
Subtract the invalid numbers from the total:128 -31 =97
If you want a quick sanity check, you can run a simple brute-force script to verify:
valid_count = 0 for num in range(100, 1000): if num %7 ==0 and '7' not in str(num): valid_count +=1 print(valid_count)
This will output 97, matching our math-based result.
备注:内容来源于stack exchange,提问作者Angeline0320

