Almost prime: Difference between revisions

30,953 bytes added ,  2 months ago
 
(48 intermediate revisions by 24 users not shown)
Line 20:
=={{header|11l}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="11l">F k_prime(k, =n)
V f = 0
V p = 2
Line 40:
 
L(k) 1..5
print(‘k = ’k‘: ’primes(k, 10))</langsyntaxhighlight>
{{out}}
<pre>k = 1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 49:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC IsAlmostPrime(INT num BYTE k)
INT f,p,v
 
Line 87:
PutE()
OD
RETURN</langsyntaxhighlight>
{{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]].
 
<langsyntaxhighlight lang="ada">with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
Line 126:
Ada.Text_IO.New_Line;
end loop;
end Test_Kth_Prime;</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="algol68">BEGIN
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</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 187:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
 
integer function mod(a, b);
Line 230:
end;
end;
end</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="algolw">begin
logical procedure kPrime( integer value nv, k ) ;
begin
Line 275:
end for_k
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 288:
{{libheader|pco}}
Works in [[Dyalog APL]]
<langsyntaxhighlight APLlang="apl">f←{↑r⊣⍵∘{r,∘⊂←⍺↑∪{⍵[⍋⍵]},f∘.×⍵}⍣(⍺-1)⊃r←⊂f←pco¨⍳⍵}</langsyntaxhighlight>
{{out}}
<pre>
Line 301:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program kprime.s */
Line 523:
pop {r4, lr}
bx lr
</syntaxhighlight>
</lang>
<p>Output:</p>
<pre>
Line 535:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">almostPrime: function [k, listLen][
result: new []
test: 2
Line 562:
 
loop 1..5 'x ->
print ["k:" x "=>" almostPrime x 10]</langsyntaxhighlight>
 
{{out}}
Line 571:
k: 4 => [16 24 36 40 54 56 60 81 84 88]
k: 5 => [32 48 72 80 108 112 120 162 168 176]</pre>
 
 
=={{header|AutoHotkey}}==
Translation of the C Version
<langsyntaxhighlight AutoHotkeylang="autohotkey">kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
Line 601 ⟶ 600:
}
 
MsgBox % results</langsyntaxhighlight>
 
'''Output (Msgbox):'''
Line 611 ⟶ 610:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ALMOST_PRIME.AWK
BEGIN {
Line 637 ⟶ 636:
return(f + (n > 1) == k)
}
</syntaxhighlight>
</lang>
<p>Output:</p>
<pre>
Line 648 ⟶ 647:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR K=1 TO 5
30 PRINT USING "K = #:";K;
Line 662 ⟶ 661:
130 IF C < 10 THEN 50
140 PRINT
150 NEXT K</langsyntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 669 ⟶ 668:
K = 4: 16 24 36 40 54 56 60 81 84 88
K = 5: 32 48 72 80 108 112 120 162 168 176</pre>
 
==={{header|ASIC}}===
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.
 
<syntaxhighlight lang="basic">
REM Almost prime
FOR K = 1 TO 5
S$ = STR$(K)
S$ = LTRIM$(S$)
S$ = "k = " + S$
S$ = S$ + ":"
PRINT S$;
I = 2
C = 0
WHILE C < 10
AN = I
GOSUB CHECKKPRIME:
IF ISKPRIME <> 0 THEN
PRINT I;
C = C + 1
ENDIF
I = I + 1
WEND
PRINT
NEXT K
END
 
CHECKKPRIME:
REM Check if N (AN) is a K prime (result: ISKPRIME)
F = 0
J = 2
LOOPFOR:
ANMODJ = AN MOD J
LOOPWHILE:
IF ANMODJ <> 0 THEN AFTERWHILE:
IF F = K THEN FEQK:
F = F + 1
AN = AN / J
ANMODJ = AN MOD J
GOTO LOOPWHILE:
AFTERWHILE:
J = J + 1
IF J <= AN THEN LOOPFOR:
IF F = K THEN
ISKPRIME = -1
ELSE
ISKPRIME = 0
ENDIF
RETURN
FEQK:
ISKPRIME = 0
RETURN
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
if f = k then return False
f += 1
n /= i
end while
next i
return f = k
end function
 
for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print rjust (string(i), 4);
c += 1
end if
i += 1
end while
print
next k
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 'Almost prime
20 FOR k = 1 TO 5
30 PRINT "k = "; k; ":";
40 LET i = 2
50 LET c = 0
60 WHILE c < 10
70 LET an = i: GOSUB 150
80 IF iskprime <> 0 THEN PRINT USING " ###"; i; : LET c = c + 1
90 LET i = i + 1
100 WEND
110 PRINT
120 NEXT k
130 END
140 ' Check if n (AN) is a k (K) prime
150 LET f = 0
160 FOR j = 2 TO an
170 WHILE an MOD j = 0
180 IF f = k THEN LET iskprime = 0: RETURN
190 LET f = f + 1
200 LET an = INT(an / j)
210 WEND
220 NEXT j
230 LET iskprime = (f = k)
240 RETURN</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">for k = 1 to 5
 
print "k = ", k, ": ",
 
let e = 2
let c = 0
 
do
 
if c < 10 then
 
let n = e
gosub kprime
 
if r then
 
print tab, e,
let c = c + 1
 
endif
 
let e = e + 1
 
endif
 
loop c < 10
 
print
 
next k
 
end
 
sub kprime
 
let f = 0
 
for i = 2 to n
 
do
 
if n mod i = 0 then
 
if f = k then
 
let r = 0
return
 
endif
 
let f = f + 1
let n = n / i
 
wait
 
endif
 
loop n mod i = 0
 
next i
 
let r = f = k
 
return</syntaxhighlight>
{{out| Output}}
<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
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
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function kPrime(n As Integer, k As Integer) As Boolean
Dim f As Integer = 0
For i As Integer = 2 To n
While n Mod i = 0
If f = k Then Return false
f += 1
n \= i
Wend
Next
Return f = k
End Function
Dim As Integer i, c, k
For k = 1 To 5
Print "k = "; k; " : ";
i = 2
c = 0
While c < 10
If kPrime(i, k) Then
Print Using "### "; i;
c += 1
End If
i += 1
Wend
Print
Next
 
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
{{out}}
<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
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
</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim i As Integer, c As Integer, k As Integer
 
For k = 1 To 5
Print "k = "; k; " : ";
i = 2
c = 0
While c < 10
If kPrime(i, k) Then
Print Format$(Str$(i), "### ");
c += 1
End If
i += 1
Wend
Print
Next
End
 
Function kPrime(n As Integer, k As Integer) As Boolean
 
Dim f As Integer = 0
For i As Integer = 2 To n
While n Mod i = 0
If f = k Then Return False
f += 1
n \= i
Wend
Next
Return f = k
 
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="qbasic">
10 'Almost prime
20 FOR K% = 1 TO 5
30 PRINT "k = "; K%; ":";
40 LET I% = 2
50 LET C% = 0
60 WHILE C% < 10
70 LET AN% = I%: GOSUB 1000
80 IF ISKPRIME <> 0 THEN PRINT USING " ###"; I%;: LET C% = C% + 1
90 LET I% = I% + 1
100 WEND
110 PRINT
120 NEXT K%
130 END
 
