Numbers which are not the sum of distinct squares: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Algol W)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 2 users not shown)
Line 36:
Recursive brute force (tempting though it is to use 9 nested loops... :) ), using the proof that if 129-324 can be expressed as the sum of distinct squares, then all integers greater than 128 can be so expressed - see the link in the Wren sample.
<syntaxhighlight lang="algol68">
BEGIN #CO find the integers that can't be expressed as the sum of distinct squares #
# it can be proved that if 120-324 can be expressed as the sum of distinct #
#squares then all integers greater than 129 can be so expressed (see the link in #
(see the link #in the Wren sample) so we need to check that 129-324 can be so expressed #
be so #expressed and find the numbers below 129 that can't be so expressed #
CO
BEGIN
INT max number = 324;
[ 0 : max number ]BOOL is sum; FOR i FROM LWB is sum TO UPB is sum DO is sum[ i ] := FALSE OD;
Line 480 ⟶ 482:
</pre>
 
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight lang=easylang>
maxNumber = 324
len isSum[] maxNumber
maxSquare = floor sqrt maxNumber
#
proc flagSum currSum sqPos . .
nextSum = currSum + sqPos * sqPos
if nextSum <= maxNumber
isSum[nextSum] = 1
for i = sqPos + 1 to maxSquare
flagSum nextSum i
.
.
.
for i = 1 to maxSquare
flagSum 0 i
.
for i = 1 to maxNumber
if isSum[i] = 0
write i & " "
.
.
</syntaxhighlight>
 
=={{header|Go}}==
Line 745 ⟶ 773:
then all integers greater than 129 can be so expressed (see the link in
the Wren sample) so we need to check that 129-324 can be so expressed
and endndfind the numbers below 129 that can't be so expressed
--]]
local maxNumber = 324
Line 1,190 ⟶ 1,218:
===Brute force===
This uses a brute force approach to generate the relevant numbers, similar to Julia, except using the same figures as the above proof. Still slow in Wren, around 20 seconds.
<syntaxhighlight lang="ecmascriptwren">var squares = (1..18).map { |i| i * i }.toList
var combs = []
var results = []
Line 1,245 ⟶ 1,273:
{{libheader|Wren-fmt}}
Hugely quicker in fact - only 24 ms, the same as C# itself.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Nums
import "./fmt" for Fmt
 
9,476

edits