Almost prime: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|PHP}}: Spaces _before_ numbers. Function floor instead of casting to int.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20:
=={{header|11l}}==
{{trans|Kotlin}}
<
V f = 0
V p = 2
Line 40:
L(k) 1..5
print(‘k = ’k‘: ’primes(k, 10))</
{{out}}
<pre>k = 1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 49:
=={{header|Action!}}==
<
INT f,p,v
Line 87:
PutE()
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Almost_prime.png Screenshot from Atari 8-bit computer]
Line 102:
This imports the package '''Prime_Numbers''' from [[Prime decomposition#Ada]].
<
procedure Test_Kth_Prime is
Line 126:
Ada.Text_IO.New_Line;
end loop;
end Test_Kth_Prime;</
{{output}}
Line 137:
=={{header|ALGOL 68}}==
Worth noticing is the n(...)(...) picture in the printf and the WHILE ... DO SKIP OD idiom which is quite common in ALgol 68.
<
INT examples=10, classes=5;
MODE SEMIPRIME = STRUCT ([examples]INT data, INT count);
Line 177:
printf (($"k = ", d, ":", n(examples)(xg(0))l$, i, data OF semi primes[i]))
OD
END</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 187:
=={{header|ALGOL-M}}==
<
integer function mod(a, b);
Line 230:
end;
end;
end</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 239:
=={{header|ALGOL W}}==
{{Trans|C}} with tweaks to the factorisation routine.
<
logical procedure kPrime( integer value nv, k ) ;
begin
Line 275:
end for_k
end
end.</
{{out}}
<pre>
Line 288:
{{libheader|pco}}
Works in [[Dyalog APL]]
<
{{out}}
<pre>
Line 301:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI */
/* program kprime.s */
Line 523:
pop {r4, lr}
bx lr
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 535:
=={{header|Arturo}}==
<
result: new []
test: 2
Line 562:
loop 1..5 'x ->
print ["k:" x "=>" almostPrime x 10]</
{{out}}
Line 575:
ASIC has both FOR and WHILE loops, but it had better not go out from the loop. So, in the subroutine CHECKKPRIME they are simulated by the constructs with GOTO statements.
<
REM Almost prime
FOR K = 1 TO 5
Line 623:
ISKPRIME = 0
RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 635:
=={{header|AutoHotkey}}==
Translation of the C Version
<
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
Line 661:
}
MsgBox % results</
'''Output (Msgbox):'''
Line 671:
=={{header|AWK}}==
<
# syntax: GAWK -f ALMOST_PRIME.AWK
BEGIN {
Line 697:
return(f + (n > 1) == k)
}
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 708:
=={{header|BASIC}}==
<
20 FOR K=1 TO 5
30 PRINT USING "K = #:";K;
Line 722:
130 IF C < 10 THEN 50
140 PRINT
150 NEXT K</
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 732:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<
f = 0
for i = 2 to n
Line 757:
print
next k
end</
=={{header|BCPL}}==
{{trans|C}}
<
let kprime(n, k) = valof
Line 790:
wrch('*N')
$)
$)</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 802:
The extra spaces are to ensure it's readable on buggy interpreters that don't include a space after numeric output.
<
|^ v0!`\*:g40:<p402p300:+1<
K| >2g03g`*#v_ 1`03g+02g->|
F@>/03g1+03p>vpv+1\.:,*48 <
P#|!\g40%g40:<4>:9`>#v_\1^|
|^>#!1#`+#50#:^#+1,+5>#5$<|</
{{out}}
Line 817:
=={{header|C}}==
<
int kprime(int n, int k)
Line 846:
return 0;
}</
{{out}}
<pre>
Line 857:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Linq;
Line 911:
}
}
}</
{{out}}
<pre>
Line 923:
=={{header|C++}}==
{{trans|Kotlin}}
<
#include <iostream>
#include <sstream>
Line 954:
return EXIT_SUCCESS;
}</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 964:
=={{header|Clojure}}==
<
(ns clojure.examples.almostprime
Line 988:
(println "k:" k (divisors-k k 10))))
}</
{{out}}
<pre>
Line 1,000:
=={{header|CLU}}==
<
f: int := 0
p: int := 2
Line 1,029:
stream$putl(po, "")
end
end start_up</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,039:
=={{header|COBOL}}==
{{trans|C}}
<
PROGRAM-ID. ALMOST-PRIME.
Line 1,107:
MOVE NEXT-N TO N.
ADD 1 TO F.
DIVIDE N BY P GIVING N-DIV-P.</
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,116:
=={{header|Common Lisp}}==
<
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
Line 1,128:
(cond ((> d (isqrt n)) (+ c 1))
((zerop (rem n d)) (?-primality (/ n d) d (+ c 1)))
(t (?-primality n (+ d 1) c))))</
{{out}}
<pre>
Line 1,140:
=={{header|Cowgol}}==
{{trans|C}}
<
sub kprime(n: uint8, k: uint8): (kp: uint8) is
Line 1,180:
print_nl();
k := k + 1;
end loop;</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,191:
This contains a copy of the function <code>decompose</code> from the Prime decomposition task.
{{trans|Ada}}
<
Unqual!T[] decompose(T)(in T number) pure nothrow
Line 1,226:
writeln;
}
}</
{{out}}
Line 1,238:
{{trans|C}}
<
program AlmostPrime;
Line 1,279:
end;
end.
</syntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,289:
=={{header|Draco}}==
<
word f, p;
f := 0;
Line 1,320:
writeln()
od
corp</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,330:
=={{header|EchoLisp}}==
Small numbers : filter the sequence [ 2 .. n]
<
(define (almost-prime? p k)
(= k (length (prime-factors p))))
Line 1,342:
(for-each write (almost-primes k nmax))
(writeln)))
</syntaxhighlight>
{{out}}
<
(task)
Line 1,352:
k= 4 | 16 24 36 40 54 56 60 81 84 88
k= 5 | 32 48 72 80 108 112 120 162 168 176
</syntaxhighlight>
Large numbers : generate - combinations with repetitions - k-almost-primes up to pmax.
<
(lib 'match)
(define-syntax-rule (: v i) (vector-ref v i))
Line 1,405:
almost-prime)))
</syntaxhighlight>
{{out}}
<
;; we want 500-almost-primes from the 10000-th.
(take (drop (list-sort < (almost-primes 500 10000)) 10000 ) 10)
Line 1,418:
;; The first one is 2^497 * 3 * 17 * 347 , same result as Haskell.
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Erlang}}
<
def factors(n), do: factors(n,2,[])
Line 1,444:
end
Factors.kfactors(10,5)</
{{out}}
Line 1,458:
Using the factors function from [[Prime_decomposition#Erlang]].
<
-module(factors).
-export([factors/1,kfactors/0,kfactors/2]).
Line 1,483:
_ ->
kfactors(Tn,Tk, N+1,K, Acc) end.
</
{{out}}
Line 1,509:
=={{header|ERRE}}==
<
PROGRAM ALMOST_PRIME
Line 1,546:
END FOR
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,555:
=={{header|F_Sharp|F#}}==
<
if f > n then None
elif n % f = 0 then Some (f, (f, n/f))
Line 1,573:
[1 .. 5]
|> List.iter (fun k ->
printfn "%A" (Seq.take 10 (kFactors k) |> Seq.toList))</
{{out}}
<pre>[2; 3; 5; 7; 11; 13; 17; 19; 23; 29]
Line 1,582:
=={{header|Factor}}==
<
math.combinatorics math.primes.factors math.ranges sequences ;
IN: rosetta-code.almost-prime
Line 1,593:
10 0 lfrom [ k k-almost-prime? ] lfilter ltake list>array ;
5 [1,b] [ dup first10 "K = %d: %[%3d, %]\n" printf ] each</
{{out}}
<pre>
Line 1,605:
=={{header|FOCAL}}==
<
01.20 Q
Line 1,624:
03.60 S I=I+1
03.70 I (C-10)3.3
03.80 T !</
{{out}}
<pre>K= 1: = 2 = 3 = 5 = 7 = 11 = 13 = 17 = 19 = 23 = 29
Line 1,633:
=={{header|Fortran}}==
<
program almost_prime
use iso_fortran_env, only: output_unit
Line 1,679:
end function kprime
end program almost_prime
</syntaxhighlight>
{{out}}
Line 1,691:
=={{header|FreeBASIC}}==
<
Function kPrime(n As Integer, k As Integer) As Boolean
Line 1,722:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,734:
=={{header|Frink}}==
<
{
n=2
Line 1,750:
println[]
}</
Output:
Line 1,763:
=={{header|Futhark}}==
<
let kprime(n: i32, k: i32): bool =
let (p,f) = (2, 0)
Line 1,782:
in ps
in map f (1...m)
</syntaxhighlight>
=={{header|Go}}==
<
import "fmt"
Line 1,820:
fmt.Println(k, gen(k, 10))
}
}</
{{out}}
<pre>
Line 1,831:
=={{header|Groovy}}==
<
public class almostprime
{
Line 1,866:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,884:
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
<
10 'Almost prime
20 FOR K% = 1 TO 5
Line 1,910:
1080 LET ISKPRIME = (F% = K%)
1090 RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 1,921:
=={{header|Haskell}}==
<
isPrime n = not $ any ((0 ==) . (mod n)) [2..(truncate $ sqrt $ fromIntegral n)]
Line 1,938:
main :: IO ()
main = flip mapM_ [1..5] $ \k ->
putStrLn $ "k = " ++ show k ++ ": " ++ (unwords $ map show (take 10 $ kPrimes k))</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,947:
Larger ''k''s require more complicated methods:
<
True (drop 1 primes)]
Line 1,973:
putStrLn "\n10000th to 10100th 500-amost primes:"
mapM_ print $ take 100 $ drop 10000 $ kprimes 500</
{{out}}
<pre>
Line 1,990:
Works in both languages.
<
procedure main()
Line 1,999:
procedure genKap(k)
suspend (k = *factors(n := seq(q)), n)
end</
Output:
Line 2,013:
=={{header|J}}==
<
2 3 5 7 11 13 17 19 23 29
4 6 9 10 14 15 21 22 25 26
8 12 18 20 27 28 30 42 44 45
16 24 36 40 54 56 60 81 84 88
32 48 72 80 108 112 120 162 168 176</
Explanation:
#Generate 10 primes.
Line 2,029:
=={{header|Java}}==
<
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
Line 2,055:
return f + ((n > 1) ? 1 : 0) == k;
}
}</
{{out}}
<pre>
Line 2,066:
=={{header|Javascript}}==
<
var divisor = 2, count = 0
while(count < k + 1 && n != 1) {
Line 2,089:
}
}
}</
{{out}}
<pre>
Line 2,101:
{{Works with| jq|1.4}}
'''Infrastructure:'''
<
def until(cond; next):
def _until:
Line 2,184:
if ($in % $p) == 0 then . + [$in|multiplicity($p)] else . end )
end
end;</
'''isalmostprime'''
<
# Emit a stream of the first N almost-k primes
Line 2,199:
end)
| .[2] | select(. != null)
end;</
'''The task:'''
<
{{out}}
<
k=1: [2,3,5,7,11,13,17,19,23,29]
k=2: [4,6,9,10,14,15,21,22,25,26]
k=3: [8,12,18,20,27,28,30,42,44,45]
k=4: [16,24,36,40,54,56,60,81,84,88]
k=5: [32,48,72,80,108,112,120,162,168,176]</
=={{header|Julia}}==
{{works with|Julia|1.1}}
<
isalmostprime(n::Integer, k::Integer) = sum(values(factor(n))) == k
Line 2,228:
for k in 1:5
println("$k-Almost-primes: ", join(almostprimes(10, k), ", "), "...")
end</
{{out}}
Line 2,239:
=={{header|Kotlin}}==
{{trans|Java}}
<
var n = x
var f = 0
Line 2,263:
for (k in 1..5)
println("k = $k: " + k.primes(10))
}</
{{out}}
<pre>k = 1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 2,274:
{{trans|FreeBASIC}}
{{works with|Just BASIC}}
<syntaxhighlight lang=lb>
' Almost prime
for k = 1 to 5
Line 2,302:
kPrime = abs(f = k)
end function
</syntaxhighlight>
{{out}}
<pre>
Line 2,313:
=={{header|Lua}}==
<
function almostPrime (n, k)
local divisor, count = 2, 0
Line 2,346:
end
print("...")
end</
{{out}}
<pre>k=1: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ...
Line 2,355:
=={{header|Maple}}==
<
local aprimes, i, intfactors;
aprimes := Array([]);
Line 2,370:
aprimes;
end proc:
<seq( AlmostPrimes(i), i = 1..5 )>;</
{{out}}
<pre>
Line 2,380:
=={{header|MAD}}==
<
INTERNAL FUNCTION(NN,KK)
Line 2,415:
0 KPR(C*5+4), KPR(C*5+5)
END OF PROGRAM </
{{out}}
<pre> K=1 K=2 K=3 K=4 K=5
Line 2,430:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
(* generates a list of the n smallest k-almost-primes *)
Module[{firstnprimes, runningkprimes = {}},
Line 2,443:
]
(* now to create table with n=10 and k ranging from 1 to 5 *)
Table[Flatten[{"k = " <> ToString[i] <> ": ", kprimes[i, 10]}], {i,1,5}] // TableForm</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,452:
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,497:
ReadChar;
END AlmostPrime.</
=={{header|Nascom BASIC}}==
{{trans|GW-BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<
10 REM Almost prime
20 FOR K=1 TO 5
Line 2,531:
1080 ISKPRIME=(F=K)
1090 RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 2,542:
=={{header|Nim}}==
<
result = @[]
var
Line 2,564:
for k in 1..5:
echo "k = ",k," : ",prime(k,10)</
{{out}}
<pre>k = 1 : @[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 2,574:
=={{header|Objeck}}==
{{trans|C}}
<
function : native : kPrime(n : Int, k : Int) ~ Bool {
f := 0;
Line 2,600:
};
}
}</
{{out}}
Line 2,612:
=={{header|Oforth}}==
<
| i |
0 2 n for: i [
Line 2,625:
2 while (l size 10 <>) [ dup k kprime? if dup l add then 1+ ]
drop
;</
{{out}}
Line 2,638:
=={{header|PARI/GP}}==
<
for(k=1,5,almost(k);print)</
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
Line 2,650:
{{libheader|primTrial}}
{{works with|Free Pascal}}
<
{$IFDEF FPC}
{$Mode Delphi}
Line 2,675:
inc(k);
until k > 10;
END.</
;output:
<pre>K= 1 : 2 3 5 7 11 13 17 19 23 29
Line 2,691:
Using a CPAN module, which is simple and fast:
{{libheader|ntheory}}
<
sub almost {
my($k,$n) = @_;
Line 2,697:
map { $i++ while scalar factor($i) != $k; $i++ } 1..$n;
}
say "$_ : ", join(" ", almost($_,10)) for 1..5;</
{{out}}
<pre>
Line 2,707:
</pre>
or writing everything by hand:
<
use warnings;
Line 2,770:
return $primes[$n];
}
}</
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29
Line 2,781:
=={{header|Phixmonti}}==
{{trans|OForth}}
<
by Galileo, 06/2022 #/
Line 2,807:
endfor
pstack</
{{out}}
<pre>
Line 2,816:
=={{header|PHP}}==
{{trans|FreeBASIC}}
<
<?php
// Almost prime
Line 2,848:
}
?>
</syntaxhighlight>
{{out}}
<pre>
Line 2,860:
=={{header|Picat}}==
{{trans|J}}
<
N = 10,
Ps = primes(100).take(N),
Line 2,879:
mul_take(L1,L2,N) = [I*J : I in L1, J in L2, I<=J].sort_remove_dups().take(N).
take(L,N) = [L[I] : I in 1..N].</
{{out}}
Line 2,911:
=={{header|PL/I}}==
{{trans|C}}
<
kprime: procedure(nn, k) returns(bit);
declare (n, nn, k, p, f) fixed;
Line 2,936:
put skip;
end;
end almost_prime;</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,946:
=={{header|PL/M}}==
{{trans|C}}
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 2,997:
END;
CALL EXIT;
EOF</
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 3,006:
=={{header|Phix}}==
<!--<
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span> <span style="color: #000080;font-style:italic;">-- ie {{1},{2},{3},{4},{5}}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 3,021:
<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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 3,032:
=={{header|PicoLisp}}==
<
(make
(let
Line 3,058:
(println I '-> (almost I) ) )
(bye)</
=={{header|Potion}}==
<
kprime = (n, k):
p = 2, f = 0
Line 3,080:
i++
.
"" say.</
C and Potion take 0.006s, Perl5 0.028s
=={{header|Prolog}}==
<
% first Take K-almost-primes.
% Notice that K need not be specified.
Line 3,105:
sort(Long, Sorted), % uniquifies
take(Take, Sorted, List).
</
<
nPrimes( Accumulator, I, Primes) :-
Line 3,146:
length(Head, N),
append(Head,X,List).
</syntaxhighlight>
<
:- if(current_prolog_flag(dialect, yap)).
Line 3,175:
between(Min, Max, I).
:- endif.
</syntaxhighlight>
Example using SWI-Prolog:<pre>
?- between(1,5,I),
Line 3,192:
=={{header|Processing}}==
<
for (int i = 1; i <= 5; i++) {
int count = 0;
Line 3,228:
}
return count;
}</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 3,238:
=={{header|PureBasic}}==
{{trans|C}}
<
Procedure.b kprime(n.i, k.i)
Line 3,280:
Next
Input()</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 3,290:
=={{header|Python}}==
This imports [[Prime decomposition#Python]]
<
from itertools import islice, count
try:
Line 3,308:
if __name__ == '__main__':
for k in range(1,6):
print('%i: %r' % (k, list(islice((n for n in count() if almostprime(n, k)), 10))))</
{{out}}
<pre>1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 3,321:
<
# k-Almost-primes
# Python 3.6.3
Line 3,360:
# try:
#k_almost_primes(6000, 10)
</syntaxhighlight>
{{out}}
<pre>
Line 3,375:
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
<
[ stack ] is factors ( --> s )
Line 3,393:
5 times
[ 10 i^ 1+ dup echo sp
almostprimes echo cr ]</
{{out}}
Line 3,406:
=={{header|R}}==
This uses the function from [[Prime decomposition#R]]
<
# Find k-Almost-primes
# R implementation
Line 3,454:
}
print(res)
}</
{{out}}
Line 3,467:
=={{header|Racket}}==
<
(require (only-in math/number-theory factorize))
Line 3,491:
"\n"))
(displayln (format-table KAP-table-values))</
{{out}}
Line 3,504:
{{trans|C}}
{{works with|Rakudo|2015.12}}
<
loop (my ($p, $f) = 2, 0; $f < $k && $p*$p <= $n; $p++) {
$n /= $p, $f++ while $n %% $p;
Line 3,514:
say ~.[^10]
given grep { is-k-almost-prime($_, $k) }, 2 .. *
}</
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29
Line 3,523:
Here is a solution with identical output based on the <tt>factors</tt> routine from [[Count_in_factors#Raku]] (to be included manually until we decide where in the distribution to put it).
<
multi sub factors(1) { 1 }
Line 3,545:
sub almost($n) { map *.key, grep *.value == $n, @factory }
put almost($_)[^10] for 1..5;</
=={{header|REXX}}==
Line 3,552:
The first three '''k-almost''' primes for each '''K''' group are computed directly (rather than found).
<
parse arg N K . /*get optional arguments from the C.L. */
if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
Line 3,586:
if f==0 then return 1 /*if prime (f==0), then return unity.*/
return f /*return to invoker the number of divs.*/</
{{out|output|text= when using the default input:}}
<pre>
Line 3,623:
Once the required primes are generated, the finding of the K─almost primes is almost instantaneous.
<
parse arg N K . /*obtain optional arguments from the CL*/
if N=='' | N==',' then N=10 /*N not specified? Then use default.*/
Line 3,688:
#=#+1; @.#=j; #.j=1; s.#=j*j /*bump prime count, and also assign ···*/
end /*j*/ /* ··· the # of factors, prime, prime².*/
return /* [↑] not an optimal prime generator.*/</
{{out|output|text= when using the input of: <tt> 20 16 </tt>}}
<pre>
Line 3,712:
=={{header|Ring}}==
<
for ap = 1 to 5
see "k = " + ap + ":"
Line 3,746:
next
return 1
</syntaxhighlight>
Output:
<pre>
Line 3,757:
=={{header|Ruby}}==
<
def almost_primes(k=2)
Line 3,764:
end
(1..5).each{|k| puts almost_primes(k).take(10).join(", ")}</
{{out}}
<pre>
Line 3,775:
{{trans|J}}
<
p ar = pr = Prime.take(10)
4.times{p ar = ar.product(pr).map{|(a,b)| a*b}.uniq.sort.take(10)}</
{{out}}
<pre>
Line 3,789:
=={{header|Rust}}==
<
let mut primes = 0;
let mut f = 2;
Line 3,827:
println!("{}: {:?}", k, kprime_generator(k).take(10).collect::<Vec<_>>());
}
}</
{{out}}
<pre>
Line 3,838:
=={{header|Scala}}==
<
case (n, k, _) if n == 1 => k == 0
case (n, _, d) if n % d == 0 => isKPrime(n / d, k - 1, d)
Line 3,853:
for (k <- 1 to 5) {
println( s"$k: [${ kPrimeStream(k).take(10) mkString " " }]" )
}</
{{out}}
Line 3,865:
=={{header|Seed7}}==
<
const func boolean: kprime (in var integer: number, in integer: k) is func
Line 3,901:
writeln;
end for;
end func;</
{{out}}
Line 3,913:
=={{header|SequenceL}}==
<
import <Utilities/Sequence.sl>;
Line 3,934:
firstNKPrimesHelper(k, N, current + 1, newResult);
isKPrime(k, n) := size(primeFactorization(n)) = k;</
Using Prime Decomposition Solution [http://rosettacode.org/wiki/Prime_decomposition#SequenceL]
Line 3,950:
=={{header|Sidef}}==
{{trans|Raku}}
<
for (var (p, f) = (2, 0); (f < k) && (p*p <= n); ++p) {
(n /= p; ++f) while (p `divides` n)
Line 3,967:
} << 1..Inf
}
} << 1..5</
{{out}}
<pre>
Line 3,979:
=={{header|Swift}}==
<
let k: Int
private(set) var n: Int
Line 4,013:
for k in 1..<6 {
print("\(k): \(Array(KPrimeGen(k: k, n: 1).lazy.prefix(10)))")
}</
{{out}}
Line 4,026:
{{works with|Tcl|8.6}}
{{tcllib|math::numtheory}}
<
package require math::numtheory
Line 4,056:
for {set K 1} {$K <= 5} {incr K} {
puts "$K => [firstN_KalmostPrimes 10 $K]"
}</
{{out}}
<pre>
Line 4,067:
=={{header|Tiny BASIC}}==
<
REM Almost prime
LET K=1
Line 4,100:
530 LET P=0
RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 4,162:
== {{header|TypeScript}} ==
{{trans|FreeBASIC}}
<
function isKPrime(n: number, k: number): bool {
Line 4,188:
console.log();
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,229:
Loop
Next
Return (b@ = (d@ + (a@ > 1)))</
{{trans|FreeBASIC}}
<lang>For k = 1 To 5
Line 4,257:
Next
Return (c@ = b@)
</syntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 4,268:
=={{header|VBA}}==
{{trans|Phix}}<
Dim p As Integer, factors As Integer
p = 2
Line 4,299:
Debug.Print
Next k
End Sub</
<pre>k = 1 : 2 3 5 7 11 13 17 19 23 29
k = 2 : 4 6 9 10 14 15 21 22 25 26
Line 4,308:
=={{header|VBScript}}==
Repurposed the VBScript code for the Prime Decomposition task.
<syntaxhighlight lang=vb>
For k = 1 To 5
count = 0
Line 4,363:
Next
End Function
</syntaxhighlight>
{{Out}}
Line 4,376:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Class KPrime
Line 4,419:
End Sub
End Module</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 4,429:
=={{header|Vlang}}==
{{trans|go}}
<
mut nf := 0
mut nn := n
Line 4,461:
println('$k ${gen(k,10)}')
}
}</
{{out}}
<pre>
Line 4,473:
=={{header|Wren}}==
{{trans|Go}}
<
var nf = 0
var i = 2
Line 4,498:
}
for (k in 1..5) System.print("%(k) %(gen.call(k, 10))") </
{{out}}
Line 4,512:
{{trans|FreeBASIC}}
{{works with|Windows XBasic}}
<
' Almost prime
PROGRAM "almostprime"
Line 4,549:
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Line 4,560:
=={{header|XPL0}}==
<
int N, F, C;
[C:= 0; F:= 2;
Line 4,586:
CrLf(0);
];
]</
{{out}}
Line 4,599:
=={{header|Yabasic}}==
{{trans|Lua}}
<
sub almostPrime(n, k)
local divisor, count
Line 4,639:
next
print "..."
next</
=={{header|zkl}}==
Line 4,646:
Can't say I entirely understand this algorithm. Uses list comprehension to calculate the outer/tensor product (p10 ⊗ ar).
<
(p10:=ar:=primes.walk(10)).println();
do(4){
(ar=([[(x,y);ar;p10;'*]] : Utils.Helpers.listUnique(_).sort()[0,10])).println();
}</
{{out}}
<pre>
Line 4,662:
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<
20 PRINT k;":";
30 LET c=0: LET i=1
Line 4,679:
1040 LET p=p+1: GO TO 1020
1100 LET r=(f+(n>1)=k)
1110 RETURN</
{{out}}
|