995 ' Check if n (AN%) is a k (K%) prime
1000 LET F% = 0
1010 FOR J% = 2 TO AN%
1020 WHILE AN% MOD J% = 0
1030 IF F% = K% THEN LET ISKPRIME = 0: RETURN
1040 LET F% = F% + 1
1050 LET AN% = AN% \ J%
1060 WEND
1070 NEXT J%
1080 LET ISKPRIME = (F% = K%)
1090 RETURN
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
==={{header|Liberty BASIC}}===
{{trans|FreeBASIC}}
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
' Almost prime
for k = 1 to 5
print "k = "; k; ":";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c + 1
end if
i = i + 1
wend
print
next k
end
 
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
if f = k then kPrime = 0: exit function
f = f + 1
n = int(n / i)
wend
next i
kPrime = abs(f = k)
end function
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
==={{header|Nascom BASIC}}===
{{trans|GW-BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">
10 REM Almost prime
20 FOR K=1 TO 5
30 PRINT "k =";STR$(K);":";
40 I=2
50 C=0
60 IF C>=10 THEN 110
70 AN=I:GOSUB 1000
80 IF ISKPRIME=0 THEN 90
82 REM Print I in 4 fields
84 S$=STR$(I)
86 PRINT SPC(4-LEN(S$));S$;
88 C=C+1
90 I=I+1
100 GOTO 60
110 PRINT
120 NEXT K
130 END
995 REM Check if N (AN) is a K prime
1000 F=0
1010 FOR J=2 TO AN
1020 IF INT(AN/J)*J<>AN THEN 1070
1030 IF F=K THEN ISKPRIME=0:RETURN
1040 F=F+1
1050 AN=INT(AN/J)
1060 GOTO 1020
1070 NEXT J
1080 ISKPRIME=(F=K)
1090 RETURN
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
==={{header|PureBasic}}===
{{trans|C}}
<syntaxhighlight lang="purebasic">EnableExplicit
 
Procedure.b kprime(n.i, k.i)
Define p.i = 2,
f.i = 0
While f < k And p*p <= n
While n % p = 0
n / p
f + 1
Wend
p + 1
Wend
ProcedureReturn Bool(f + Bool(n > 1) = k)
EndProcedure
 
;___main____
If Not OpenConsole("Almost prime")
End -1
EndIf
 
Define i.i,
c.i,
k.i
 
For k = 1 To 5
Print("k = " + Str(k) + ":")
i = 2
c = 0
While c < 10
If kprime(i, k)
Print(RSet(Str(i),4))
c + 1
EndIf
i + 1
Wend
PrintN("")
Next
 
Input()</syntaxhighlight>
{{out}}
<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
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</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{trans|Liberty BASIC}}
<syntaxhighlight lang="vb">for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
 
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
if f = k then kPrime = 0
f = f +1
n = int(n / i)
wend
next i
kPrime = abs(f = k)
end function</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
<syntaxhighlight lang="tinybasic">
REM Almost prime
LET K=1
10 IF K>5 THEN END
PRINT "k = ",K,":"
LET I=2
LET C=0
20 IF C>=10 THEN GOTO 40
LET N=I
GOSUB 500
IF P=0 THEN GOTO 30
PRINT I
LET C=C+1
30 LET I=I+1
GOTO 20
40 LET K=K+1
GOTO 10
 
REM Check if N is a K prime (result: P)
500 LET F=0
LET J=2
510 IF (N/J)*J<>N THEN GOTO 520
IF F=K THEN GOTO 530
LET F=F+1
LET N=N/J
GOTO 510
520 LET J=J+1
IF J<=N THEN GOTO 510
LET P=0
IF F=K THEN LET P=-1
RETURN
530 LET P=0
RETURN
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION iskprime(n, k)
! Check if n (AN) is a k (K) prime
LET f = 0
FOR j = 2 TO an
DO WHILE REMAINDER(an, j) = 0
IF f = k THEN LET iskprime = 0
LET f = f + 1
LET an = INT(an / j)
LOOP
NEXT j
IF (f = k) THEN LET iskprime = 1
END FUNCTION
 
!ALMOST prime
FOR k = 1 TO 5
PRINT "k = "; k; ":";
LET i = 2
LET c = 0
DO WHILE c < 10
LET an = i
IF iskprime(i,k) <> 0 THEN
PRINT USING " ###": i;
LET c = c + 1
END IF
LET i = i + 1
LOOP
PRINT
NEXT k
END</syntaxhighlight>
 
==={{header|uBasic/4tH}}===
{{trans|C}}
<syntaxhighlight lang="text">Local(3)
 
For c@ = 1 To 5
Print "k = ";c@;": ";
 
b@=0
 
For a@ = 2 Step 1 While b@ < 10
If FUNC(_kprime (a@,c@)) Then
b@ = b@ + 1
Print " ";a@;
EndIf
Next
 
Print
Next
 
End
 
_kprime Param(2)
Local(2)
 
d@ = 0
For c@ = 2 Step 1 While (d@ < b@) * ((c@ * c@) < (a@ + 1))
Do While (a@ % c@) = 0
a@ = a@ / c@
d@ = d@ + 1
Loop
Next
Return (b@ = (d@ + (a@ > 1)))</syntaxhighlight>
{{trans|FreeBASIC}}
<syntaxhighlight lang="text">For k = 1 To 5
Print "k = "; k; " : ";
i = 2
c = 0
Do While c < 10
If FUNC(_kPrime(i, k)) Then Print Using "__# "; i; : c = c + 1
i = i + 1
Loop
Print
Next
End
 
_kPrime
Param (2)
Local (2)
c@ = 0
 
For d@ = 2 To a@
Do While (a@ % d@) = 0
If c@ = b@ Then Unloop: Unloop: Return (0)
c@ = c@ + 1
a@ = a@ / d@
Loop
Next
Return (c@ = b@)
</syntaxhighlight>
{{out}}
<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
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
 
0 OK, 0:200</pre>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Class KPrime
Public K As Integer
 
Public Function IsKPrime(number As Integer) As Boolean
Dim primes = 0
Dim p = 2
While p * p <= number AndAlso primes < K
While number Mod p = 0 AndAlso primes < K
number = number / p
primes = primes + 1
End While
p = p + 1
End While
If number > 1 Then
primes = primes + 1
End If
Return primes = K
End Function
 
Public Function GetFirstN(n As Integer) As List(Of Integer)
Dim result As New List(Of Integer)
Dim number = 2
While result.Count < n
If IsKPrime(number) Then
result.Add(number)
End If
number = number + 1
End While
Return result
End Function
End Class
 
Sub Main()
For Each k In Enumerable.Range(1, 5)
Dim kprime = New KPrime With {
.K = k
}
Console.WriteLine("k = {0}: {1}", k, String.Join(" ", kprime.GetFirstN(10)))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<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
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</pre>
 
==={{header|XBasic}}===
{{trans|FreeBASIC}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
' Almost prime
PROGRAM "almostprime"
VERSION "0.0002"
DECLARE FUNCTION Entry()
INTERNAL FUNCTION KPrime(n%%, k%%)
FUNCTION Entry()
FOR k@@ = 1 TO 5
PRINT "k ="; k@@; ":";
i%% = 2
c%% = 0
DO WHILE c%% < 10
IFT KPrime(i%%, k@@) THEN
PRINT FORMAT$(" ###", i%%);
INC c%%
END IF
INC i%%
LOOP
PRINT
NEXT k@@
END FUNCTION
FUNCTION KPrime(n%%, k%%)
f%% = 0
FOR i%% = 2 TO n%%
DO WHILE n%% MOD i%% = 0
IF f%% = k%% THEN RETURN $$FALSE
INC f%%
n%% = n%% \ i%%
LOOP
NEXT i%%
RETURN f%% = k%%
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
==={{header|Yabasic}}===
{{trans|Lua}}
<syntaxhighlight lang="yabasic">// Returns boolean indicating whether n is k-almost prime
sub almostPrime(n, k)
local divisor, count
divisor = 2
while(count < (k + 1) and n <> 1)
if not mod(n, divisor) then
n = n / divisor
count = count + 1
else
divisor = divisor + 1
end if
wend
return count = k
end sub
 
// Generates table containing first ten k-almost primes for given k
sub kList(k, kTab())
local n, i
n = 2^k : i = 1
while(i < 11)
if almostPrime(n, k) then
kTab(i) = n
i = i + 1
end if
n = n + 1
wend
end sub
 
// Main procedure, displays results from five calls to kList()
dim kTab(10)
for k = 1 to 5
print "k = ", k, " : ";
kList(k, kTab())
for n = 1 to 10
print kTab(n), ", ";
next
print "..."
next</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
{{trans|AWK}}
<syntaxhighlight lang="zxbasic">10 FOR k=1 TO 5
20 PRINT k;":";
30 LET c=0: LET i=1
40 IF c=10 THEN GO TO 100
50 LET i=i+1
60 GO SUB 1000
70 IF r THEN PRINT " ";i;: LET c=c+1
90 GO TO 40
100 PRINT
110 NEXT k
120 STOP
1000 REM kprime
1010 LET p=2: LET n=i: LET f=0
1020 IF f=k OR (p*p)>n THEN GO TO 1100
1030 IF n/p=INT (n/p) THEN LET n=n/p: LET f=f+1: GO TO 1030
1040 LET p=p+1: GO TO 1020
1100 LET r=(f+(n>1)=k)
1110 RETURN</syntaxhighlight>
 
{{out}}
<pre>1: 2 3 5 7 11 13 17 19 23 29
2: 4 6 9 10 14 15 21 22 25 26
3: 8 12 18 20 27 28 30 42 44 45
4: 16 24 36 40 54 56 60 81 84 88
5: 32 48 72 80 108 112 120 162 168 176</pre>
 
=={{header|BCPL}}==
{{trans|C}}
<langsyntaxhighlight BCPLlang="bcpl">get "libhdr"
 
let kprime(n, k) = valof
Line 699 ⟶ 1,546:
wrch('*N')
$)
$)</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 711 ⟶ 1,558:
The extra spaces are to ensure it's readable on buggy interpreters that don't include a space after numeric output.
 
<langsyntaxhighlight lang="befunge">1>::48*"= k",,,,02p.":",01v
|^ 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$<|</langsyntaxhighlight>
 
