ปัญหา AFBF + CGHB + DAFG +AEAB = BCDC

สืบเนื่องจากกระทู้พันทิพเรื่องการบ้านเด็กป.4 https://pantip.com/topic/39141834 ที่ถามว่าถ้า A-H เป็นตัวเลขโดดๆไม่ซ้ำกันและ AFBF + CGHB + DAFG +AEAB = BCDC ให้หาค่าว่า A-H มีค่าอะไรบ้าง ถ้าเราไม่อยากคิดด้วยสมองเราเองเราก็อาจสั่งให้คอมพิวเตอร์ไปนั่งไล่ค่าที่เป็นไปได้ของ A,B,C,...,H ดูว่าค่าแบบไหนทำให้สมการเป็นจริงบ้าง มีวิธีอื่นๆที่สามารถทำงานได้เร็วกว่าแต่เด็กๆม.ต้นยังไม่มีพื้นฐานความรู้ของวิธีเหล่านั้น เราจึงใช้วิธีตรงไปตรงมาที่สุดที่พอจะนั่งรอได้ก่อน เด็กๆหัดใช้ list comprehension และ for loop

In [1]:
import time

def mkn(a,b,c,d):
    """
    เอาตัวเลขโดด a, b, c, d มาทำเป็นจำนวน abcd
    """
    return 1000*a+100*b+10*c+d

d = range(0,10)

begin = time.time()
ans = [(A,B,C,D,E,F,G,H) \
       for A in d for B in d for C in d \
       for D in d for E in d for F in d \
       for G in d for H in d \
       if len(set((A,B,C,D,E,F,G,H))) == len((A,B,C,D,E,F,G,H))\
       and mkn(A,F,B,F) + mkn(C,G,H,B) + mkn(D,A,F,G) + mkn(A,E,A,B) == mkn(B,C,D,C)]
end = time.time()

print(f"Done in {end-begin:.1f} seconds")
print(f"There are {len(ans)} answers\n")

print("Solutions of AFBF + CGHB + DAFG +AEAB = BCDC:\n")
for a in ans:
    (A,B,C,D,E,F,G,H) = a
    print(f"(A, B, C, D, E, F, G, H) is {a}")
    print(f"{mkn(A,F,B,F)} + {mkn(C,G,H,B)} + {mkn(D,A,F,G)} + {mkn(A,E,A,B)} = {mkn(B,C,D,C)}")
    print(f"{mkn(A,F,B,F) + mkn(C,G,H,B) + mkn(D,A,F,G) + mkn(A,E,A,B)} == {mkn(B,C,D,C)}")
Done in 38.5 seconds
There are 18 answers

Solutions of AFBF + CGHB + DAFG +AEAB = BCDC:

(A, B, C, D, E, F, G, H) is (0, 7, 1, 4, 2, 8, 9, 6)
878 + 1967 + 4089 + 207 = 7141
7141 == 7141
(A, B, C, D, E, F, G, H) is (0, 7, 1, 4, 2, 9, 8, 5)
979 + 1857 + 4098 + 207 = 7141
7141 == 7141
(A, B, C, D, E, F, G, H) is (0, 7, 5, 1, 2, 3, 8, 9)
373 + 5897 + 1038 + 207 = 7515
7515 == 7515
(A, B, C, D, E, F, G, H) is (0, 7, 5, 1, 2, 8, 3, 4)
878 + 5347 + 1083 + 207 = 7515
7515 == 7515
(A, B, C, D, E, F, G, H) is (0, 8, 1, 6, 5, 2, 3, 4)
282 + 1348 + 6023 + 508 = 8161
8161 == 8161
(A, B, C, D, E, F, G, H) is (0, 8, 4, 3, 5, 1, 7, 2)
181 + 4728 + 3017 + 508 = 8434
8434 == 8434
(A, B, C, D, E, F, G, H) is (0, 8, 4, 3, 5, 2, 6, 1)
282 + 4618 + 3026 + 508 = 8434
8434 == 8434
(A, B, C, D, E, F, G, H) is (0, 8, 5, 2, 4, 3, 6, 9)
383 + 5698 + 2036 + 408 = 8525
8525 == 8525
(A, B, C, D, E, F, G, H) is (0, 9, 2, 6, 7, 1, 3, 4)
191 + 2349 + 6013 + 709 = 9262
9262 == 9262
(A, B, C, D, E, F, G, H) is (0, 9, 3, 4, 6, 7, 8, 5)
797 + 3859 + 4078 + 609 = 9343
9343 == 9343
(A, B, C, D, E, F, G, H) is (1, 6, 0, 2, 9, 5, 3, 8)
1565 + 386 + 2153 + 1916 = 6020
6020 == 6020
(A, B, C, D, E, F, G, H) is (1, 6, 2, 0, 9, 3, 7, 8)
1363 + 2786 + 137 + 1916 = 6202
6202 == 6202
(A, B, C, D, E, F, G, H) is (1, 6, 2, 0, 9, 7, 3, 4)
1767 + 2346 + 173 + 1916 = 6202
6202 == 6202
(A, B, C, D, E, F, G, H) is (1, 8, 0, 4, 3, 5, 9, 7)
1585 + 978 + 4159 + 1318 = 8040
8040 == 8040
(A, B, C, D, E, F, G, H) is (1, 9, 2, 3, 5, 6, 8, 4)
1696 + 2849 + 3168 + 1519 = 9232
9232 == 9232
(A, B, C, D, E, F, G, H) is (2, 9, 3, 1, 4, 0, 5, 8)
2090 + 3589 + 1205 + 2429 = 9313
9313 == 9313
(A, B, C, D, E, F, G, H) is (3, 9, 1, 0, 2, 7, 6, 8)
3797 + 1689 + 376 + 3239 = 9101
9101 == 9101
(A, B, C, D, E, F, G, H) is (3, 9, 1, 0, 2, 8, 5, 7)
3898 + 1579 + 385 + 3239 = 9101
9101 == 9101
In [2]:
# โจทย์ AFBF + CGHB + DAFG +AEAB = BCDC
# อาจใช้เวลานานหลายนาที

