Forbidden numbers: Difference between revisions

Added Sidef
(Added Sidef)
 
(2 intermediate revisions by 2 users not shown)
Line 670:
 
printf "First %s forbidden numbers:\n", num2en 50;
printfprint sprintf(('%4d')x50, @F[0..49]) =~ s/.{40}\K(?=.)/\n/gr;
print "\n\n";
 
Line 845:
2: 831
1: 8330
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">say ("First 50 terms: ", 50.by { .squares_r(3) == 0 })
 
for n in (500, 5_000, 50_000, 500_000) {
var v = (1..n -> count {|n|
idiv(n, ipow(4, n.valuation(4))).is_congruent(7, 8)
})
say "There are #{v} forbidden numbers up to #{n.commify}."
}</syntaxhighlight>
{{out}}
<pre>
First 50 terms: [7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, 79, 87, 92, 95, 103, 111, 112, 119, 124, 127, 135, 143, 151, 156, 159, 167, 175, 183, 188, 191, 199, 207, 215, 220, 223, 231, 239, 240, 247, 252, 255, 263, 271, 279, 284, 287, 295, 303, 311]
There are 82 forbidden numbers up to 500.
There are 831 forbidden numbers up to 5,000.
There are 8330 forbidden numbers up to 50,000.
There are 83331 forbidden numbers up to 500,000.
</pre>
 
Line 851 ⟶ 869:
{{libheader|Wren-fmt}}
This uses a sieve to filter out those numbers which are the sums of one, two or three squares. Works but very slow (c. 52 seconds).
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var forbidden = Fn.new { |limit, countOnly|
Line 906 ⟶ 924:
===Version 2===
This is a translation of the formula-based Python code in the OEIS link which at around 1.1 seconds is almost 50 times faster than Version 1 and is also about 3 times faster than the PARI code in that link.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isForbidden = Fn.new { |n|
2,747

edits