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

Content added Content deleted
m (syntax highlighting fixup automation)
Line 36: Line 36:
Following in the example set by the '''Free Pascal''' entry for this task, this C# code is re-purposed from [[Practical_numbers#C#]].<br>
Following in the example set by the '''Free Pascal''' entry for this task, this C# code is re-purposed from [[Practical_numbers#C#]].<br>
It seems that finding as many (or more) contiguous numbers-that-''are''-the-sum-of-distinct-squares as the highest found gap demonstrates that there is no higher gap, since there is enough overlap among the permutations of the sums of possible squares (once the numbers are large enough).
It seems that finding as many (or more) contiguous numbers-that-''are''-the-sum-of-distinct-squares as the highest found gap demonstrates that there is no higher gap, since there is enough overlap among the permutations of the sums of possible squares (once the numbers are large enough).
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 71: Line 71:
a.Count, sw.Elapsed.TotalMilliseconds);
a.Count, sw.Elapsed.TotalMilliseconds);
}
}
}</lang>
}</syntaxhighlight>
{{out|Output @Tio.run}}
{{out|Output @Tio.run}}
<pre>Numbers which are not the sum of distinct squares:
<pre>Numbers which are not the sum of distinct squares:
Line 80: Line 80:
===Alternate Version===
===Alternate Version===
A little quicker, seeks between squares.
A little quicker, seeks between squares.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 110: Line 110:
Console.Write(sf, s.Last()-y.Last(),y.Last());
Console.Write(sf, s.Last()-y.Last(),y.Last());
}
}
}</lang>
}</syntaxhighlight>
{{out|Output @Tio.run}}
{{out|Output @Tio.run}}
<pre>Numbers which are not the sum of distinct squares:
<pre>Numbers which are not the sum of distinct squares:
Line 121: Line 121:
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
The quicker version and hence (indirectly) a translation of C#.
The quicker version and hence (indirectly) a translation of C#.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 183: Line 183:
}
}


var r int</lang>
var r int</syntaxhighlight>


{{out}}
{{out}}
Line 204: Line 204:
Since the implementation is based on generic concepts (such as runs), the
Since the implementation is based on generic concepts (such as runs), the
helper functions are potentially independently useful.
helper functions are potentially independently useful.
<lang jq>#def squares: range(1; infinite) | . * .;
<syntaxhighlight lang="jq">#def squares: range(1; infinite) | . * .;


# sums of distinct squares (sods)
# sums of distinct squares (sods)
Line 256: Line 256:
| [range(2; $ix+2|sq)] - $sods;
| [range(2; $ix+2|sq)] - $sods;


not_sum_of_distinct_squares</lang>
not_sum_of_distinct_squares</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 264: Line 264:
=={{header|Julia}}==
=={{header|Julia}}==
A true proof of the sketch below would require formal mathematical induction.
A true proof of the sketch below would require formal mathematical induction.
<lang julia>#=
<syntaxhighlight lang="julia">#=
Here we show all the 128 < numbers < 400 can be expressed as a sum of distinct squares. Now
Here we show all the 128 < numbers < 400 can be expressed as a sum of distinct squares. Now
11 * 11 < 128 < 12 * 12. It is also true that we need no square less than 144 (12 * 12) to
11 * 11 < 128 < 12 * 12. It is also true that we need no square less than 144 (12 * 12) to
Line 282: Line 282:


println(possibles)
println(possibles)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
[2, 3, 6, 7, 8, 11, 12, 15, 18, 19, 22, 23, 24, 27, 28, 31, 32, 33, 43, 44, 47, 48, 60, 67, 72, 76, 92, 96, 108, 112, 128]
[2, 3, 6, 7, 8, 11, 12, 15, 18, 19, 22, 23, 24, 27, 28, 31, 32, 33, 43, 44, 47, 48, 60, 67, 72, 76, 92, 96, 108, 112, 128]
Line 288: Line 288:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[SumOfSquareable]
<syntaxhighlight lang="mathematica">ClearAll[SumOfSquareable]
SumOfSquareable[n_Integer] := MemberQ[Total /@ Subsets[Range[1, Floor[Sqrt[n]]]^2, {1, \[Infinity]}], n]
SumOfSquareable[n_Integer] := MemberQ[Total /@ Subsets[Range[1, Floor[Sqrt[n]]]^2, {1, \[Infinity]}], n]
notsummable = {};
notsummable = {};
Line 306: Line 306:
{\[Infinity]}
{\[Infinity]}
]
]
notsummable</lang>
notsummable</syntaxhighlight>
{{out}}
{{out}}
<pre>{2, 3, 6, 7, 8, 11, 12, 15, 18, 19, 22, 23, 24, 27, 28, 31, 32, 33, 43, 44, 47, 48, 60, 67, 72, 76, 92, 96, 108, 112, 128}</pre>
<pre>{2, 3, 6, 7, 8, 11, 12, 15, 18, 19, 22, 23, 24, 27, 28, 31, 32, 33, 43, 44, 47, 48, 60, 67, 72, 76, 92, 96, 108, 112, 128}</pre>
Line 318: Line 318:
are not reachably nor<br>
are not reachably nor<br>
SumOfSquare-2, SumOfSquare-3,SumOfSquare-6,...SumOfSquare-108,SumOfSquare-112,SumOfSquare-128<br>
SumOfSquare-2, SumOfSquare-3,SumOfSquare-6,...SumOfSquare-108,SumOfSquare-112,SumOfSquare-128<br>
<lang pascal>program practicalnumbers;
<syntaxhighlight lang="pascal">program practicalnumbers;
{$IFDEF Windows} {$APPTYPE CONSOLE} {$ENDIF}
{$IFDEF Windows} {$APPTYPE CONSOLE} {$ENDIF}
var
var
Line 404: Line 404:
{$IFNDEF UNIX} readln; {$ENDIF}
{$IFNDEF UNIX} readln; {$ENDIF}
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 432: Line 432:


