<?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));
You have javascript disabled. You will not be able to edit any code.