Jump to content

Numbers whose count of divisors is prime: Difference between revisions

m
syntax highlighting fixup automation
(Added 11l)
m (syntax highlighting fixup automation)
Line 11:
{{trans|FreeBASIC}}
 
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 35:
print()
 
print("\n\nFound "row‘ numbers’)</langsyntaxhighlight>
 
{{out}}
Line 62:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
INT FUNC CountDivisors(INT x)
Line 99:
FI
OD
RETURN</langsyntaxhighlight>
{{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]
Line 123:
=={{header|ALGOL 68}}==
Counts the divisors without using division.
<langsyntaxhighlight lang="algol68">BEGIN # find numbers with prime divisor counts #
INT max number := 1 000;
TO 2 DO
Line 145:
max number := 100 000
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 163:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_WHOSE_COUNT_OF_DIVISORS_IS_PRIME.AWK
BEGIN {
Line 201:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 220:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">function isPrime(v)
if v < 2 then return False
if (v mod 2) = 0 then return v = 2
Line 248:
print
print "Found "; row; " numbers"
end</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
Line 280:
 
Print !"\n\nFound"; row; " numbers"
Sleep</langsyntaxhighlight>
{{out}}
<pre>Numbers which count of divisors is prime are:
Line 306:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight QBasiclang="qbasic">row = 0
 
PRINT "Numbers which count of divisors is prime are:"
Line 335:
WEND
isPrime = True
END FUNCTION</langsyntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight PureBasiclang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 383:
Input()
CloseConsole()
End</langsyntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">row = 0
 
print "Numbers which count of divisors is prime are:"
Line 415:
end while
return True
end sub</langsyntaxhighlight>
{{out}}
<pre>
Line 423:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <cstdlib>
#include <iomanip>
Line 488:
std::cout << "\nCount: " << count << '\n';
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 512:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Find the amount of divisors for 1..N
div_counts = proc (n: int) returns (sequence[int])
divs: array[int] := array[int]$fill(1,n,1)
Line 559:
end
stream$putl(po, "\nFound " || int$unparse(count) || " numbers.")
end start_up</langsyntaxhighlight>
{{out}}
<pre> 4 9 16 25 49 64 81 121 169 289
Line 573:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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 ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 584:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel math math.primes
math.primes.factors math.ranges sequences sequences.extras ;
FROM: math.extras => integer-sqrt ;
Line 594:
[ sq ] [ divisors length odd-prime? ] map-filter ;
 
100,000 pdc-upto 10 group [ [ "%-8d" printf ] each nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 609:
=={{header|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 657:
}
fmt.Printf("\n\nFound %d such integers (%d under 1,000).\n", len(results), under1000)
}</langsyntaxhighlight>
 
{{out}}
Line 679:
 
For a suitable definition of `is_prime`, see [[Erd%C5%91s-primes#jq]].
<langsyntaxhighlight lang="jq">def add(s): reduce s as $x (null; .+$x);
 
def count_divisors:
Line 700:
1000, 100000
| "\nn with odd prime divisor counts, 1 < n < \(.):",
(range(1;.) | select(count_divisors | (. > 2 and is_prime)))</langsyntaxhighlight>
{{out}}
<pre>
Line 732:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
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)))
</langsyntaxhighlight>{{out}}
<pre>
4 9 16 25 49 64 81 121 169 289
Line 749:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">max = 100000;
maxPrime = NextPrime[Sqrt@max, -1];
maxPower = NextPrime[Log[2, max], -1];
Line 758:
TableForm, "Numbers up to 1000 with prime divisor counts:", Top]
Labeled[Partition[ans, UpTo[8]] //
TableForm, "Numbers up to 100,000 with prime divisor counts:", Top]</langsyntaxhighlight>
 
{{out}}<pre>
Line 781:
Checking only divisors of squares (see discussion).
 
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strformat, strutils
 
 
Line 823:
for i, n in list:
stdout.write &"{n:5}", if (i + 1) mod 10 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 840:
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program FacOfInteger;
{$IFDEF FPC}
// {$R+,O+} //debuging purpose
Line 1,176:
writeln;
SpeedTest(pD,4000*1000*1000);
END.</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10
Line 1,203:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <is_prime divisors>;
 
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;</langsyntaxhighlight>
{{out}}
<pre>79 matching:
Line 1,220:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,237:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
my $ceiling = ceiling sqrt 1e5;
Line 1,246:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</langsyntaxhighlight>
{{out}}
<pre>79 matching:
Line 1,259:
 
=={{header|REXX}}==
<langsyntaxhighlight 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*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the defaults*/
Line 1,307:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,337:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
row = 0
Line 1,362:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,376:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var limit = 100_000
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|
say a.map{'%6s' % _}.join(' ')
})</langsyntaxhighlight>
{{out}}
<pre>
Line 1,399:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/seq" for Lst
import "/fmt" for Fmt
Line 1,414:
for (chunk in Lst.chunks(results, 10)) Fmt.print("$,7d", chunk)
var under1000 = results.count { |r| r < 1000 }
System.print("\nFound %(results.count) such integers (%(under1000) under 1,000).")</langsyntaxhighlight>
 
{{out}}
Line 1,432:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,458:
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.