Sagot :
Réponse :
Bonsoir,
Explications étape par étape
Je me doutais qu'il y aurait un problème pour le 2è carré magique
Il y a en réalité 26 solutions possibles !!!
1 ---- numéro de la solution
1 8 7 16 16 calcul de la somme, valeur donnée
9 4 2 15 15
6 5 3 14 14
-------------------
16 17 12
16 17 12
2
1 9 6 16 16
8 3 4 15 15
7 5 2 14 14
-------------------
16 17 12
16 17 12
3
1 9 6 16 16
8 5 2 15 15
7 3 4 14 14
-------------------
16 17 12
16 17 12
4
2 6 8 16 16
5 7 3 15 15
9 4 1 14 14
-------------------
16 17 12
16 17 12
5
3 4 9 16 16
6 8 1 15 15
7 5 2 14 14
-------------------
16 17 12
16 17 12
6
3 4 9 16 16
8 6 1 15 15
5 7 2 14 14
-------------------
16 17 12
16 17 12
7
3 6 7 16 16
5 9 1 15 15
8 2 4 14 14
-------------------
16 17 12
16 17 12
8
3 9 4 16 16
8 1 6 15 15
5 7 2 14 14
-------------------
16 17 12
16 17 12
9
3 9 4 16 16
8 6 1 15 15
5 2 7 14 14
-------------------
16 17 12
16 17 12
10
4 3 9 16 16
5 8 2 15 15
7 6 1 14 14
-------------------
16 17 12
16 17 12
11
4 3 9 16 16
7 6 2 15 15
5 8 1 14 14
-------------------
16 17 12
16 17 12
12
4 9 3 16 16
5 2 8 15 15
7 6 1 14 14
-------------------
16 17 12
16 17 12
13
5 7 4 16 16
8 1 6 15 15
3 9 2 14 14
-------------------
16 17 12
16 17 12
14
5 9 2 16 16
8 1 6 15 15
3 7 4 14 14
-------------------
16 17 12
16 17 12
15
6 3 7 16 16
2 9 4 15 15
8 5 1 14 14
-------------------
16 17 12
16 17 12
16
6 7 3 16 16
2 9 4 15 15
8 1 5 14 14
-------------------
16 17 12
16 17 12
17
6 7 3 16 16
9 2 4 15 15
1 8 5 14 14
-------------------
16 17 12
16 17 12
18
6 9 1 16 16
8 3 4 15 15
2 5 7 14 14
-------------------
16 17 12
16 17 12
19
7 3 6 16 16
8 5 2 15 15
1 9 4 14 14
-------------------
16 17 12
16 17 12
20
7 6 3 16 16
1 9 5 15 15
8 2 4 14 14
-------------------
16 17 12
16 17 12
21
7 6 3 16 16
5 2 8 15 15
4 9 1 14 14
-------------------
16 17 12
16 17 12
22
7 6 3 16 16
5 9 1 15 15
4 2 8 14 14
-------------------
16 17 12
16 17 12
23
7 6 3 16 16
8 2 5 15 15
1 9 4 14 14
-------------------
16 17 12
16 17 12
24
9 3 4 16 16
2 6 7 15 15
5 8 1 14 14
-------------------
16 17 12
16 17 12
25
9 6 1 16 16
5 3 7 15 15
2 8 4 14 14
-------------------
16 17 12
16 17 12
26
9 6 1 16 16
5 7 3 15 15
2 4 8 14 14
-------------------
16 17 12
16 17 12
DIM i1 AS INTEGER, i2 AS INTEGER, i3 AS INTEGER
DIM i4 AS INTEGER, i5 AS INTEGER, i6 AS INTEGER
DIM i7 AS INTEGER, i8 AS INTEGER, i9 AS INTEGER
DIM L1 AS INTEGER, L2 AS INTEGER, L3 AS INTEGER
DIM C1 AS INTEGER, C2 AS INTEGER, C3 AS INTEGER
L1 = 16: L2 = 15: L3 = 14
C1 = 16: C2 = 17: C3 = 12
OPEN "\nosdevoirs\mag_car.txt" FOR OUTPUT AS #1
FOR i1 = 1 TO 9
FOR i2 = 1 TO 9
IF i2 <> i1 THEN
FOR i3 = 1 TO 9
IF i3 <> i2 AND i3 <> i1 THEN
IF i1 + i2 + i3 = L1 THEN
FOR i4 = 1 TO 9
IF i4 <> i3 AND i4 <> i2 AND i4 <> i1 THEN
FOR i5 = 1 TO 9
IF i5 <> i4 AND i5 <> i3 AND i5 <> i2 AND i5 <> i1 THEN
FOR i6 = 1 TO 9
IF i6 <> i5 AND i6 <> i4 AND i6 <> i3 AND i6 <> i2 AND i6 <> i1 THEN
IF i4 + i5 + i6 = L2 THEN
FOR i7 = 1 TO 9
IF i7 <> i6 AND i7 <> i5 AND i7 <> i4 AND i7 <> i3 AND i7 <> i2 AND i7 <> i1 THEN
IF i1 + i4 + i7 = C1 THEN
FOR i8 = 1 TO 9
IF i8 <> i7 AND i8 <> i6 AND i8 <> i5 AND i8 <> i4 AND i8 <> i3 AND i8 <> i2 AND i8 <> i1 THEN
IF i2 + i5 + i8 = C2 THEN
i9 = 45 - i1 - i2 - i3 - i4 - i5 - i6 - i7 - i8
IF i9 > 0 AND i9 < 10 THEN
IF i9 <> i8 AND i9 <> i7 AND i9 <> i6 AND i9 <> i5 AND i9 <> i4 AND i9 <> i3 AND i9 <> i2 AND i9 <> i1 THEN
IF i3 + i6 + i9 = C3 THEN
k = k + 1
PRINT k
PRINT i1; i2; i3, i1 + i2 + i3; L1
PRINT i4; i5; i6, i4 + i5 + i6; L2
PRINT i7; i8; i9, i7 + i8 + i9; L3
PRINT "-------------------"
PRINT #1, k
PRINT #1, i1; i2; i3, i1 + i2 + i3; L1
PRINT #1, i4; i5; i6, i4 + i5 + i6; L2
PRINT #1, i7; i8; i9, i7 + i8 + i9; L3
PRINT #1, "-------------------"
PRINT #1, C1; C2; C3
PRINT #1, i1 + i4 + i7; i2 + i5 + i8; i3 + i6 + i9
END IF
END IF
END IF
END IF
END IF
NEXT i8
END IF
END IF
NEXT i7
END IF
END IF
NEXT i6
END IF
NEXT i5
END IF
NEXT i4
END IF
END IF
NEXT i3
END IF
NEXT i2
NEXT i1
CLOSE #1
END