8
Well, let’s start at the beginning, at the beginning, at the beginning.
I have this program:
from itertools import combinations
lis = [1,2,3,4,5,6,7,8,9]
for i in (3, len(lis)):
for comb in combinations(lis, i):
if sum(comb) == 10:
print (comb,'= 10')
His function is nothing more than: Adding a specific amount of numbers within a list, so that you get the established result.
Suppose the list has 9 numbers, from 1 to 9. lis = [1,2,3,4,5,6,7,8,9]
And I want the number of numbers added together to be 3. for i in (3, len(lis)):
And I also determined a result, in this case, number 10. if sum(comb) == 10:
Obviously the output of the program will be this:
(1, 2, 7) = 10
(1, 3, 6) = 10
(1, 4, 5) = 10
(2, 3, 5) = 10
Run the program and see for yourself:
https://repl.it/@William33/RecentHeftyTern-10
Briefly, succinctly and synthetically, it is the sum of all possible combinations of a certain amount of numbers within the list that will give the proposed result.
Now, knowing what this is about, we can continue with the application of this knowledge.
Guess where it’ll be used?
That’s right, in a magic square!
For those who still don’t know what a magic square is, here’s the explanation:
Magic Square is a square table equal to the intersection of numbers where the sum of each column, each row and the two diagonals are equal.
Take the example:
Continuing:
Look now that interesting, to apply in the program I "pulled out" the square diagonals.
Now, you’re seeing "these" results?(107,113,119,150,131,137,143,149,141,133,150,117,109,101)
I’ll apply in the program with the amount "5"
the matrix numbers to sum all possible combinations that will give "these" results, with the exception of the 150 that will be with the quantity "6"
.
Run the program and, don’t worry, it will take less than 30 seconds to find all the results:
https://repl.it/@William33/RecentHeftyTern-11
Now finally, the big question!
How to make all possible combinations of the sums of all these results obtained in this program, regroup so that the matrix is exactly like this?
The program would print all the possibilities that arrive at these results with the matrix exactly this way, with the numbers within the matrix and their results around.
Updated program:
https://repl.it/@William33/RecentHeftyTern-16
What needs to be done in it is for these "results" to repeat the same thing that the "Combinations" function did to find these "results" but this time adding 7 out of 7 and the
if sum(Comb) must be equal to the result_final..
for comb4 in combinations(lista, 7):
if sum(comb4) == resultado_final[1]:
if it is he will print a matrix like this:
MATRIX
#....[47][16][41][10][35]....
#[05]....[48][17][42]....[29]
#[30][06]....[49]....[36][12]
#[13][31][07]....[43][19][37]
#[38][14]....[01]....[44][20]
#[21]....[08][33][02]....[45]
#....[15][40][09][34][03]....
and all the possibilities of this matrix.. that give the same results...
FINAL RESULT
#[107][113][119][150][131][137][143]
#...................................[149]
#...................................[141]
#...................................[133]
#...................................[150]
#...................................[117]
#...................................[109]
#...................................[101]
You will manually enter the values in red and want the program to generate the matrix?
– Woss
Yes kind of like this... I enter the values in red.. but the matrix will have to have a check to see if they hit all the red results.. then she prints out all the matrices that match those results...
– William
Always leaving the diagonals blank?
– Woss
Yes. Always in white..
– William
@Andersoncarloswoss ?
– William
Curiosity: what would be the purpose of this?
– hdiogenes
You want to check all possible results, or have a result that satisfies the proposed problem?
– user89389
What is the purpose? What is your doubt?
– de_python