{{out}}
Line 726 ⟶ 1,573:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int kprime(int n, int k)
Line 755 ⟶ 1,602:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 765 ⟶ 1,612:
</pre>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 820 ⟶ 1,667:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 832 ⟶ 1,679:
=={{header|C++}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <iostream>
#include <sstream>
Line 863 ⟶ 1,710:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 873 ⟶ 1,720:
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="clojure">
 
(ns clojure.examples.almostprime
Line 897 ⟶ 1,744:
(println "k:" k (divisors-k k 10))))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 907 ⟶ 1,754:
nil
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">kprime = proc (n,k: int) returns (bool)
f: int := 0
p: int := 2
while f<k & p*p<=n do
while n//p=0 do
n := n/p
f := f+1
end
p := p+1
end
if n>1 then f:=f+1 end
return(f=k)
end kprime
 
start_up = proc ()
po: stream := stream$primary_output()
for k: int in int$from_to(1,5) do
i: int := 2
c: int := 0
stream$puts(po, "k = " || int$unparse(k) || ":")
while c<10 do
if kprime(i,k) then
stream$putright(po, int$unparse(i), 4)
c := c+1
end
i := i+1
end
stream$putl(po, "")
end
end start_up</syntaxhighlight>
{{out}}
<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
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</pre>
 
=={{header|COBOL}}==
{{trans|C}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ALMOST-PRIME.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CONTROL-VARS.
03 K PIC 9.
03 I PIC 999.
03 SEEN PIC 99.
03 N PIC 999.
03 P PIC 99.
03 P-SQUARED PIC 9(4).
03 F PIC 99.
03 N-DIV-P PIC 999V999.
03 FILLER REDEFINES N-DIV-P.
05 NEXT-N PIC 999.
05 FILLER PIC 999.
88 N-DIVS-P VALUE ZERO.
01 OUT-VARS.
03 K-LN PIC X(70).
03 K-LN-PTR PIC 99.
03 LN-HDR.
05 FILLER PIC X(4) VALUE "K = ".
05 K-OUT PIC 9.
05 FILLER PIC X VALUE ":".
03 I-FMT.
05 FILLER PIC X VALUE SPACE.
05 I-OUT PIC ZZ9.
PROCEDURE DIVISION.
BEGIN.
PERFORM K-ALMOST-PRIMES VARYING K FROM 1 BY 1
UNTIL K IS GREATER THAN 5.
STOP RUN.
K-ALMOST-PRIMES.
MOVE SPACES TO K-LN.
MOVE 1 TO K-LN-PTR.
MOVE ZERO TO SEEN.
MOVE K TO K-OUT.
STRING LN-HDR DELIMITED BY SIZE INTO K-LN
WITH POINTER K-LN-PTR.
PERFORM I-K-ALMOST-PRIME VARYING I FROM 2 BY 1
UNTIL SEEN IS EQUAL TO 10.
DISPLAY K-LN.
I-K-ALMOST-PRIME.
MOVE ZERO TO F, P-SQUARED.
MOVE I TO N.
PERFORM PRIME-FACTOR VARYING P FROM 2 BY 1
UNTIL F IS NOT LESS THAN K
OR P-SQUARED IS GREATER THAN N.
IF N IS GREATER THAN 1, ADD 1 TO F.
IF F IS EQUAL TO K,
MOVE I TO I-OUT,
ADD 1 TO SEEN,
STRING I-FMT DELIMITED BY SIZE INTO K-LN
WITH POINTER K-LN-PTR.
PRIME-FACTOR.
MULTIPLY P BY P GIVING P-SQUARED.
DIVIDE N BY P GIVING N-DIV-P.
PERFORM DIVIDE-FACTOR UNTIL NOT N-DIVS-P.
DIVIDE-FACTOR.
MOVE NEXT-N TO N.
ADD 1 TO F.
DIVIDE N BY P GIVING N-DIV-P.</syntaxhighlight>
{{out}}
<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
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</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
Line 921 ⟶ 1,884:
(cond ((> d (isqrt n)) (+ c 1))
((zerop (rem n d)) (?-primality (/ n d) d (+ c 1)))
(t (?-primality n (+ d 1) c))))</langsyntaxhighlight>
{{out}}
<pre>
Line 933 ⟶ 1,896:
=={{header|Cowgol}}==
{{trans|C}}
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub kprime(n: uint8, k: uint8): (kp: uint8) is
Line 973 ⟶ 1,936:
print_nl();
k := k + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 984 ⟶ 1,947:
This contains a copy of the function <code>decompose</code> from the Prime decomposition task.
{{trans|Ada}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.traits;
 
Unqual!T[] decompose(T)(in T number) pure nothrow
Line 1,019 ⟶ 1,982:
writeln;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,031 ⟶ 1,994:
{{trans|C}}
 
<syntaxhighlight lang="delphi">
<lang Delphi>
program AlmostPrime;
 
Line 1,072 ⟶ 2,035:
end;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,080 ⟶ 2,043:
K = 5: 32 48 72 80 108 112 120 162 168 176
</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec kprime(word n, k) bool:
word f, p;
f := 0;
p := 2;
while f < k and p*p <= n do
while n%p = 0 do
n := n/p;
f := f+1
od;
p := p+1
od;
if n>1 then f+1 = k
else f = k
fi
corp
 
proc nonrec main() void:
byte k, i, c;
for k from 1 upto 5 do
write("k = ", k:1, ":");
i := 2;
c := 0;
while c < 10 do
if kprime(i,k) then
write(i:4);
c := c+1
fi;
i := i+1
od;
writeln()
od
corp</syntaxhighlight>
{{out}}
<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
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</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=easylang>
func kprime n k .
i = 2
while i <= n
while n mod i = 0
if f = k
return 0
.
f += 1
n /= i
.
i += 1
.
if f = k
return 1
.
return 0
.
for k = 1 to 5
write "k=" & k & " : "
i = 2
c = 0
while c < 10
if kprime i k = 1
write i & " "
c += 1
.
i += 1
.
print ""
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Small numbers : filter the sequence [ 2 .. n]
<langsyntaxhighlight lang="scheme">
(define (almost-prime? p k)
(= k (length (prime-factors p))))
Line 1,095 ⟶ 2,133:
(for-each write (almost-primes k nmax))
(writeln)))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(task)
 
Line 1,105 ⟶ 2,143:
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>
</lang>
Large numbers : generate - combinations with repetitions - k-almost-primes up to pmax.
<langsyntaxhighlight lang="scheme">
(lib 'match)
(define-syntax-rule (: v i) (vector-ref v i))
Line 1,158 ⟶ 2,196:
almost-prime)))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang="scheme">
;; we want 500-almost-primes from the 10000-th.
(take (drop (list-sort < (almost-primes 500 10000)) 10000 ) 10)
Line 1,171 ⟶ 2,209:
;; The first one is 2^497 * 3 * 17 * 347 , same result as Haskell.
 
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule Factors do
def factors(n), do: factors(n,2,[])
Line 1,197 ⟶ 2,235:
end
 
Factors.kfactors(10,5)</langsyntaxhighlight>
 
