The input 7|12|1|14|2|13|8|11|16|3|10|5|9|6|15|4
returns 0 by the PHP -code, while 1 by Python code: 1 means that the sums of the 4x4 magic square are the same, while 0 means the reverse. Python code is correct.
The problem of the PHP code seems to be in the function divide's for -loop, since PHP gives too many sums.
How does the logic of PHP code differ from Python's?
PHP
$data = "7|12|1|14|2|13|8|11|16|3|10|5|9|6|15|4";
$array = explode("|", $data);
# Calculate the unique sums of the four figures
# @return array int
function divide ( $array ) {
$sum = array_map('array_sum', array_chunk($array, 4));
$apu_a = array();
for ( $i = 0; $i < count( $sum ); $i++ ) {
if ( $i % 5 == 0 )
$apu_a []= $array[$i];
}
$sum []= array_sum( $apu_a );
$apu_a = array();
for ( $i = 0; $i < count( $sum ); $i++ ) {
if ( $i % 3 == 0 and $i != 15 and $i != 0 )
$apu_a []= $array[$i];
}
$sum []= array_sum( $apu_a );
$result = array_unique($sum);
return $result;
}
Python
data = "7|12|1|14|2|13|8|11|16|3|10|5|9|6|15|4"
lista = map(int,data.split("|"))
def divide( lista ):
summat = [sum(lista[i:i+4]) for i in range(0,len(lista),4)]
summat += [sum(lista[0::5]) for i in range(0, len(lista), 16)]
summat += [sum(a for i,a in enumerate(lista) if i %3==0 and i != 15 and i != 0)]
return set(summat)