// The following Magma program implements a simple algorithm that // uses only integer arithmetic and tests all positive integers from // 1 through N in O(N) time. nMax:=10^6; // max value of n to which to search // Initialize 7 integer variables: n:=0; // n si:=0; // index of current square; S = si^2 ti:=0; // index of current triangular number; T = ti*(ti+1)/2 smc:=0; // S-n^3 (store this instead of actual cubes, squares) tmc:=0; // T-n^3 (store this instead of actual cubes, triangular nums) dsi:=0; // min amount to increase si when n is incremented by 1 dti:=0; // min amount to increase ti when n is incremented by 1 while n le nMax do n:=n+1; // test next integer n dn3:=3*n*(n-1)+1; // amount by which n^3 has been incremented si:=si+dsi; // update index of square smc:=smc-dn3+(2*si-dsi)*dsi; // update S-n^3 if smc le 0 then // S < n^3 + 1 si:=si+1; smc:=smc+2*si-1; if smc le 0 then // S < n^3 + 1 (still) si:=si+1; smc:=smc+2*si-1; dsi:=dsi+1; // si needed +1 twice, so increment dsi end if; end if; ti:=ti+dti; // update index of triangular number tmc:=tmc-dn3+((2*ti-dti+1)*dti) div 2; // update T-n^3 if tmc le 0 then // T < n^3 + 1 ti:=ti+1; tmc:=tmc+ti; if tmc le 0 then // T < n^3 + 1 (still) ti:=ti+1; tmc:=tmc+ti; dti:=dti+1; // ti needed +1 twice, so increment dti end if; end if; if (smc lt n) and (tmc lt n) then // S & T are in [n^3+1,n^3+n-1] n; // n is in A246042 end if; end while;