{{out}}
Line 1,211 ⟶ 2,249:
Using the factors function from [[Prime_decomposition#Erlang]].
 
<langsyntaxhighlight lang="erlang">
-module(factors).
-export([factors/1,kfactors/0,kfactors/2]).
Line 1,236 ⟶ 2,274:
_ ->
kfactors(Tn,Tk, N+1,K, Acc) end.
</langsyntaxhighlight>
 
{{out}}
Line 1,262 ⟶ 2,300:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM ALMOST_PRIME
 
Line 1,299 ⟶ 2,337:
END FOR
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,308 ⟶ 2,346:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let rec genFactor (f, n) =
if f > n then None
elif n % f = 0 then Some (f, (f, n/f))
Line 1,326 ⟶ 2,364:
[1 .. 5]
|> List.iter (fun k ->
printfn "%A" (Seq.take 10 (kFactors k) |> Seq.toList))</langsyntaxhighlight>
{{out}}
<pre>[2; 3; 5; 7; 11; 13; 17; 19; 23; 29]
Line 1,335 ⟶ 2,373:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry kernel lists lists.lazy locals
math.combinatorics math.primes.factors math.ranges sequences ;
IN: rosetta-code.almost-prime
Line 1,346 ⟶ 2,384:
10 0 lfrom [ k k-almost-prime? ] lfilter ltake list>array ;
5 [1,b] [ dup first10 "K = %d: %[%3d, %]\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,358 ⟶ 2,396:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 F K=1,5;D 3
01.20 Q
 
Line 1,377 ⟶ 2,415:
03.60 S I=I+1
03.70 I (C-10)3.3
03.80 T !</langsyntaxhighlight>
{{out}}
<pre>K= 1: = 2 = 3 = 5 = 7 = 11 = 13 = 17 = 19 = 23 = 29
Line 1,386 ⟶ 2,424:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">
program almost_prime
use iso_fortran_env, only: output_unit
Line 1,432 ⟶ 2,470:
end function kprime
end program almost_prime
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,441 ⟶ 2,479:
k = 4 : 16 24 36 40 54 56 60 81 84 88
k = 5 : 32 48 72 80 108 112 120 162 168 176
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Function kPrime(n As Integer, k As Integer) As Boolean
Dim f As Integer = 0
For i As Integer = 2 To n
While n Mod i = 0
If f = k Then Return false
f += 1
n \= i
Wend
Next
Return f = k
End Function
Dim As Integer i, c, k
For k = 1 To 5
Print "k = "; k; " : ";
i = 2
c = 0
While c < 10
If kPrime(i, k) Then
Print Using "### "; i;
c += 1
End If
i += 1
Wend
Print
Next
 
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<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
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
</pre>
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">for k = 1 to 5
{
n=2
Line 1,503 ⟶ 2,498:
 
println[]
}</langsyntaxhighlight>
 
Output:
Line 1,516 ⟶ 2,511:
=={{header|Futhark}}==
 
<syntaxhighlight lang="futhark">
<lang Futhark>
let kprime(n: i32, k: i32): bool =
let (p,f) = (2, 0)
Line 1,535 ⟶ 2,530:
in ps
in map f (1...m)
</syntaxhighlight>
</lang>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn K_Prime( n as long, k as long ) as BOOL
long f = 0, i = 0
BOOL result
for i = 2 to n
while ( n mod i == 0 )
if f = k then exit fn = NO
f += 1
n /= i
wend
next
result = f = k
end fn = result
 
 
long i, c, k
 
for k = 1 to 5
printf @"k = %ld:\b", k
i = 2
c = 0
while ( c < 10 )
if ( fn K_Prime( i, k ) )
printf @"%4ld\b", i
c++
end if
i++
wend
print
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<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
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
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,573 ⟶ 2,613:
fmt.Println(k, gen(k, 10))
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,584 ⟶ 2,624:
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
public class almostprime
{
Line 1,619 ⟶ 2,659:
}
}​
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,632 ⟶ 2,672:
k = 5:
32 48 72 80 108 112 120 162 168 176
</pre>
 
=={{header|GW-BASIC}}==
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
<lang qbasic>
10 'Almost prime
20 FOR K% = 1 TO 5
30 PRINT "k = "; K%; ": ";
40 LET I% = 2
50 LET C% = 0
60 WHILE C% < 10
70 LET AN% = I%: LET AK% = K%: GOSUB 1000
80 IF ISKPRIME <> 0 THEN PRINT USING "### "; I%;: LET C% = C% + 1
90 LET I% = I% + 1
100 WEND
110 PRINT
120 NEXT K%
130 END
 
995 ' Check if n (AN%) is a k (AK%) prime
1000 LET F% = 0
1010 FOR II% = 2 TO AN%
1020 WHILE AN% MOD II% = 0
1030 IF F% = AK% THEN LET ISKPRIME = 0: RETURN
1040 LET F% = F% + 1
1050 LET AN% = AN% \ II%
1060 WEND
1070 NEXT II%
1080 LET ISKPRIME = (F% = AK%)
1090 RETURN
</lang>
{{out}}
<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
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
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">isPrime :: Integral a => a -> Bool
isPrime n = not $ any ((0 ==) . (mod n)) [2..(truncate $ sqrt $ fromIntegral n)]
 
Line 1,691 ⟶ 2,692:
main :: IO ()
main = flip mapM_ [1..5] $ \k ->
putStrLn $ "k = " ++ show k ++ ": " ++ (unwords $ map show (take 10 $ kPrimes k))</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,700 ⟶ 2,701:
 
Larger ''k''s require more complicated methods:
<langsyntaxhighlight lang="haskell">primes = 2:3:[n | n <- [5,7..], foldr (\p r-> p*p > n || rem n p > 0 && r)
True (drop 1 primes)]
 
Line 1,726 ⟶ 2,727:
 
putStrLn "\n10000th to 10100th 500-amost primes:"
mapM_ print $ take 100 $ drop 10000 $ kprimes 500</langsyntaxhighlight>
{{out}}
<pre>
Line 1,743 ⟶ 2,744:
 
Works in both languages.
<langsyntaxhighlight lang="unicon">link "factors"
 
procedure main()
Line 1,752 ⟶ 2,753:
procedure genKap(k)
suspend (k = *factors(n := seq(q)), n)
end</langsyntaxhighlight>
 
Output:
Line 1,763 ⟶ 2,764:
5: 32 48 72 80 108 112 120 162 168 176
->
</pre>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(function prime-sieve search siever sieved
(return-when (empty? siever) (.. vec sieved search))
(let [p ps] ((juxt 0 (skip 1)) siever))
(recur (remove #(div? % p) search)
(remove #(div? % p) ps)
(append p sieved)))
 
(function primes n
(prime-sieve (range 2 (inc n)) (range 2 (ceil (sqrt n))) []))
 
(function decompose n ps factors
(return-when (= n 1) factors)
(let div (find (div? n) ps))
(recur (/ n div) ps (append div factors)))
 
(function almost-prime up-to n k
(return-when (zero? up-to) [])
(let ps (primes n))
(if (= k (len (decompose n ps [])))
(prepend n (almost-prime (dec up-to) (inc n) k))
(almost-prime up-to (inc n) k)))
 
(function row n
(-> n
@(almost-prime 10 1)
(join " ")
@(str n (match n 1 "st" 2 "nd" 3 "rd" "th") " almost-primes: " )))
 
(join "\n" (map row (range 1 6)))
</syntaxhighlight>
 
{{out}}
 
<pre>
1st almost-primes: 2 3 5 7 11 13 17 19 23 29
2nd almost-primes: 4 6 9 10 14 15 21 22 25 26
3rd almost-primes: 8 12 18 20 27 28 30 42 44 45
4th almost-primes: 16 24 36 40 54 56 60 81 84 88
5th almost-primes: 32 48 72 80 108 112 120 162 168 176
</pre>
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> (10 {. [:~.[:/:~[:,*/~)^:(i.5)~p:i.10
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</langsyntaxhighlight>
Explanation:
#Generate 10 primes.
Line 1,782 ⟶ 2,827:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
Line 1,808 ⟶ 2,853:
return f + ((n > 1) ? 1 : 0) == k;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,819 ⟶ 2,864:
 
=={{header|Javascript}}==
<langsyntaxhighlight lang="javascript">function almostPrime (n, k) {
var divisor = 2, count = 0
while(count < k + 1 && n != 1) {
Line 1,842 ⟶ 2,887:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,854 ⟶ 2,899:
{{Works with| jq|1.4}}
'''Infrastructure:'''
<langsyntaxhighlight lang="jq"># Recent versions of jq (version > 1.4) have the following definition of "until":
def until(cond; next):
def _until:
Line 1,937 ⟶ 2,982:
if ($in % $p) == 0 then . + [$in|multiplicity($p)] else . end )
end
end;</langsyntaxhighlight>
'''isalmostprime'''
<langsyntaxhighlight lang="jq">def isalmostprime(k): (prime_factors_with_multiplicities | length) == k;
 
# Emit a stream of the first N almost-k primes
Line 1,952 ⟶ 2,997:
end)
| .[2] | select(. != null)
end;</langsyntaxhighlight>
'''The task:'''
<langsyntaxhighlight lang="jq">range(1;6) as $k | "k=\($k): \([almostprimes(10;$k)])"</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -c -r -n -f Almost_prime.jq
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]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|1.1}}
<langsyntaxhighlight lang="julia">using Primes
 
isalmostprime(n::Integer, k::Integer) = sum(values(factor(n))) == k
Line 1,981 ⟶ 3,026:
for k in 1:5
println("$k-Almost-primes: ", join(almostprimes(10, k), ", "), "...")
end</langsyntaxhighlight>
 
{{out}}
Line 1,992 ⟶ 3,037:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">fun Int.k_prime(x: Int): Boolean {
var n = x
var f = 0
Line 2,016 ⟶ 3,061:
for (k in 1..5)
println("k = $k: " + k.primes(10))
}</langsyntaxhighlight>
{{out}}
<pre>k = 1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 2,023 ⟶ 3,068:
k = 4: [16, 24, 36, 40, 54, 56, 60, 81, 84, 88]
k = 5: [32, 48, 72, 80, 108, 112, 120, 162, 168, 176]</pre>
 
=={{header|Liberty BASIC}}==
{{trans|FreeBASIC}}
{{works with|Just BASIC}}
<lang lb>
for k = 1 to 5
print "k = "; k; ": ";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print using("###", i); " ";
c = c + 1
end if
i = i + 1
wend
print
next k
end
 
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
if f = k then kPrime = 0: exit function
f = f + 1
n = int(n / i)
wend
next i
kPrime = abs(f = k)
end function
</lang>
{{out}}
<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
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
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Returns boolean indicating whether n is k-almost prime
function almostPrime (n, k)
local divisor, count = 2, 0
Line 2,098 ⟶ 3,103:
end
print("...")
end</langsyntaxhighlight>
{{out}}
<pre>k=1: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ...
Line 2,107 ⟶ 3,112:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">AlmostPrimes:=proc(k, numvalues::posint:=10)
local aprimes, i, intfactors;
aprimes := Array([]);
Line 2,122 ⟶ 3,127:
aprimes;
end proc:
<seq( AlmostPrimes(i), i = 1..5 )>;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,132 ⟶ 3,137:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(NN,KK)
Line 2,167 ⟶ 3,172:
0 KPR(C*5+4), KPR(C*5+5)
 
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre> K=1 K=2 K=3 K=4 K=5
Line 2,182 ⟶ 3,187:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">kprimes[k_,n_] :=
(* generates a list of the n smallest k-almost-primes *)
Module[{firstnprimes, runningkprimes = {}},
Line 2,195 ⟶ 3,200:
]
(* 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</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,203 ⟶ 3,208:
k = 5: 32 48 72 80 108 112 120 162 168 176</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Predicate function that checks k-almost primality for given integer n and parameter k */
k_almost_primep(n,k):=if integerp((n)^(1/k)) and primep((n)^(1/k)) then true else
lambda([x],(length(ifactors(x))=k and unique(map(second,ifactors(x)))=[1]) or (length(ifactors(x))<k and apply("+",map(second,ifactors(x)))=k))(n)$
 
/* Function that given a parameter k1 returns the first len k1-almost primes */
k_almost_prime_count(k1,len):=block(
count:len,
while length(sublist(makelist(i,i,count),lambda([x],k_almost_primep(x,k1))))<len do (count:count+1),
sublist(makelist(i,i,count),lambda([x],k_almost_primep(x,k1))))$
 
/* Test cases */
k_almost_prime_count(1,10);
k_almost_prime_count(2,10);
k_almost_prime_count(3,10);
k_almost_prime_count(4,10);
k_almost_prime_count(5,10);
</syntaxhighlight>
{{out}}
<pre>
[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]
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
primeFactory = function(n=2)
if n < 2 then return ""
for i in range(2, n)
p = floor(n / i)
q = n % i
if not q then return str(i) + " " + str(primeFactory(p))
end for
return n
end function
 
getAlmostPrimes = function(k)
almost = []
n = 2
while almost.len < 10
primes = primeFactory(n).trim.split
if primes.len == k then almost.push(n)
n += 1
end while
return almost
end function
 
for i in range(1, 5)
print i + ": " + getAlmostPrimes(i)
end for
</syntaxhighlight>
 
{{out}}
<pre>
]run
1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
2: [4, 6, 9, 10, 14, 15, 21, 22, 25, 26]
3: [8, 12, 18, 20, 27, 28, 30, 42, 44, 45]
4: [16, 24, 36, 40, 54, 56, 60, 81, 84, 88]
5: [32, 48, 72, 80, 108, 112, 120, 162, 168, 176]
</pre>
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE AlmostPrime;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,249 ⟶ 3,319:
 
ReadChar;
END AlmostPrime.</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">proc prime(k: int, listLen: int): seq[int] =
result = @[]
var
Line 2,274 ⟶ 3,344:
for k in 1..5:
echo "k = ",k," : ",prime(k,10)</langsyntaxhighlight>
{{out}}
<pre>k = 1 : @[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 2,284 ⟶ 3,354:
=={{header|Objeck}}==
{{trans|C}}
<langsyntaxhighlight lang="objeck">class Kth_Prime {
function : native : kPrime(n : Int, k : Int) ~ Bool {
f := 0;
Line 2,310 ⟶ 3,380:
};
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,319 ⟶ 3,389:
k = 4: 16 24 36 40 54 56 60 81 84 88
k = 5: 32 48 72 80 108 112 120 162 168 176</pre>
 
=={{header|Odin}}==
<syntaxhighlight lang="Go">
package almostprime
import "core:fmt"
main :: proc() {
i, c, k: int
for k in 1 ..= 5 {
fmt.printf("k = %d:", k)
for i, c := 2, 0; c < 10; i += 1 {
if kprime(i, k) {
fmt.printf(" %v", i)
c += 1
}
}
fmt.printf("\n")
}
}
kprime :: proc(n: int, k: int) -> bool {
p, f: int = 0, 0
n := n
for p := 2; f < k && p * p <= n; p += 1 {
for (0 == n % p) {
n /= p
f += 1
}
}
return f + (n > 1 ? 1 : 0) == k
}
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: kprime?( n k -- b )
| i |
0 2 n for: i [
Line 2,335 ⟶ 3,443:
2 while (l size 10 <>) [ dup k kprime? if dup l add then 1+ ]
drop
;</langsyntaxhighlight>
 
{{out}}
Line 2,345 ⟶ 3,453:
[16, 24, 36, 40, 54, 56, 60, 81, 84, 88]
[32, 48, 72, 80, 108, 112, 120, 162, 168, 176]
</pre>
 
=={{header|Onyx (wasm)}}==
===procedural===
<syntaxhighlight lang="ts">
package main
use core {printf}
main :: () -> void {
printf("\n");
for k in 1..6 {
printf("k = {}:", k);
i := 2;
c: i32;
while c < 10 {
if kprime(i, k) {
printf(" {}", i);
c += 1;
}
i += 1;
}
printf("\n");
}
}
kprime :: (n: i32, k: i32) -> bool {
f: i32;
while p := 2; f < k && p * p <= n {
while n % p == 0 {
n /= p;
f += 1;
}
p += 1;
}
return f + (1 if n > 1 else 0) == k;
}
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
===functional===
<syntaxhighlight lang="TS">
//+optional-semicolons
use core {printf}
use core.iter
 
main :: () {
generator :=
iter.counter(1)
|> iter.map(k => .{
k = k, kprimes = kprime_iter(k)->take(10)
})
|> iter.take(5)
 
for val in generator {
printf("k = {}:", val.k)
for p in val.kprimes do printf(" {}", p)
printf("\n")
}
}
 
kprime_iter :: k =>
iter.counter(2)
|> iter.filter((i, [k]) => kprime(i, k))
 
kprime :: (n, k) => {
f := 0
for p in iter.counter(2) {
if f >= k do break
if p * p > n do break
 
while n % p == 0 {
n /= p
f += 1
}
}
 
return f + (1 if n > 1 else 0) == k
}
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">almost(k)=my(n); for(i=1,10,while(bigomega(n++)!=k,); print1(n", "));
for(k=1,5,almost(k);print)</langsyntaxhighlight>
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
Line 2,360 ⟶ 3,559:
{{libheader|primTrial}}
{{works with|Free Pascal}}
<langsyntaxhighlight Pascallang="pascal">program AlmostPrime;
{$IFDEF FPC}
{$Mode Delphi}
Line 2,385 ⟶ 3,584:
inc(k);
until k > 10;
END.</langsyntaxhighlight>
;output:
<pre>K= 1 : 2 3 5 7 11 13 17 19 23 29
Line 2,401 ⟶ 3,600:
Using a CPAN module, which is simple and fast:
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/factor/;
sub almost {
my($k,$n) = @_;
Line 2,407 ⟶ 3,606:
map { $i++ while scalar factor($i) != $k; $i++ } 1..$n;
}
say "$_ : ", join(" ", almost($_,10)) for 1..5;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,417 ⟶ 3,616:
</pre>
or writing everything by hand:
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 2,480 ⟶ 3,679:
return $primes[$n];
}
}</langsyntaxhighlight>
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29
Line 2,488 ⟶ 3,687:
32, 48, 72, 80, 108, 112, 120, 162, 168, 176
</pre>
 
=={{header|Phixmonti}}==
{{trans|OForth}}
<syntaxhighlight lang="phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Almost_prime
by Galileo, 06/2022 #/
 
include ..\Utilitys.pmt
 
def test tps over mod not enddef
 
def kprime?
>ps >ps
0 ( 2 tps ) for
test while
tps over / int ps> drop >ps
swap 1 + swap
test endwhile
drop
endfor
ps> drop
ps> ==
enddef
 
5 for >ps
2 ( )
len 10 < while over tps kprime? if over 0 put endif swap 1 + swap len 10 < endwhile
nip ps> drop
endfor
 
pstack</syntaxhighlight>
{{out}}
<pre>
[[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]]
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="php">
<?php
// Almost prime
 
function isKPrime($n, $k)
{
$f = 0;
for ($j = 2; $j <= $n; $j++) {
while ($n % $j == 0) {
if ($f == $k)
return false;
$f++;
$n = floor($n / $j);
} // while
} // for $j
return ($f == $k);
}
 
for ($k = 1; $k <= 5; $k++) {
echo "k = ", $k, ":";
$i = 2;
$c = 0;
while ($c < 10) {
if (isKPrime($i, $k)) {
echo " ", str_pad($i, 3, ' ', STR_PAD_LEFT);
$c++;
}
$i++;
}
echo PHP_EOL;
}
?>
</syntaxhighlight>
{{out}}
<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
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
</pre>
 
=={{header|Picat}}==
{{trans|J}}
<syntaxhighlight lang="picat">go =>
N = 10,
Ps = primes(100).take(N),
println(1=Ps),
T = Ps,
foreach(K in 2..5)
T := mul_take(Ps,T,N),
println(K=T)
end,
nl,
foreach(K in 6..25)
T := mul_take(Ps,T,N),
println(K=T)
end,
nl.
 
% take first N values of L1 x L2
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].</syntaxhighlight>
 
{{out}}
<pre>1 = [2,3,5,7,11,13,17,19,23,29]
2 = [4,6,9,10,14,15,21,22,25,26]
3 = [8,12,18,20,27,28,30,42,44,45]
4 = [16,24,36,40,54,56,60,81,84,88]
5 = [32,48,72,80,108,112,120,162,168,176]
 
6 = [64,96,144,160,216,224,240,324,336,352]
7 = [128,192,288,320,432,448,480,648,672,704]
8 = [256,384,576,640,864,896,960,1296,1344,1408]
9 = [512,768,1152,1280,1728,1792,1920,2592,2688,2816]
10 = [1024,1536,2304,2560,3456,3584,3840,5184,5376,5632]
11 = [2048,3072,4608,5120,6912,7168,7680,10368,10752,11264]
12 = [4096,6144,9216,10240,13824,14336,15360,20736,21504,22528]
13 = [8192,12288,18432,20480,27648,28672,30720,41472,43008,45056]
14 = [16384,24576,36864,40960,55296,57344,61440,82944,86016,90112]
15 = [32768,49152,73728,81920,110592,114688,122880,165888,172032,180224]
16 = [65536,98304,147456,163840,221184,229376,245760,331776,344064,360448]
17 = [131072,196608,294912,327680,442368,458752,491520,663552,688128,720896]
18 = [262144,393216,589824,655360,884736,917504,983040,1327104,1376256,1441792]
19 = [524288,786432,1179648,1310720,1769472,1835008,1966080,2654208,2752512,2883584]
20 = [1048576,1572864,2359296,2621440,3538944,3670016,3932160,5308416,5505024,5767168]
21 = [2097152,3145728,4718592,5242880,7077888,7340032,7864320,10616832,11010048,11534336]
22 = [4194304,6291456,9437184,10485760,14155776,14680064,15728640,21233664,22020096,23068672]
23 = [8388608,12582912,18874368,20971520,28311552,29360128,31457280,42467328,44040192,46137344]
24 = [16777216,25165824,37748736,41943040,56623104,58720256,62914560,84934656,88080384,92274688]
25 = [33554432,50331648,75497472,83886080,113246208,117440512,125829120,169869312,176160768,184549376]</pre>
 
=={{header|PL/I}}==
{{trans|C}}
<syntaxhighlight lang="pli">almost_prime: procedure options(main);
kprime: procedure(nn, k) returns(bit);
declare (n, nn, k, p, f) fixed;
f = 0;
n = nn;
do p=2 repeat(p+1) while(f<k & p*p <= n);
do n=n repeat(n/p) while(mod(n,p) = 0);
f = f+1;
end;
end;
return(f + (n>1) = k);
end kprime;
declare (i, c, k) fixed;
do k=1 to 5;
put edit('k = ',k,':') (A,F(1),A);
c = 0;
do i=2 repeat(i+1) while(c<10);
if kprime(i,k) then do;
put edit(i) (F(4));
c = c+1;
end;
end;
put skip;
end;
end almost_prime;</syntaxhighlight>
{{out}}
<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
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</pre>
 
=={{header|PL/M}}==
{{trans|C}}
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 2,542 ⟶ 3,906:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,551 ⟶ 3,915:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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 2,566 ⟶ 3,930:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,577 ⟶ 3,941:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de factor (N)
(make
(let
Line 2,603 ⟶ 3,967:
(println I '-> (almost I) ) )
 
(bye)</langsyntaxhighlight>
 
=={{header|Potion}}==
<langsyntaxhighlight lang="potion"># Converted from C
kprime = (n, k):
p = 2, f = 0
Line 2,625 ⟶ 3,989:
i++
.
"" say.</langsyntaxhighlight>
 
C and Potion take 0.006s, Perl5 0.028s
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">% almostPrime(K, +Take, List) succeeds if List can be unified with the
% first Take K-almost-primes.
% Notice that K need not be specified.
Line 2,650 ⟶ 4,014:
sort(Long, Sorted), % uniquifies
take(Take, Sorted, List).
</langsyntaxhighlight>That's it. The rest is machinery. For portability, a compatibility section is included below.
<langsyntaxhighlight Prologlang="prolog">nPrimes( M, Primes) :- nPrimes( [2], M, Primes).
 
nPrimes( Accumulator, I, Primes) :-
Line 2,691 ⟶ 4,055:
length(Head, N),
append(Head,X,List).
</syntaxhighlight>
</lang>
<langsyntaxhighlight Prologlang="prolog">%%%%% compatibility section %%%%%
 
:- if(current_prolog_flag(dialect, yap)).
Line 2,720 ⟶ 4,084:
between(Min, Max, I).
:- endif.
</syntaxhighlight>
</lang>
Example using SWI-Prolog:<pre>
?- between(1,5,I),
Line 2,737 ⟶ 4,101:
 
=={{header|Processing}}==
<langsyntaxhighlight lang="processing">void setup() {
for (int i = 1; i <= 5; i++) {
int count = 0;
Line 2,773 ⟶ 4,137:
}
return count;
}</langsyntaxhighlight>
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,780 ⟶ 4,144:
k = 4: 16 24 36 40 54 56 60 81 84 88
k = 5: 32 48 72 80 108 112 120 162 168 176</pre>
 
=={{header|PureBasic}}==
{{trans|C}}
<lang PureBasic>EnableExplicit
 
Procedure.b kprime(n.i, k.i)
Define p.i = 2,
f.i = 0
While f < k And p*p <= n
While n % p = 0
n / p
f + 1
Wend
p + 1
Wend
ProcedureReturn Bool(f + Bool(n > 1) = k)
EndProcedure
 
;___main____
If Not OpenConsole("Almost prime")
End -1
EndIf
 
Define i.i,
c.i,
k.i
 
For k = 1 To 5
Print("k = " + Str(k) + ":")
i = 2
c = 0
While c < 10
If kprime(i, k)
Print(RSet(Str(i),4))
c + 1
EndIf
i + 1
Wend
PrintN("")
Next
 
Input()</lang>
{{out}}
<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
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</pre>
 
=={{header|Python}}==
This imports [[Prime decomposition#Python]]
<langsyntaxhighlight lang="python">from prime_decomposition import decompose
from itertools import islice, count
try:
Line 2,853 ⟶ 4,165:
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))))</langsyntaxhighlight>
{{out}}
<pre>1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 2,866 ⟶ 4,178:
 
 
<langsyntaxhighlight lang="python">
# k-Almost-primes
# Python 3.6.3
# no imports
# author: manuelcaeiro | https://github.com/manuelcaeiro
 
def prime_factors(m=2):
Line 2,904 ⟶ 4,217:
# try:
#k_almost_primes(6000, 10)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,913 ⟶ 4,226:
k=4: [16, 24, 36, 40, 54, 56, 60, 81, 84, 88]
k=5: [32, 48, 72, 80, 108, 112, 120, 162, 168, 176]
</pre>
 
=={{header|Quackery}}==
 
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
 
<syntaxhighlight lang="quackery"> [ stack ] is quantity ( --> s )
[ stack ] is factors ( --> s )
 
[ factors put
quantity put
[] 1
[ over size
quantity share != while
1+ dup primefactors
size factors share = if
[ tuck join swap ]
again ]
drop
factors release
quantity release ] is almostprimes ( n n --> [ )
 
5 times
[ 10 i^ 1+ dup echo sp
almostprimes echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre>1 [ 2 3 5 7 11 13 17 19 23 29 ]
2 [ 4 6 9 10 14 15 21 22 25 26 ]
3 [ 8 12 18 20 27 28 30 42 44 45 ]
4 [ 16 24 36 40 54 56 60 81 84 88 ]
5 [ 32 48 72 80 108 112 120 162 168 176 ]
</pre>
 
=={{header|R}}==
This uses the function from [[Prime decomposition#R]]
<langsyntaxhighlight lang="rsplus">#===============================================================
# Find k-Almost-primes
# R implementation
Line 2,965 ⟶ 4,311:
}
print(res)
}</langsyntaxhighlight>
 
{{out}}
Line 2,978 ⟶ 4,324:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(require (only-in math/number-theory factorize))
 
Line 3,002 ⟶ 4,348:
"\n"))
 
(displayln (format-table KAP-table-values))</langsyntaxhighlight>
 
{{out}}
Line 3,015 ⟶ 4,361:
{{trans|C}}
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku" perl6line>sub is-k-almost-prime($n is copy, $k) returns Bool {
loop (my ($p, $f) = 2, 0; $f < $k && $p*$p <= $n; $p++) {
$n /= $p, $f++ while $n %% $p;
Line 3,025 ⟶ 4,371:
say ~.[^10]
given grep { is-k-almost-prime($_, $k) }, 2 .. *
}</langsyntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29
Line 3,034 ⟶ 4,380:
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).
 
<syntaxhighlight lang="raku" perl6line>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;
 
multi sub factors(1) { 1 }
Line 3,056 ⟶ 4,402:
sub almost($n) { map *.key, grep *.value == $n, @factory }
 
put almost($_)[^10] for 1..5;</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 3,063 ⟶ 4,409:
 
The first three &nbsp; '''k-almost''' &nbsp; primes for each &nbsp; '''K''' &nbsp; group are computed directly &nbsp; (rather than found).
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
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,097 ⟶ 4,443:
 
if f==0 then return 1 /*if prime (f==0), then return unity.*/
return f /*return to invoker the number of divs.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,134 ⟶ 4,480:
 
Once the required primes are generated, the finding of the K─almost primes is almost instantaneous.
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
parse arg N K . /*obtain optional arguments from the CL*/
if N=='' | N==',' then N=10 /*N not specified? Then use default.*/
Line 3,199 ⟶ 4,545:
#=#+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.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; <tt> &nbsp; 20 &nbsp; 16 </tt>}}
<pre>
Line 3,223 ⟶ 4,569:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
for ap = 1 to 5
see "k = " + ap + ":"
Line 3,257 ⟶ 4,603:
next
return 1
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,265 ⟶ 4,611:
k = 4: 16 24 36 40 54 56 60 81 84 88
k = 5: 32 48 72 80 108 112 120 162 168 176
</pre>
 
=={{header|RPL}}==
{{trans|FreeBASIC}}
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → k
≪ 0 1 SF
2 3 PICK '''FOR''' j
'''WHILE''' OVER j MOD NOT '''REPEAT'''
'''IF''' DUP k == '''THEN''' 1 CF OVER 'j' STO '''END'''
1 +
SWAP j / SWAP
'''END'''
'''NEXT'''
k == 1 FS? AND SWAP DROP
≫ ≫ '<span style="color:blue">KPRIM</span>' STO
≪ 5 1 '''FOR''' k
{ } 2
'''WHILE''' OVER SIZE 10 < '''REPEAT'''
'''IF''' DUP k <span style="color:blue">KPRIM</span> '''THEN''' SWAP OVER + SWAP '''END'''
1 + '''END''' DROP
-1 '''STEP'''
≫ '<span style="color:blue">TASK</span>' STO
|
<span style="color:blue">KPRIM</span> ''( n k → boolean ) ''
Dim f As Integer = 0
For i As Integer = 2 To n
While n Mod i = 0
If f = k Then Return false
f += 1
n \= i
Wend
Next
Return f = k
End Function
|}
{{out}}
<pre>
5 : { 32 48 72 80 108 112 120 162 168 176 }
4 : { 16 24 36 40 54 56 60 81 84 88 }
3 : { 8 12 18 20 27 28 30 42 44 45 }
2 : { 4 6 9 10 14 15 21 22 25 26 }
1 : { 2 3 5 7 9 11 13 17 19 23 29 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
def almost_primes(k=2)
Line 3,275 ⟶ 4,678:
end
 
(1..5).each{|k| puts almost_primes(k).take(10).join(", ")}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,286 ⟶ 4,689:
 
{{trans|J}}
<langsyntaxhighlight lang="ruby">require 'prime'
 
p ar = pr = Prime.take(10)
4.times{p ar = ar.product(pr).map{|(a,b)| a*b}.uniq.sort.take(10)}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,300 ⟶ 4,703:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_kprime(n: u32, k: u32) -> bool {
let mut primes = 0;
let mut f = 2;
Line 3,338 ⟶ 4,741:
println!("{}: {:?}", k, kprime_generator(k).take(10).collect::<Vec<_>>());
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,349 ⟶ 4,752:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">def isKPrime(n: Int, k: Int, d: Int = 2): Boolean = (n, k, d) match {
case (n, k, _) if n == 1 => k == 0
case (n, _, d) if n % d == 0 => isKPrime(n / d, k - 1, d)
Line 3,364 ⟶ 4,767:
for (k <- 1 to 5) {
println( s"$k: [${ kPrimeStream(k).take(10) mkString " " }]" )
}</langsyntaxhighlight>
 
{{out}}
Line 3,376 ⟶ 4,779:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: kprime (in var integer: number, in integer: k) is func
Line 3,412 ⟶ 4,815:
writeln;
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,424 ⟶ 4,827:
 
=={{header|SequenceL}}==
<langsyntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
 
Line 3,445 ⟶ 4,848:
firstNKPrimesHelper(k, N, current + 1, newResult);
 
isKPrime(k, n) := size(primeFactorization(n)) = k;</langsyntaxhighlight>
 
Using Prime Decomposition Solution [http://rosettacode.org/wiki/Prime_decomposition#SequenceL]
Line 3,460 ⟶ 4,863:
 
=={{header|Sidef}}==
Efficient algorithm for generating all the k-almost prime numbers in a given range '''[a,b]''':
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func is_k_almost_primealmost_primes(na, b, k) {
 
for (var (p, f) = (2, 0); (f < k) && (p*p <= n); ++p) {
a (n /= p; ++f) while max(p `divides`2**k, na)
}var arr = []
 
n > 1 ? (f.inc == k) : (f == k)
func (m, lo, k) {
 
var hi = idiv(b,m).iroot(k)
 
if (k == 1) {
 
lo = max(lo, idiv_ceil(a, m))
 
each_prime(lo, hi, {|p|
arr << m*p
})
 
return nil
}
 
each_prime(lo, hi, {|p|
 
var t = m*p
var u = idiv_ceil(a, t)
var v = idiv(b, t)
 
next if (u > v)
 
__FUNC__(t, p, k-1)
})
}(1, 2, k)
 
return arr.sort
}
 
for k in (1..5) {
{ |k|
var (x=10, lo=1, 10hi=2)
sayvar gatherarr {= []
loop { |i|
arr += if almost_primes(is_k_almost_prime(ilo, hi, k)) {
break if (arr.len >= take(ix)
--xlo == 0 && breakhi+1
hi = }2*lo
} << 1..Inf
}
say arr.first(x)
} << 1..5</lang>
}</syntaxhighlight>
{{out}}
<pre>
Line 3,487 ⟶ 4,918:
[32, 48, 72, 80, 108, 112, 120, 162, 168, 176]
</pre>
 
Also built-in:
 
<syntaxhighlight lang="ruby">for k in (1..5) {
var x = 10
say k.almost_primes(x.nth_almost_prime(k))
}</syntaxhighlight>
 
(same output as above)
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">struct KPrimeGen: Sequence, IteratorProtocol {
let k: Int
private(set) var n: Int
Line 3,524 ⟶ 4,964:
for k in 1..<6 {
print("\(k): \(Array(KPrimeGen(k: k, n: 1).lazy.prefix(10)))")
}</langsyntaxhighlight>
 
{{out}}
Line 3,537 ⟶ 4,977:
{{works with|Tcl|8.6}}
{{tcllib|math::numtheory}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
package require math::numtheory
 
Line 3,567 ⟶ 5,007:
for {set K 1} {$K <= 5} {incr K} {
puts "$K => [firstN_KalmostPrimes 10 $K]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,577 ⟶ 5,017:
</pre>
 
== {{header|Tiny BASICTypeScript}} ==
{{trans|FreeBASIC}}
<lang tinybasic>
<syntaxhighlight REMlang="javascript">// Almost prime
LET K=1
10 IF K>5 THEN END
PRINT "k = ",K,":"
LET I=2
LET C=0
20 IF C>=10 THEN GOTO 40
LET N=I
GOSUB 500
IF P=0 THEN GOTO 30
PRINT I
LET C=C+1
30 LET I=I+1
GOTO 20
40 LET K=K+1
GOTO 10
 
function isKPrime(n: number, k: number): bool {
REM Check if N is a K prime (result: P)
500 LET Fvar f = 0;
for (var LETi J= 2; i <= n; i++)
while (n % i == 0) {
510 IF (N/J)*J<>N THEN GOTO 520
IF F=K THENif (f == GOTOk) 530
LET F=F+1 return false;
LET N=N/J ++f;
n = Math.floor(n / i);
GOTO 510
}
520 LET J=J+1
return f == k;
IF J<=N THEN GOTO 510
}
LET P=0
IF F=K THEN LET P=-1
for (var k = 1; k <= 5; k++) {
RETURN
process.stdout.write(`k = ${k}:`);
530 LET P=0
var i RETURN= 2, c = 0;
while (c < 10) {
</lang>
if (isKPrime(i, k)) {
process.stdout.write(" " + i.toString().padStart(3, ' '));
++c;
}
++i;
}
console.log();
}
</syntaxhighlight>
{{out}}
<pre>
k = 1: 2 3 5 7 11 13 17 19 23 29
k = 1:
k = 2: 4 6 9 10 14 15 21 22 25 26
2
k = 3: 8 12 18 20 27 28 30 42 44 45
3
k = 4: 16 24 36 40 54 56 60 81 84 88
5
k = 5: 32 48 72 80 108 112 120 162 168 176
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
</pre>
 
=={{header|uBasic/4tH}}==
{{trans|C}}
<lang>Local(3)
 
For c@ = 1 To 5
Print "k = ";c@;": ";
 
b@=0
 
For a@ = 2 Step 1 While b@ < 10
If FUNC(_kprime (a@,c@)) Then
b@ = b@ + 1
Print " ";a@;
EndIf
Next
 
Print
Next
 
End
 
_kprime Param(2)
Local(2)
 
d@ = 0
For c@ = 2 Step 1 While (d@ < b@) * ((c@ * c@) < (a@ + 1))
Do While (a@ % c@) = 0
a@ = a@ / c@
d@ = d@ + 1
Loop
Next
Return (b@ = (d@ + (a@ > 1)))</lang>
{{out}}
<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
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
 
0 OK, 0:200</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Private Function kprime(ByVal n As Integer, k As Integer) As Boolean
Dim p As Integer, factors As Integer
p = 2
Line 3,744 ⟶ 5,087:
Debug.Print
Next k
End Sub</langsyntaxhighlight>{{out}}
<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 3,753 ⟶ 5,096:
=={{header|VBScript}}==
Repurposed the VBScript code for the Prime Decomposition task.
<syntaxhighlight lang="vb">
<lang vb>
For k = 1 To 5
count = 0
Line 3,808 ⟶ 5,151:
Next
End Function
</syntaxhighlight>
</lang>
 
{{Out}}
Line 3,819 ⟶ 5,162:
</pre>
 
=={{header|VisualV Basic .NET(Vlang)}}==
{{trans|C#Go}}
<syntaxhighlight lang="v (vlang)">
<lang vbnet>Module Module1
fn k_prime(n int, k int) bool {
mut nf := 0
mut nn := n
for i in 2..nn + 1 {
for nn % i == 0 {
if nf == k {return false}
nf++
nn /= i
}
}
return nf == k
}
 
fn gen(k int, n int) []int {
Class KPrime
mut r := Public K As Integer[]int{len:n}
mut nx := 2
for i in 0..n {
for !k_prime(nx, k) {nx++}
r[i] = nx
nx++
}
return r
}
 
fn main(){
Public Function IsKPrime(number As Integer) As Boolean
for k in 1..6 {println('$k ${gen(k,10)}')}
Dim primes = 0
}
Dim p = 2
</syntaxhighlight>
While p * p <= number AndAlso primes < K
While number Mod p = 0 AndAlso primes < K
number = number / p
primes = primes + 1
End While
p = p + 1
End While
If number > 1 Then
primes = primes + 1
End If
Return primes = K
End Function
 
Public Function GetFirstN(n As Integer) As List(Of Integer)
Dim result As New List(Of Integer)
Dim number = 2
While result.Count < n
If IsKPrime(number) Then
result.Add(number)
End If
number = number + 1
End While
Return result
End Function
End Class
 
Sub Main()
For Each k In Enumerable.Range(1, 5)
Dim kprime = New KPrime With {
.K = k
}
Console.WriteLine("k = {0}: {1}", k, String.Join(" ", kprime.GetFirstN(10)))
Next
End Sub
 
End Module</lang>
{{out}}
<pre>
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
k =1 [2: 4 63 95 107 1411 1513 2117 2219 2523 2629]
k2 =[4 3:6 89 1210 1814 2015 2721 2822 3025 42 44 4526]
k3 =[8 4:12 1618 2420 3627 4028 5430 5642 6044 81 84 8845]
4 [16 24 36 40 54 56 60 81 84 88]
k = 5: 32 48 72 80 108 112 120 162 168 176</pre>
5 [32 48 72 80 108 112 120 162 168 176]
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var kPrime = Fn.new { |n, k|
var nf = 0
var i = 2
Line 3,899 ⟶ 5,229:
}
 
for (k in 1..5) System.print("%(k) %(gen.call(k, 10))") </langsyntaxhighlight>
 
{{out}}
Line 3,910 ⟶ 5,240:
</pre>
 
=={{header|XBasicXPL0}}==
<syntaxhighlight lang="xpl0">func Factors(N); \Return number of (prime) factors in N
{{trans|FreeBASIC}}
int N, F, C;
{{works with|Windows XBasic}}
[C:= 0; F:= 2;
<lang xbasic>
repeat if rem(N/F) = 0 then
PROGRAM "almostprime"
[C:= C+1;
VERSION "0.0001"
N:= N/F;
]
else F:= F+1;
until F > N;
return C;
];
 
int K, C, N;
DECLARE FUNCTION Entry()
[for K:= 1 to 5 do
INTERNAL FUNCTION KPrime(n%%, k%%)
[C:= 0;
N:= 2;
IntOut(0, K); Text(0, ": ");
loop [if Factors(N) = K then
[IntOut(0, N); ChOut(0, ^ );
C:= C+1;
if C >= 10 then quit;
];
N:= N+1;
];
CrLf(0);
];
]</syntaxhighlight>
 
FUNCTION Entry()
FOR k@@ = 1 TO 5
PRINT "k ="; k@@; ": ";
i%% = 2
c%% = 0
DO WHILE c%% < 10
IFT KPrime(i%%, k@@) THEN
PRINT FORMAT$("### ", i%%);
INC c%%
END IF
INC i%%
LOOP
PRINT
NEXT k@@
END FUNCTION
 
FUNCTION KPrime(n%%, k%%)
f%% = 0
FOR i%% = 2 TO n%%
DO WHILE n%% MOD i%% = 0
IF f%% = k%% THEN RETURN $$FALSE
INC f%%
n%% = n%% \ i%%
LOOP
NEXT i%%
RETURN f%% = k%%
END FUNCTION
 
END PROGRAM
</lang>
{{out}}
<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
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
</pre>
 
=={{header|Yabasic}}==
{{trans|Lua}}
<lang Yabasic>// Returns boolean indicating whether n is k-almost prime
sub almostPrime(n, k)
local divisor, count
divisor = 2
while(count < (k + 1) and n <> 1)
if not mod(n, divisor) then
n = n / divisor
count = count + 1
else
divisor = divisor + 1
end if
wend
return count = k
end sub
 
// Generates table containing first ten k-almost primes for given k
sub kList(k, kTab())
local n, i
n = 2^k : i = 1
while(i < 11)
if almostPrime(n, k) then
kTab(i) = n
i = i + 1
end if
n = n + 1
wend
end sub
 
// Main procedure, displays results from five calls to kList()
dim kTab(10)
for k = 1 to 5
print "k = ", k, " : ";
kList(k, kTab())
for n = 1 to 10
print kTab(n), ", ";
next
print "..."
next</lang>
 
=={{header|zkl}}==
Line 4,008 ⟶ 5,283:
 
Can't say I entirely understand this algorithm. Uses list comprehension to calculate the outer/tensor product (p10 ⊗ ar).
<langsyntaxhighlight lang="zkl">primes:=Utils.Generator(Import("sieve").postponed_sieve);
(p10:=ar:=primes.walk(10)).println();
do(4){
(ar=([[(x,y);ar;p10;'*]] : Utils.Helpers.listUnique(_).sort()[0,10])).println();
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,021 ⟶ 5,296:
L(32,48,72,80,108,112,120,162,168,176)
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<lang zxbasic>10 FOR k=1 TO 5
20 PRINT k;":";
30 LET c=0: LET i=1
40 IF c=10 THEN GO TO 100
50 LET i=i+1
60 GO SUB 1000
70 IF r THEN PRINT " ";i;: LET c=c+1
90 GO TO 40
100 PRINT
110 NEXT k
120 STOP
1000 REM kprime
1010 LET p=2: LET n=i: LET f=0
1020 IF f=k OR (p*p)>n THEN GO TO 1100
1030 IF n/p=INT (n/p) THEN LET n=n/p: LET f=f+1: GO TO 1030
1040 LET p=p+1: GO TO 1020
1100 LET r=(f+(n>1)=k)
1110 RETURN</lang>
 
{{out}}
<pre>1: 2 3 5 7 11 13 17 19 23 29
2: 4 6 9 10 14 15 21 22 25 26
3: 8 12 18 20 27 28 30 42 44 45
4: 16 24 36 40 54 56 60 81 84 88
5: 32 48 72 80 108 112 120 162 168 176</pre>
16

edits