Strictly speaking the termination test should probably be <code>if r and sq>r then</code>, not that shaving off two pointless iterations makes any difference at all.
Strictly speaking the termination test should probably be <code>if r and sq>r then</code>, not that shaving off two pointless iterations makes any difference at all.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">summable</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #004600;">true</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- (1 can be expressed as 1*1)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">summable</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #004600;">true</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- (1 can be expressed as 1*1)</span>
Line 455: Line 455:
<span style="color: #008080;">constant</span> <span style="color: #000000;">nwansods</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"numbers which are not the sum of distinct squares"</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">nwansods</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"numbers which are not the sum of distinct squares"</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">find_all</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">summable</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">nwansods</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">find_all</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">summable</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">nwansods</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 466: Line 466:
'''Spoiler:''' ''(highlight to read)''<br>
'''Spoiler:''' ''(highlight to read)''<br>
<span style="foreground-color:black;background-color:black;">Once the longest run of consecutive generated sums is longer the the next square, every number after can be generated by adding the next square to every number in the run. Find the ''new'' longest run, add the ''next'' square, etc.<span>
<span style="foreground-color:black;background-color:black;">Once the longest run of consecutive generated sums is longer the the next square, every number after can be generated by adding the next square to every number in the run. Find the ''new'' longest run, add the ''next'' square, etc.<span>
<lang perl6>my @squares = ^∞ .map: *²; # Infinite series of squares
<syntaxhighlight lang="raku" line>my @squares = ^∞ .map: *²; # Infinite series of squares


for 1..∞ -> $sq { # for every combination of all squares
for 1..∞ -> $sq { # for every combination of all squares
Line 476: Line 476:
}
}
put grep * ∉ @sums, 1..@run.tail and last if @run.elems > @squares[$sq];
put grep * ∉ @sums, 1..@run.tail and last if @run.elems > @squares[$sq];
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 6 7 8 11 12 15 18 19 22 23 24 27 28 31 32 33 43 44 47 48 60 67 72 76 92 96 108 112 128</pre>
<pre>2 3 6 7 8 11 12 15 18 19 22 23 24 27 28 31 32 33 43 44 47 48 60 67 72 76 92 96 108 112 128</pre>
Line 485: Line 485:
===Brute force===
===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.
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.
<lang ecmascript>var squares = (1..18).map { |i| i * i }.toList
<syntaxhighlight lang="ecmascript">var squares = (1..18).map { |i| i * i }.toList
var combs = []
var combs = []
var results = []
var results = []
Line 527: Line 527:


System.print("Numbers which are not the sum of distinct squares:")
System.print("Numbers which are not the sum of distinct squares:")
System.print(results)</lang>
System.print(results)</syntaxhighlight>


{{out}}
{{out}}
Line 540: Line 540:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
Hugely quicker in fact - only 24 ms, the same as C# itself.
Hugely quicker in fact - only 24 ms, the same as C# itself.
<lang ecmascript>import "./math" for Nums
<syntaxhighlight lang="ecmascript">import "./math" for Nums
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 570: Line 570:
System.print(a.join(", "))
System.print(a.join(", "))
Fmt.print(sf, i - g, g)
Fmt.print(sf, i - g, g)
Fmt.print("Found $d total in $d ms.", a.count, ((System.clock - start)*1000).round)</lang>
Fmt.print("Found $d total in $d ms.", a.count, ((System.clock - start)*1000).round)</syntaxhighlight>


{{out}}
{{out}}