Cracking a 3‑Digit Logic Puzzle with a Simple Python Brute‑Force Script
The author tackles a elementary school three‑digit logic puzzle—determining a number that meets specific divisibility and digit‑sum conditions—by first reasoning through the constraints and then using a short Python brute‑force script, ultimately revealing the unique solution abc = 168.
Author introduces a weekend elementary math logic puzzle that caused him to lose 18 hairs.
The puzzle involves four statements (甲, 乙, 丙, 丁) about a three‑digit number abc: 甲 says abc is divisible by 8, 乙 says divisible by 9, 丙 says divisible by 7, 丁 says the sum of its digits is 15. Only one statement is false and abc is between 100 and 500.
He first reasons that if 丁 is false, the smallest abc satisfying the other three conditions is 504, which exceeds the range, so 丁 must be true and one of 甲, 乙, 丙 is false.
Using Python brute‑force, he enumerates all three‑digit numbers and checks the four conditions, printing which statement is false for each valid abc.
for a in range(1,5):
for b in range(0,9):
for c in range(0,9):
abc = a*100 + b*10 + c
T1 = abc % 8 == 0 # 甲
T2 = abc % 9 == 0 # 乙
T3 = abc % 7 == 0 # 丙
T4 = a + b + c == 15 # 丁
if T1 and T2 and T3:
print('丁说谎,abc=%s' % abc)
elif T1 and T2 and T4:
print('丙说谎,abc=%s' % abc)
elif T1 and T3 and T4:
print('乙说谎,abc=%s' % abc)
elif T2 and T3 and T4:
print('甲说谎,abc=%s' % abc)The script finds that the only solution satisfying the constraints is abc = 168, where 甲, 丙, 丁 are true and 乙 is false.
Thus the answer to the puzzle is 168.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
