Untouchable numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|F_Sharp|F#}}: fixed duplicate <lang fsharp>)
m (syntax highlighting fixup automation)
Line 72: Line 72:
Note that under Windows (and possibly under Linux), Algol 68G requires that the heap size be increased in order to allow arrays big enough to handle 100 000 and 1 000 000 untouchable numbers. See [[ALGOL_68_Genie#Using_a_Large_Heap]].
Note that under Windows (and possibly under Linux), Algol 68G requires that the heap size be increased in order to allow arrays big enough to handle 100 000 and 1 000 000 untouchable numbers. See [[ALGOL_68_Genie#Using_a_Large_Heap]].
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find some untouchable numbers - numbers not equal to the sum of the #
<syntaxhighlight lang="algol68">BEGIN # find some untouchable numbers - numbers not equal to the sum of the #
# proper divisors of any +ve integer #
# proper divisors of any +ve integer #
INT max untouchable = 1 000 000;
INT max untouchable = 1 000 000;
Line 157: Line 157:
# show the counts of untouchable numbers #
# show the counts of untouchable numbers #
show untouchable statistics
show untouchable statistics
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 186: Line 186:
This solution implements [[Talk:Untouchable_numbers#Nice_recursive_solution]]
This solution implements [[Talk:Untouchable_numbers#Nice_recursive_solution]]
===The Function===
===The Function===
<lang cpp>
<syntaxhighlight lang="cpp">
// Untouchable Numbers : Nigel Galloway - March 4th., 2021;
// Untouchable Numbers : Nigel Galloway - March 4th., 2021;
#include <functional>
#include <functional>
Line 212: Line 212:
}
}
};
};
</syntaxhighlight>
</lang>
===The Task===
===The Task===
;Less than 2000
;Less than 2000
<lang cpp>
<syntaxhighlight lang="cpp">
int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int c{0}; auto n{uT{2000}}; for(auto g=n.nxt(0); g; g=n.nxt(*g+1)){if(c++==30){c=1; printf("\n");} printf("%4d ",*g);} printf("\n");
int c{0}; auto n{uT{2000}}; for(auto g=n.nxt(0); g; g=n.nxt(*g+1)){if(c++==30){c=1; printf("\n");} printf("%4d ",*g);} printf("\n");
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 231: Line 231:
</pre>
</pre>
;Count less than or equal 100000
;Count less than or equal 100000
<lang cpp>
<syntaxhighlight lang="cpp">
int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int z{100000}; auto n{uT{z}}; cout<<"untouchables below "<<z<<"->"<<n.count()<<endl;
int z{100000}; auto n{uT{z}}; cout<<"untouchables below "<<z<<"->"<<n.count()<<endl;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 242: Line 242:
</pre>
</pre>
;Count less than or equal 1000000
;Count less than or equal 1000000
<lang cpp>
<syntaxhighlight lang="cpp">
int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int z{1000000}; auto n{uT{z}}; cout<<"untouchables below "<<z<<"->"<<n.count()<<endl;
int z{1000000}; auto n{uT{z}}; cout<<"untouchables below "<<z<<"->"<<n.count()<<endl;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 253: Line 253:
</pre>
</pre>
;Count less than or equal 2000000
;Count less than or equal 2000000
<lang cpp>
<syntaxhighlight lang="cpp">
int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int z{2000000}; auto n{uT{z}}; cout<<"untouchables below "<<z<<"->"<<n.count()<<endl;
int z{2000000}; auto n{uT{z}}; cout<<"untouchables below "<<z<<"->"<<n.count()<<endl;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 270: Line 270:
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
{{Trans|Go}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Untouchable_numbers;
program Untouchable_numbers;


Line 393: Line 393:
writeln(cu:7, ' untouchable numbers were found <= ', cl);
writeln(cu:7, ' untouchable numbers were found <= ', cl);
readln;
readln;
end.</lang>
end.</syntaxhighlight>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 399: Line 399:
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
It implements [[Talk:Untouchable_numbers#Nice_recursive_solution]]
It implements [[Talk:Untouchable_numbers#Nice_recursive_solution]]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Applied dendrology. Nigel Galloway: February 15., 2021
// Applied dendrology. Nigel Galloway: February 15., 2021
let uT a=let N,G=Array.create(a+1) true, [|yield! primes64()|>Seq.takeWhile((>)(int64 a))|]
let uT a=let N,G=Array.create(a+1) true, [|yield! primes64()|>Seq.takeWhile((>)(int64 a))|]
Line 409: Line 409:
|_->N.[0]<-false; N
|_->N.[0]<-false; N
fL (fG 1L 0L 0) [fN 1L 0L 1]
fL (fG 1L 0L 0) [fN 1L 0L 1]
</syntaxhighlight>
</lang>
===The Task===
===The Task===
;Less than 2000
;Less than 2000
<lang fsharp>
<syntaxhighlight lang="fsharp">
uT 2000|>Array.mapi(fun n g->(n,g))|>Array.filter(fun(_,n)->n)|>Array.chunkBySize 30|>Array.iter(fun n->n|>Array.iter(fst>>printf "%5d");printfn "")
uT 2000|>Array.mapi(fun n g->(n,g))|>Array.filter(fun(_,n)->n)|>Array.chunkBySize 30|>Array.iter(fun n->n|>Array.iter(fst>>printf "%5d");printfn "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 426: Line 426:
</pre>
</pre>
;Count less than or equal 100000
;Count less than or equal 100000
<lang fsharp>
<syntaxhighlight lang="fsharp">
printfn "%d" (uT 100000|>Array.filter id|>Array.length)
printfn "%d" (uT 100000|>Array.filter id|>Array.length)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 435: Line 435:
</pre>
</pre>
;Count less than or equal 1000000
;Count less than or equal 1000000
<lang fsharp>
<syntaxhighlight lang="fsharp">
printfn "%d" (uT 1000000|>Array.filter id|>Array.length)
printfn "%d" (uT 1000000|>Array.filter id|>Array.length)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 444: Line 444:
</pre>
</pre>
;Count less than or equal 2000000
;Count less than or equal 2000000
<lang fsharp>
<syntaxhighlight lang="fsharp">
printfn "%d" (uT 2000000|>Array.filter id|>Array.length)
printfn "%d" (uT 2000000|>Array.filter id|>Array.length)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 453: Line 453:
</pre>
</pre>
;Count less than or equal 3000000
;Count less than or equal 3000000
<lang fsharp>
<syntaxhighlight lang="fsharp">
printfn "%d" (uT 3000000|>Array.filter id|>Array.length)
printfn "%d" (uT 3000000|>Array.filter id|>Array.length)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 464: Line 464:
=={{header|Go}}==
=={{header|Go}}==
This was originally based on the Wren example but has been modified somewhat to find untouchable numbers up to 1 million rather than 100,000 in a reasonable time. On my machine, the former (with a sieve factor of 63) took 31 minutes 9 seconds and the latter (with a sieve factor of 14) took 6.2 seconds.
This was originally based on the Wren example but has been modified somewhat to find untouchable numbers up to 1 million rather than 100,000 in a reasonable time. On my machine, the former (with a sieve factor of 63) took 31 minutes 9 seconds and the latter (with a sieve factor of 14) took 6.2 seconds.
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 576: Line 576:
cl := commatize(limit)
cl := commatize(limit)
fmt.Printf("%7s untouchable numbers were found <= %s\n", cu, cl)
fmt.Printf("%7s untouchable numbers were found <= %s\n", cu, cl)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 612: Line 612:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
factor=: 3 : 0 NB. explicit
factor=: 3 : 0 NB. explicit
'primes powers'=. __&q: y
'primes powers'=. __&q: y
Line 628: Line 628:
candidates=: 5 , [: +: [: #\@i. >.@-: NB. within considered range, all but one candidate are even.
candidates=: 5 , [: +: [: #\@i. >.@-: NB. within considered range, all but one candidate are even.
spds=:([:sum_of_proper_divisors"0(#\@i.-.i.&.:(p:inv))@*:)f. NB. remove primes which contribute 1
spds=:([:sum_of_proper_divisors"0(#\@i.-.i.&.:(p:inv))@*:)f. NB. remove primes which contribute 1
</syntaxhighlight>
</lang>
We may revisit to correct the larger untouchable tallies. The straightforward algorithm presented is a little bit efficient, and, I claim, the verb <tt>(candidates-.spds)</tt> produces the full list of untouchable numbers up to its argument. It considers the sum of proper divisors through the argument squared, less primes. Since J is an algorithm description language, it may be "fairer" to state in J that "more resources required" than in some other language. [https://www.eecg.utoronto.ca/~jzhu/csc326/readings/iverson.pdf]
We may revisit to correct the larger untouchable tallies. The straightforward algorithm presented is a little bit efficient, and, I claim, the verb <tt>(candidates-.spds)</tt> produces the full list of untouchable numbers up to its argument. It considers the sum of proper divisors through the argument squared, less primes. Since J is an algorithm description language, it may be "fairer" to state in J that "more resources required" than in some other language. [https://www.eecg.utoronto.ca/~jzhu/csc326/readings/iverson.pdf]


Line 682: Line 682:
but the 512,000,000 sieved below is just from doubling 1,000,000 and running the sieve until we get 150232 for the number
but the 512,000,000 sieved below is just from doubling 1,000,000 and running the sieve until we get 150232 for the number
of untouchables under 1,000,000.
of untouchables under 1,000,000.
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function properfactorsum(n)
function properfactorsum(n)
Line 715: Line 715:
println("The count of untouchable numbers ≤ $N is: ", count(x -> untouchables[x], 1:N))
println("The count of untouchable numbers ≤ $N is: ", count(x -> untouchables[x], 1:N))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The untouchable numbers ≤ 2000 are:
The untouchable numbers ≤ 2000 are:
Line 748: Line 748:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>f = DivisorSigma[1, #] - # &;
<syntaxhighlight lang="mathematica">f = DivisorSigma[1, #] - # &;
limit = 10^5;
limit = 10^5;
c = Not /@ PrimeQ[Range[limit]];
c = Not /@ PrimeQ[Range[limit]];
Line 779: Line 779:
Count[untouchable, _?(LessEqualThan[1000])]
Count[untouchable, _?(LessEqualThan[1000])]
Count[untouchable, _?(LessEqualThan[10000])]
Count[untouchable, _?(LessEqualThan[10000])]
Count[untouchable, _?(LessEqualThan[100000])]</lang>
Count[untouchable, _?(LessEqualThan[100000])]</syntaxhighlight>
{{out}}
{{out}}
<pre>2 246 342 540 714 804 964 1102 1212 1316 1420 1596 1774 1884
<pre>2 246 342 540 714 804 964 1102 1212 1316 1420 1596 1774 1884
Line 804: Line 804:
=={{header|Nim}}==
=={{header|Nim}}==
I borrowed some ideas from Go version, but keep the limit to 100_000 as in Wren version.
I borrowed some ideas from Go version, but keep the limit to 100_000 as in Wren version.
<lang Nim>import math, strutils
<syntaxhighlight lang="nim">import math, strutils


const
const
Line 866: Line 866:
if lim == Lim1:
if lim == Lim1:
# Emit last message.
# Emit last message.
echo CountMessage.format(count, lim)</lang>
echo CountMessage.format(count, lim)</syntaxhighlight>


{{out}}
{{out}}
Line 891: Line 891:
Appended a list of count of untouchable numbers out of math.dartmouth.edu/~carlp/uupaper3.pdf<BR>
Appended a list of count of untouchable numbers out of math.dartmouth.edu/~carlp/uupaper3.pdf<BR>
Nigel is still right, that this procedure is not the way to get proven results.
Nigel is still right, that this procedure is not the way to get proven results.
<lang pascal>program UntouchableNumbers;
<syntaxhighlight lang="pascal">program UntouchableNumbers;
program UntouchableNumbers;
program UntouchableNumbers;
{$IFDEF FPC}
{$IFDEF FPC}
Line 1,267: Line 1,267:


OutCounts(pUntouch);
OutCounts(pUntouch);
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,367: Line 1,367:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use enum qw(False True);
use enum qw(False True);
Line 1,403: Line 1,403:
printf($fmt, scalar @untouchable, $limit) and last if $limit == ($p *= 10)
printf($fmt, scalar @untouchable, $limit) and last if $limit == ($p *= 10)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Number of untouchable numbers ≤ 2000 : 196
<pre>Number of untouchable numbers ≤ 2000 : 196
Line 1,429: Line 1,429:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">limz</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">64</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- found by experiment</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limz</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">64</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- found by experiment</span>
Line 1,489: Line 1,489:
<span style="color: #000000;">untouchable</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">-(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()==</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">untouchable</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">-(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()==</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,527: Line 1,527:
Borrow the proper divisors routine from [https://rosettacode.org/wiki/Proper_divisors#Raku here].
Borrow the proper divisors routine from [https://rosettacode.org/wiki/Proper_divisors#Raku here].
{{trans|Wren}}
{{trans|Wren}}
<lang perl6># 20210220 Raku programming solution
<syntaxhighlight lang="raku" line># 20210220 Raku programming solution


sub propdiv (\x) {
sub propdiv (\x) {
Line 1,570: Line 1,570:
}
}
}
}
printf "%6d untouchable numbers were found ≤ %7d\n", +@untouchable, limit</lang>
printf "%6d untouchable numbers were found ≤ %7d\n", +@untouchable, limit</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,615: Line 1,615:


This version of REXX would need a 64-bit version to calculate the number of untouchable numbers for one million.
This version of REXX would need a 64-bit version to calculate the number of untouchable numbers for one million.
<lang rexx>/*REXX pgm finds N untouchable numbers (numbers that can't be equal to any aliquot sum).*/
<syntaxhighlight lang="rexx">/*REXX pgm finds N untouchable numbers (numbers that can't be equal to any aliquot sum).*/
parse arg n cols tens over . /*obtain optional arguments from the CL*/
parse arg n cols tens over . /*obtain optional arguments from the CL*/
if n='' | n=="," then n=2000 /*Not specified? Then use the default.*/
if n='' | n=="," then n=2000 /*Not specified? Then use the default.*/
Line 1,675: Line 1,675:
end /*m*/ /* [↑] process an even integer. ___*/
end /*m*/ /* [↑] process an even integer. ___*/
if q.m==j then return s + m /*Was J a square? If so, add √ J */
if q.m==j then return s + m /*Was J a square? If so, add √ J */
return s /* No, just return. */</lang>
return s /* No, just return. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,716: Line 1,716:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
Not an easy task as, even allowing for the prime tests, it's difficult to know how far you need to sieve to get the right answers. The parameters here were found by trial and error.
Not an easy task as, even allowing for the prime tests, it's difficult to know how far you need to sieve to get the right answers. The parameters here were found by trial and error.
<lang ecmascript>import "/math" for Int, Nums
<syntaxhighlight lang="ecmascript">import "/math" for Int, Nums
import "/seq" for Lst
import "/seq" for Lst
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 1,756: Line 1,756:
}
}
}
}
Fmt.print("$,6d untouchable numbers were found <= $,d", untouchable.count, limit)</lang>
Fmt.print("$,6d untouchable numbers were found <= $,d", untouchable.count, limit)</syntaxhighlight>


{{out}}
{{out}}