import time

def mkn(a,b,c,d):
    """
    เอาตัวเลขโดด a, b, c, d มาทำเป็นจำนวน abcd
    """
    return 1000*a+100*b+10*c+d

d = range(0,10)

ans = []
begin = time.time()
for A in range(0,10):
    for B in range(0,10):
        for C in range(0,10):
            for D in range(0,10):
                for E in range(0,10):
                    for F in range(0,10):
                        for G in range(0,10):
                            for H in range(0,10):
                                if len(set((A,B,C,D,E,F,G,H))) == len((A,B,C,D,E,F,G,H))\
                                and mkn(A,F,B,F) + mkn(C,G,H,B) + mkn(D,A,F,G) + mkn(A,E,A,B)\
                                == mkn(B,C,D,C):
                                    ans.append((A,B,C,D,E,F,G,H))
end = time.time()

print(f"Done in {end-begin:.1f} seconds")
print(f"There are {len(ans)} answers\n")

print("Solutions of AFBF + CGHB + DAFG +AEAB = BCDC:\n")
for a in ans:
    (A,B,C,D,E,F,G,H) = a
    print(f"(A, B, C, D, E, F, G, H) is {a}")
    print(f"{mkn(A,F,B,F)} + {mkn(C,G,H,B)} + {mkn(D,A,F,G)} + {mkn(A,E,A,B)} = {mkn(B,C,D,C)}")
    print(f"{mkn(A,F,B,F) + mkn(C,G,H,B) + mkn(D,A,F,G) + mkn(A,E,A,B)} == {mkn(B,C,D,C)}")
    #print()
Done in 61.5 seconds
There are 18 answers

Solutions of AFBF + CGHB + DAFG +AEAB = BCDC:

(A, B, C, D, E, F, G, H) is (0, 7, 1, 4, 2, 8, 9, 6)
878 + 1967 + 4089 + 207 = 7141
7141 == 7141
(A, B, C, D, E, F, G, H) is (0, 7, 1, 4, 2, 9, 8, 5)
979 + 1857 + 4098 + 207 = 7141
7141 == 7141
(A, B, C, D, E, F, G, H) is (0, 7, 5, 1, 2, 3, 8, 9)
373 + 5897 + 1038 + 207 = 7515
7515 == 7515
(A, B, C, D, E, F, G, H) is (0, 7, 5, 1, 2, 8, 3, 4)
878 + 5347 + 1083 + 207 = 7515
7515 == 7515
(A, B, C, D, E, F, G, H) is (0, 8, 1, 6, 5, 2, 3, 4)
282 + 1348 + 6023 + 508 = 8161
8161 == 8161
(A, B, C, D, E, F, G, H) is (0, 8, 4, 3, 5, 1, 7, 2)
181 + 4728 + 3017 + 508 = 8434
8434 == 8434
(A, B, C, D, E, F, G, H) is (0, 8, 4, 3, 5, 2, 6, 1)
282 + 4618 + 3026 + 508 = 8434
8434 == 8434
(A, B, C, D, E, F, G, H) is (0, 8, 5, 2, 4, 3, 6, 9)
383 + 5698 + 2036 + 408 = 8525
8525 == 8525
(A, B, C, D, E, F, G, H) is (0, 9, 2, 6, 7, 1, 3, 4)
191 + 2349 + 6013 + 709 = 9262
9262 == 9262
(A, B, C, D, E, F, G, H) is (0, 9, 3, 4, 6, 7, 8, 5)
797 + 3859 + 4078 + 609 = 9343
9343 == 9343
(A, B, C, D, E, F, G, H) is (1, 6, 0, 2, 9, 5, 3, 8)
1565 + 386 + 2153 + 1916 = 6020
6020 == 6020
(A, B, C, D, E, F, G, H) is (1, 6, 2, 0, 9, 3, 7, 8)
1363 + 2786 + 137 + 1916 = 6202
6202 == 6202
(A, B, C, D, E, F, G, H) is (1, 6, 2, 0, 9, 7, 3, 4)
1767 + 2346 + 173 + 1916 = 6202
6202 == 6202
(A, B, C, D, E, F, G, H) is (1, 8, 0, 4, 3, 5, 9, 7)
1585 + 978 + 4159 + 1318 = 8040
8040 == 8040
(A, B, C, D, E, F, G, H) is (1, 9, 2, 3, 5, 6, 8, 4)
1696 + 2849 + 3168 + 1519 = 9232
9232 == 9232
(A, B, C, D, E, F, G, H) is (2, 9, 3, 1, 4, 0, 5, 8)
2090 + 3589 + 1205 + 2429 = 9313
9313 == 9313
(A, B, C, D, E, F, G, H) is (3, 9, 1, 0, 2, 7, 6, 8)
3797 + 1689 + 376 + 3239 = 9101
9101 == 9101
(A, B, C, D, E, F, G, H) is (3, 9, 1, 0, 2, 8, 5, 7)
3898 + 1579 + 385 + 3239 = 9101
9101 == 9101