3v4l.org

run code in 300+ PHP versions simultaneously
<?php function decompose($value){ $value *= $value; $dp = array_fill(0,$value + 1,-1); $dp[1] = 1; for($i = 2; $i <= $value; ++$i){ for($j = intval( sqrt($i) ); $j >= 1; --$j){ if($dp[$i - $j * $j] != -1 && $dp[$i - $j * $j] < $j){ // meaning can be represented as sum of squares $dp[$i] = $j; break; // since we are looking for sequence with greater numbers } } } $result = []; $curr = $value; while($dp[$curr] != -1){ $result[] = $dp[$curr]; $curr -= $dp[$curr] * $dp[$curr]; } return $result; } print_r(decompose(11));

preferences:
33.55 ms | 406 KiB | 5 Q