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

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
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>
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).
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 71:
a.Count, sw.Elapsed.TotalMilliseconds);
}
}</langsyntaxhighlight>
{{out|Output @Tio.run}}
<pre>Numbers which are not the sum of distinct squares:
Line 80:
===Alternate Version===
A little quicker, seeks between squares.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 110:
Console.Write(sf, s.Last()-y.Last(),y.Last());
}
}</langsyntaxhighlight>
{{out|Output @Tio.run}}
<pre>Numbers which are not the sum of distinct squares:
Line 121:
{{libheader|Go-rcu}}
The quicker version and hence (indirectly) a translation of C#.
<langsyntaxhighlight lang="go">package main
 
import (
Line 183:
}
 
var r int</langsyntaxhighlight>
 
{{out}}
Line 204:
Since the implementation is based on generic concepts (such as runs), the
helper functions are potentially independently useful.
<langsyntaxhighlight lang="jq">#def squares: range(1; infinite) | . * .;
 
# sums of distinct squares (sods)
Line 256:
| [range(2; $ix+2|sq)] - $sods;
 
not_sum_of_distinct_squares</langsyntaxhighlight>
{{out}}
<pre>
Line 264:
=={{header|Julia}}==
A true proof of the sketch below would require formal mathematical induction.
<langsyntaxhighlight lang="julia">#=
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
Line 282:
 
println(possibles)
</langsyntaxhighlight>{{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]
Line 288:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[SumOfSquareable]
SumOfSquareable[n_Integer] := MemberQ[Total /@ Subsets[Range[1, Floor[Sqrt[n]]]^2, {1, \[Infinity]}], n]
notsummable = {};
Line 306:
{\[Infinity]}
]
notsummable</langsyntaxhighlight>
{{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>
Line 318:
are not reachably nor<br>
SumOfSquare-2, SumOfSquare-3,SumOfSquare-6,...SumOfSquare-108,SumOfSquare-112,SumOfSquare-128<br>
<langsyntaxhighlight lang="pascal">program practicalnumbers;
{$IFDEF Windows} {$APPTYPE CONSOLE} {$ENDIF}
var
Line 404:
{$IFNDEF UNIX} readln; {$ENDIF}
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 466:
'''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>
<syntaxhighlight lang="raku" perl6line>my @squares = ^∞ .map: *²; # Infinite series of squares
 
for 1..∞ -> $sq { # for every combination of all squares
Line 476:
}
put grep * ∉ @sums, 1..@run.tail and last if @run.elems > @squares[$sq];
}</langsyntaxhighlight>
{{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>
Line 485:
===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.
<langsyntaxhighlight lang="ecmascript">var squares = (1..18).map { |i| i * i }.toList
var combs = []
var results = []
Line 527:
 
System.print("Numbers which are not the sum of distinct squares:")
System.print(results)</langsyntaxhighlight>
 
{{out}}
Line 540:
{{libheader|Wren-fmt}}
Hugely quicker in fact - only 24 ms, the same as C# itself.
<langsyntaxhighlight lang="ecmascript">import "./math" for Nums
import "./fmt" for Fmt
 
Line 570:
System.print(a.join(", "))
Fmt.print(sf, i - g, g)
Fmt.print("Found $d total in $d ms.", a.count, ((System.clock - start)*1000).round)</langsyntaxhighlight>
 
{{out}}
10,327

edits