Numbers whose count of divisors is prime: Difference between revisions

Content added Content deleted
(Added 11l)
m (syntax highlighting fixup automation)
Line 11: Line 11:
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}


<lang 11l>F is_prime(a)
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
I a == 2
R 1B
R 1B
Line 35: Line 35:
print()
print()


print("\n\nFound "row‘ numbers’)</lang>
print("\n\nFound "row‘ numbers’)</syntaxhighlight>


{{out}}
{{out}}
Line 62: Line 62:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
{{libheader|Action! Sieve of Eratosthenes}}
<lang Action!>INCLUDE "H6:SIEVE.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"


INT FUNC CountDivisors(INT x)
INT FUNC CountDivisors(INT x)
Line 99: Line 99:
FI
FI
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_whose_count_of_divisors_is_prime.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_whose_count_of_divisors_is_prime.png Screenshot from Atari 8-bit computer]
Line 123: Line 123:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Counts the divisors without using division.
Counts the divisors without using division.
<lang algol68>BEGIN # find numbers with prime divisor counts #
<syntaxhighlight lang="algol68">BEGIN # find numbers with prime divisor counts #
INT max number := 1 000;
INT max number := 1 000;
TO 2 DO
TO 2 DO
Line 145: Line 145:
max number := 100 000
max number := 100 000
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 163: Line 163:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_WHOSE_COUNT_OF_DIVISORS_IS_PRIME.AWK
# syntax: GAWK -f NUMBERS_WHOSE_COUNT_OF_DIVISORS_IS_PRIME.AWK
BEGIN {
BEGIN {
Line 201: Line 201:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 220: Line 220:
==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang BASIC256>function isPrime(v)
<syntaxhighlight lang="basic256">function isPrime(v)
if v < 2 then return False
if v < 2 then return False
if (v mod 2) = 0 then return v = 2
if (v mod 2) = 0 then return v = 2
Line 248: Line 248:
print
print
print "Found "; row; " numbers"
print "Found "; row; " numbers"
end</lang>
end</syntaxhighlight>


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>Function isPrime(Byval ValorEval As Integer) As Boolean
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval < 2 Then Return False
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
Line 280: Line 280:


Print !"\n\nFound"; row; " numbers"
Print !"\n\nFound"; row; " numbers"
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>Numbers which count of divisors is prime are:
<pre>Numbers which count of divisors is prime are:
Line 306: Line 306:
{{works with|QuickBasic}}
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang QBasic>row = 0
<syntaxhighlight lang="qbasic">row = 0


PRINT "Numbers which count of divisors is prime are:"
PRINT "Numbers which count of divisors is prime are:"
Line 335: Line 335:
WEND
WEND
isPrime = True
isPrime = True
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang PureBasic>Procedure isPrime(v.i)
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v < 4 : ProcedureReturn #True
Line 383: Line 383:
Input()
Input()
CloseConsole()
CloseConsole()
End</lang>
End</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang yabasic>row = 0
<syntaxhighlight lang="yabasic">row = 0


print "Numbers which count of divisors is prime are:"
print "Numbers which count of divisors is prime are:"
Line 415: Line 415:
end while
end while
return True
return True
end sub</lang>
end sub</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 423: Line 423:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <cmath>
<syntaxhighlight lang="cpp">#include <cmath>
#include <cstdlib>
#include <cstdlib>
#include <iomanip>
#include <iomanip>
Line 488: Line 488:
std::cout << "\nCount: " << count << '\n';
std::cout << "\nCount: " << count << '\n';
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 512: Line 512:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>% Find the amount of divisors for 1..N
<syntaxhighlight lang="clu">% Find the amount of divisors for 1..N
div_counts = proc (n: int) returns (sequence[int])
div_counts = proc (n: int) returns (sequence[int])
divs: array[int] := array[int]$fill(1,n,1)
divs: array[int] := array[int]$fill(1,n,1)
Line 559: Line 559:
end
end
stream$putl(po, "\nFound " || int$unparse(count) || " numbers.")
stream$putl(po, "\nFound " || int$unparse(count) || " numbers.")
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> 4 9 16 25 49 64 81 121 169 289
<pre> 4 9 16 25 49 64 81 121 169 289
Line 573: Line 573:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Numbers whose divisor count is prime. Nigel Galloway: July 13th., 2021
// Numbers whose divisor count is prime. Nigel Galloway: July 13th., 2021
primes64()|>Seq.takeWhile(fun n->n*n<100000L)|>Seq.collect(fun n->primes32()|>Seq.skip 1|>Seq.map(fun g->pown n (g-1))|>Seq.takeWhile((>)100000L))|>Seq.sort|>Seq.iter(printf "%d "); printfn ""
primes64()|>Seq.takeWhile(fun n->n*n<100000L)|>Seq.collect(fun n->primes32()|>Seq.skip 1|>Seq.map(fun g->pown n (g-1))|>Seq.takeWhile((>)100000L))|>Seq.sort|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 584: Line 584:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting grouping io kernel math math.primes
<syntaxhighlight lang="factor">USING: formatting grouping io kernel math math.primes
math.primes.factors math.ranges sequences sequences.extras ;
math.primes.factors math.ranges sequences sequences.extras ;
FROM: math.extras => integer-sqrt ;
FROM: math.extras => integer-sqrt ;
Line 594: Line 594:
[ sq ] [ divisors length odd-prime? ] map-filter ;
[ sq ] [ divisors length odd-prime? ] map-filter ;


100,000 pdc-upto 10 group [ [ "%-8d" printf ] each nl ] each</lang>
100,000 pdc-upto 10 group [ [ "%-8d" printf ] each nl ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 609: Line 609:
=={{header|Go}}==
=={{header|Go}}==
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 657: Line 657:
}
}
fmt.Printf("\n\nFound %d such integers (%d under 1,000).\n", len(results), under1000)
fmt.Printf("\n\nFound %d such integers (%d under 1,000).\n", len(results), under1000)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 679: Line 679:


For a suitable definition of `is_prime`, see [[Erd%C5%91s-primes#jq]].
For a suitable definition of `is_prime`, see [[Erd%C5%91s-primes#jq]].
<lang jq>def add(s): reduce s as $x (null; .+$x);
<syntaxhighlight lang="jq">def add(s): reduce s as $x (null; .+$x);


def count_divisors:
def count_divisors:
Line 700: Line 700:
1000, 100000
1000, 100000
| "\nn with odd prime divisor counts, 1 < n < \(.):",
| "\nn with odd prime divisor counts, 1 < n < \(.):",
(range(1;.) | select(count_divisors | (. > 2 and is_prime)))</lang>
(range(1;.) | select(count_divisors | (. > 2 and is_prime)))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 732: Line 732:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


ispdc(n) = (ndivs = prod(collect(values(factor(n))).+ 1); ndivs > 2 && isprime(ndivs))
ispdc(n) = (ndivs = prod(collect(values(factor(n))).+ 1); ndivs > 2 && isprime(ndivs))


foreach(p -> print(rpad(p[2], 8), p[1] % 10 == 0 ? "\n" : ""), enumerate(filter(ispdc, 1:100000)))
foreach(p -> print(rpad(p[2], 8), p[1] % 10 == 0 ? "\n" : ""), enumerate(filter(ispdc, 1:100000)))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
4 9 16 25 49 64 81 121 169 289
4 9 16 25 49 64 81 121 169 289
Line 749: Line 749:
</pre>
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>max = 100000;
<syntaxhighlight lang="mathematica">max = 100000;
maxPrime = NextPrime[Sqrt@max, -1];
maxPrime = NextPrime[Sqrt@max, -1];
maxPower = NextPrime[Log[2, max], -1];
maxPower = NextPrime[Log[2, max], -1];
Line 758: Line 758:
TableForm, "Numbers up to 1000 with prime divisor counts:", Top]
TableForm, "Numbers up to 1000 with prime divisor counts:", Top]
Labeled[Partition[ans, UpTo[8]] //
Labeled[Partition[ans, UpTo[8]] //
TableForm, "Numbers up to 100,000 with prime divisor counts:", Top]</lang>
TableForm, "Numbers up to 100,000 with prime divisor counts:", Top]</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 781: Line 781:
Checking only divisors of squares (see discussion).
Checking only divisors of squares (see discussion).


<lang Nim>import math, sequtils, strformat, strutils
<syntaxhighlight lang="nim">import math, sequtils, strformat, strutils




Line 823: Line 823:
for i, n in list:
for i, n in list:
stdout.write &"{n:5}", if (i + 1) mod 10 == 0: '\n' else: ' '
stdout.write &"{n:5}", if (i + 1) mod 10 == 0: '\n' else: ' '
echo()</lang>
echo()</syntaxhighlight>


{{out}}
{{out}}
Line 840: Line 840:


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>program FacOfInteger;
<syntaxhighlight lang="pascal">program FacOfInteger;
{$IFDEF FPC}
{$IFDEF FPC}
// {$R+,O+} //debuging purpose
// {$R+,O+} //debuging purpose
Line 1,176: Line 1,176:
writeln;
writeln;
SpeedTest(pD,4000*1000*1000);
SpeedTest(pD,4000*1000*1000);
END.</lang>
END.</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10
<pre> 1 2 3 4 5 6 7 8 9 10
Line 1,203: Line 1,203:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory <is_prime divisors>;
use ntheory <is_prime divisors>;


push @matches, $_**2 for grep { is_prime divisors $_**2 } 1..int sqrt 1e5;
push @matches, $_**2 for grep { is_prime divisors $_**2 } 1..int sqrt 1e5;
print @matches . " matching:\n" . (sprintf "@{['%6d' x @matches]}", @matches) =~ s/(.{72})/$1\n/gr;</lang>
print @matches . " matching:\n" . (sprintf "@{['%6d' x @matches]}", @matches) =~ s/(.{72})/$1\n/gr;</syntaxhighlight>
{{out}}
{{out}}
<pre>79 matching:
<pre>79 matching:
Line 1,220: Line 1,220:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<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: #008080;">function</span> <span style="color: #000000;">pd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">return</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">return</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 1,228: Line 1,228:
<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;">"%d &lt; %,d found: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</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;">"%d &lt; %,d found: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,237: Line 1,237:
=={{header|Raku}}==
=={{header|Raku}}==


<lang perl6>use Prime::Factor;
<syntaxhighlight lang="raku" line>use Prime::Factor;


my $ceiling = ceiling sqrt 1e5;
my $ceiling = ceiling sqrt 1e5;
Line 1,246: Line 1,246:
cache $list;
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>79 matching:
<pre>79 matching:
Line 1,259: Line 1,259:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds positive integers N whose # of divisors is prime (& ¬=2), where N<1000.*/
<syntaxhighlight lang="rexx">/*REXX pgm finds positive integers N whose # of divisors is prime (& ¬=2), where N<1000.*/
parse arg hi cols . /*obtain optional arguments from the CL*/
parse arg hi cols . /*obtain optional arguments from the CL*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the defaults*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the defaults*/
Line 1,307: Line 1,307:
end /*k*/ /* [↑] only process numbers ≤ √ J */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,337: Line 1,337:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
row = 0
row = 0
Line 1,362: Line 1,362:
see nl + "Found " + row + " numbers" + nl
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,376: Line 1,376:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var limit = 100_000
<syntaxhighlight lang="ruby">var limit = 100_000
say "Positive integers under #{limit.commify} whose number of divisors is an odd prime:"
say "Positive integers under #{limit.commify} whose number of divisors is an odd prime:"


1..limit -> grep { !.is_prime && .sigma0.is_prime }.each_slice(10, {|*a|
1..limit -> grep { !.is_prime && .sigma0.is_prime }.each_slice(10, {|*a|
say a.map{'%6s' % _}.join(' ')
say a.map{'%6s' % _}.join(' ')
})</lang>
})</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,399: Line 1,399:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/seq" for Lst
import "/seq" for Lst
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 1,414: Line 1,414:
for (chunk in Lst.chunks(results, 10)) Fmt.print("$,7d", chunk)
for (chunk in Lst.chunks(results, 10)) Fmt.print("$,7d", chunk)
var under1000 = results.count { |r| r < 1000 }
var under1000 = results.count { |r| r < 1000 }
System.print("\nFound %(results.count) such integers (%(under1000) under 1,000).")</lang>
System.print("\nFound %(results.count) such integers (%(under1000) under 1,000).")</syntaxhighlight>


{{out}}
{{out}}
Line 1,432: Line 1,432:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[if N <= 1 then return false;
[if N <= 1 then return false;
Line 1,458: Line 1,458:
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}