Almost prime: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add CLU |
|||
(49 intermediate revisions by 25 users not shown) | |||
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}}
<syntaxhighlight lang="arm assembly">
/* 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 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
<
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
Line 601 ⟶ 600:
}
MsgBox % results</
'''Output (Msgbox):'''
Line 611 ⟶ 610:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f ALMOST_PRIME.AWK
BEGIN {
Line 637 ⟶ 636:
return(f + (n > 1) == k)
}
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 648 ⟶ 647:
=={{header|BASIC}}==
<
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</
{{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}}
<
let kprime(n, k) = valof
Line 699 ⟶ 1,546:
wrch('*N')
$)
$)</
{{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.
<
|^ 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 726 ⟶ 1,573:
=={{header|C}}==
<
int kprime(int n, int k)
Line 755 ⟶ 1,602:
return 0;
}</
{{out}}
<pre>
Line 765 ⟶ 1,612:
</pre>
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Linq;
Line 820 ⟶ 1,667:
}
}
}</
{{out}}
<pre>
Line 832 ⟶ 1,679:
=={{header|C++}}==
{{trans|Kotlin}}
<
#include <iostream>
#include <sstream>
Line 863 ⟶ 1,710:
return EXIT_SUCCESS;
}</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 873 ⟶ 1,720:
=={{header|Clojure}}==
<
(ns clojure.examples.almostprime
Line 897 ⟶ 1,744:
(println "k:" k (divisors-k k 10))))
}</
{{out}}
<pre>
Line 909 ⟶ 1,756:
=={{header|CLU}}==
<
f: int := 0
p: int := 2
Line 938 ⟶ 1,785:
stream$putl(po, "")
end
end start_up</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 945 ⟶ 1,792:
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}}==
<
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
Line 959 ⟶ 1,884:
(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 971 ⟶ 1,896:
=={{header|Cowgol}}==
{{trans|C}}
<
sub kprime(n: uint8, k: uint8): (kp: uint8) is
Line 1,011 ⟶ 1,936:
print_nl();
k := k + 1;
end loop;</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,022 ⟶ 1,947:
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,057 ⟶ 1,982:
writeln;
}
}</
{{out}}
Line 1,069 ⟶ 1,994:
{{trans|C}}
<syntaxhighlight lang="delphi">
program AlmostPrime;
Line 1,110 ⟶ 2,035:
end;
end.
</syntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,118 ⟶ 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]
<
(define (almost-prime? p k)
(= k (length (prime-factors p))))
Line 1,133 ⟶ 2,133:
(for-each write (almost-primes k nmax))
(writeln)))
</syntaxhighlight>
{{out}}
<
(task)
Line 1,143 ⟶ 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>
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,196 ⟶ 2,196:
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,209 ⟶ 2,209:
;; 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,235 ⟶ 2,235:
end
Factors.kfactors(10,5)</
{{out}}
Line 1,249 ⟶ 2,249:
Using the factors function from [[Prime_decomposition#Erlang]].
<
-module(factors).
-export([factors/1,kfactors/0,kfactors/2]).
Line 1,274 ⟶ 2,274:
_ ->
kfactors(Tn,Tk, N+1,K, Acc) end.
</
{{out}}
Line 1,300 ⟶ 2,300:
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM ALMOST_PRIME
Line 1,337 ⟶ 2,337:
END FOR
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,346 ⟶ 2,346:
=={{header|F_Sharp|F#}}==
<
if f > n then None
elif n % f = 0 then Some (f, (f, n/f))
Line 1,364 ⟶ 2,364:
[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,373 ⟶ 2,373:
=={{header|Factor}}==
<
math.combinatorics math.primes.factors math.ranges sequences ;
IN: rosetta-code.almost-prime
Line 1,384 ⟶ 2,384:
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,396 ⟶ 2,396:
=={{header|FOCAL}}==
<
01.20 Q
Line 1,415 ⟶ 2,415:
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,424 ⟶ 2,424:
=={{header|Fortran}}==
<
program almost_prime
use iso_fortran_env, only: output_unit
Line 1,470 ⟶ 2,470:
end function kprime
end program almost_prime
</syntaxhighlight>
{{out}}
Line 1,479 ⟶ 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|Frink}}==
<
{
n=2
Line 1,541 ⟶ 2,498:
println[]
}</
Output:
Line 1,554 ⟶ 2,511:
=={{header|Futhark}}==
<syntaxhighlight lang="futhark">
let kprime(n: i32, k: i32): bool =
let (p,f) = (2, 0)
Line 1,573 ⟶ 2,530:
in ps
in map f (1...m)
</syntaxhighlight>
=={{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}}==
<
import "fmt"
Line 1,611 ⟶ 2,613:
fmt.Println(k, gen(k, 10))
}
}</
{{out}}
<pre>
Line 1,622 ⟶ 2,624:
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
public class almostprime
{
Line 1,657 ⟶ 2,659:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,670 ⟶ 2,672:
k = 5:
32 48 72 80 108 112 120 162 168 176
</pre>
=={{header|Haskell}}==
<
isPrime n = not $ any ((0 ==) . (mod n)) [2..(truncate $ sqrt $ fromIntegral n)]
Line 1,729 ⟶ 2,692:
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,738 ⟶ 2,701:
Larger ''k''s require more complicated methods:
<
True (drop 1 primes)]
Line 1,764 ⟶ 2,727:
putStrLn "\n10000th to 10100th 500-amost primes:"
mapM_ print $ take 100 $ drop 10000 $ kprimes 500</
{{out}}
<pre>
Line 1,781 ⟶ 2,744:
Works in both languages.
<
procedure main()
Line 1,790 ⟶ 2,753:
procedure genKap(k)
suspend (k = *factors(n := seq(q)), n)
end</
Output:
Line 1,801 ⟶ 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}}==
<
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 1,820 ⟶ 2,827:
=={{header|Java}}==
<
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
Line 1,846 ⟶ 2,853:
return f + ((n > 1) ? 1 : 0) == k;
}
}</
{{out}}
<pre>
Line 1,857 ⟶ 2,864:
=={{header|Javascript}}==
<
var divisor = 2, count = 0
while(count < k + 1 && n != 1) {
Line 1,880 ⟶ 2,887:
}
}
}</
{{out}}
<pre>
Line 1,892 ⟶ 2,899:
{{Works with| jq|1.4}}
'''Infrastructure:'''
<
def until(cond; next):
def _until:
Line 1,975 ⟶ 2,982:
if ($in % $p) == 0 then . + [$in|multiplicity($p)] else . end )
end
end;</
'''isalmostprime'''
<
# Emit a stream of the first N almost-k primes
Line 1,990 ⟶ 2,997:
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,019 ⟶ 3,026:
for k in 1:5
println("$k-Almost-primes: ", join(almostprimes(10, k), ", "), "...")
end</
{{out}}
Line 2,030 ⟶ 3,037:
=={{header|Kotlin}}==
{{trans|Java}}
<
var n = x
var f = 0
Line 2,054 ⟶ 3,061:
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,061 ⟶ 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|Lua}}==
<
function almostPrime (n, k)
local divisor, count = 2, 0
Line 2,136 ⟶ 3,103:
end
print("...")
end</
{{out}}
<pre>k=1: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ...
Line 2,145 ⟶ 3,112:
=={{header|Maple}}==
<
local aprimes, i, intfactors;
aprimes := Array([]);
Line 2,160 ⟶ 3,127:
aprimes;
end proc:
<seq( AlmostPrimes(i), i = 1..5 )>;</
{{out}}
<pre>
Line 2,170 ⟶ 3,137:
=={{header|MAD}}==
<
INTERNAL FUNCTION(NN,KK)
Line 2,205 ⟶ 3,172:
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,220 ⟶ 3,187:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
(* generates a list of the n smallest k-almost-primes *)
Module[{firstnprimes, runningkprimes = {}},
Line 2,233 ⟶ 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</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,241 ⟶ 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}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,287 ⟶ 3,319:
ReadChar;
END AlmostPrime.</
=={{header|Nim}}==
<
result = @[]
var
Line 2,312 ⟶ 3,344:
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,322 ⟶ 3,354:
=={{header|Objeck}}==
{{trans|C}}
<
function : native : kPrime(n : Int, k : Int) ~ Bool {
f := 0;
Line 2,348 ⟶ 3,380:
};
}
}</
{{out}}
Line 2,357 ⟶ 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}}==
<
| i |
0 2 n for: i [
Line 2,373 ⟶ 3,443:
2 while (l size 10 <>) [ dup k kprime? if dup l add then 1+ ]
drop
;</
{{out}}
Line 2,383 ⟶ 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}}==
<
for(k=1,5,almost(k);print)</
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
Line 2,398 ⟶ 3,559:
{{libheader|primTrial}}
{{works with|Free Pascal}}
<
{$IFDEF FPC}
{$Mode Delphi}
Line 2,423 ⟶ 3,584:
inc(k);
until k > 10;
END.</
;output:
<pre>K= 1 : 2 3 5 7 11 13 17 19 23 29
Line 2,439 ⟶ 3,600:
Using a CPAN module, which is simple and fast:
{{libheader|ntheory}}
<
sub almost {
my($k,$n) = @_;
Line 2,445 ⟶ 3,606:
map { $i++ while scalar factor($i) != $k; $i++ } 1..$n;
}
say "$_ : ", join(" ", almost($_,10)) for 1..5;</
{{out}}
<pre>
Line 2,455 ⟶ 3,616:
</pre>
or writing everything by hand:
<
use warnings;
Line 2,518 ⟶ 3,679:
return $primes[$n];
}
}</
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29
Line 2,526 ⟶ 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}}
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 2,580 ⟶ 3,906:
END;
CALL EXIT;
EOF</
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,589 ⟶ 3,915:
=={{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 2,604 ⟶ 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>
<!--</
{{out}}
<pre>
Line 2,615 ⟶ 3,941:
=={{header|PicoLisp}}==
<
(make
(let
Line 2,641 ⟶ 3,967:
(println I '-> (almost I) ) )
(bye)</
=={{header|Potion}}==
<
kprime = (n, k):
p = 2, f = 0
Line 2,663 ⟶ 3,989:
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 2,688 ⟶ 4,014:
sort(Long, Sorted), % uniquifies
take(Take, Sorted, List).
</
<
nPrimes( Accumulator, I, Primes) :-
Line 2,729 ⟶ 4,055:
length(Head, N),
append(Head,X,List).
</syntaxhighlight>
<
:- if(current_prolog_flag(dialect, yap)).
Line 2,758 ⟶ 4,084:
between(Min, Max, I).
:- endif.
</syntaxhighlight>
Example using SWI-Prolog:<pre>
?- between(1,5,I),
Line 2,775 ⟶ 4,101:
=={{header|Processing}}==
<
for (int i = 1; i <= 5; i++) {
int count = 0;
Line 2,811 ⟶ 4,137:
}
return count;
}</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,818 ⟶ 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|Python}}==
This imports [[Prime decomposition#Python]]
<
from itertools import islice, count
try:
Line 2,891 ⟶ 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))))</
{{out}}
<pre>1: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Line 2,904 ⟶ 4,178:
<
# k-Almost-primes
# Python 3.6.3
# no imports
# author: manuelcaeiro | https://github.com/manuelcaeiro
def prime_factors(m=2):
Line 2,942 ⟶ 4,217:
# try:
#k_almost_primes(6000, 10)
</syntaxhighlight>
{{out}}
<pre>
Line 2,951 ⟶ 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]]
<
# Find k-Almost-primes
# R implementation
Line 3,003 ⟶ 4,311:
}
print(res)
}</
{{out}}
Line 3,016 ⟶ 4,324:
=={{header|Racket}}==
<
(require (only-in math/number-theory factorize))
Line 3,040 ⟶ 4,348:
"\n"))
(displayln (format-table KAP-table-values))</
{{out}}
Line 3,053 ⟶ 4,361:
{{trans|C}}
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku"
loop (my ($p, $f) = 2, 0; $f < $k && $p*$p <= $n; $p++) {
$n /= $p, $f++ while $n %% $p;
Line 3,063 ⟶ 4,371:
say ~.[^10]
given grep { is-k-almost-prime($_, $k) }, 2 .. *
}</
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29
Line 3,072 ⟶ 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"
multi sub factors(1) { 1 }
Line 3,094 ⟶ 4,402:
sub almost($n) { map *.key, grep *.value == $n, @factory }
put almost($_)[^10] for 1..5;</
=={{header|REXX}}==
===Version 1 naive
The method used is to count the number of factors in the number to determine the K-primality.
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,135 ⟶ 4,443:
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,143 ⟶ 4,451:
4─almost (10) primes: 16 24 36 40 54 56 60 81 84 88
5─almost (10) primes: 32 48 72 80 108 112 120 162 168 176
0.006 seconds (Regina)
</pre>
{{out|output|text= when using the input of: <tt> 20 12 </tt>}}
Line 3,158 ⟶ 4,467:
11─almost (20) primes: 2048 3072 4608 5120 6912 7168 7680 10368 10752 11264 11520 12800 13312 15552 16128 16896 17280 17408 17920 19200
12─almost (20) primes: 4096 6144 9216 10240 13824 14336 15360 20736 21504 22528 23040 25600 26624 31104 32256 33792 34560 34816 35840 38400
22.380 seconds (Regina)
</pre>
===Version 2: optimized===
This optimized REXX version can be ''over a hundred times'' faster than the naive version.
Line 3,167 ⟶ 4,476:
:::* generating the primes (up to the limit) instead of dividing by (most) divisors.
:::* extending the ''up-front'' prime divisors in the '''factr''' function.
The 1<sup>st</sup> optimization (bullet) allows the direct computation (instead of searching) of all K─almost primes up to the first ''odd'' prime in the list.
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,237 ⟶ 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.*/</
{{out|output|text= when using the input of: <tt> 20 16 </tt>}}
<pre>
Line 3,258 ⟶ 4,566:
15─almost (20) primes: 32768 49152 73728 81920 110592 114688 122880 165888 172032 180224 184320 204800 212992 248832 258048 270336 276480 278528 286720 307200
16─almost (20) primes: 65536 98304 147456 163840 221184 229376 245760 331776 344064 360448 368640 409600 425984 497664 516096 540672 552960 557056 573440 614400
0.088 seconds (Regina)
</pre>
Yes, this is very fast. But both Version 1 and Version 2 are highly optimized for this specific task. They take advantage from the fact that we calculate for k = 1,2,3 and so on, thus use patterns in the generated primes and can pre generate and use a list of primes. In Version 2 you see also several several hardcoded numbers, specific for this task. And by the way, both programs are rather complicated.
A more standard approach, using a procedure Factors (see [[Prime decomposition#REXX|Prime decomposition]]), follows.
===Version 4 Standard procedures===
'''Libraries:''' [[Mathematics.hel|How to use]]<br>
'''Library:''' [[Mathematics.rex#Numbers|Numbers]]<br>
'''Library:''' [[Mathematics.rex#Functions|Functions]]<br>
<syntaxhighlight lang="rexx">
numeric digits 16
parse version version; say version digits() 'digits'; glob. = ''
say 'k-Almost primes'
say 'Direct approach using Factors'
arg n k m
if n = '' then
n = 10
if k = '' then
k = 5
/* Maximum number to examine */
if m = '' then
m = 180
call time('r')
/* Collect almost primes */
ap. = 0
do i = 2 to m
f = Factors(i); ap.f.0 = ap.f.0+1
ap = ap.f.0; ap.f.ap = i
end
/* Show results */
do i = 1 to k
call charout ,'k='i': '
do j = 1 to n
if ap.i.j > 0 then do
call charout ,ap.i.j' '
end
end
say
end
say format(time('e'),,3) 'seconds'
exit
/* Procedure Factors is in the Numbers library */
include Numbers
include Functions
</syntaxhighlight>
The maximum number is parameter here, but may be estimated from n and k, as in Versions 1 and 2.
{{out|Output default parameters}}
<pre>
REXX-Regina_3.9.6(MT) 5.00 29 Apr 2024 16 digits
k-Almost primes
Direct approach using Factors
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.003 seconds
</pre>
{{out|Output parameters 20 12 39000}}
<pre style="font-size:80%">
REXX-Regina_3.9.6(MT) 5.00 29 Apr 2024 16 digits
k-Almost primes
Direct approach using Factors
k=1: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
k=2: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57
k=3: 8 12 18 20 27 28 30 42 44 45 50 52 63 66 68 70 75 76 78 92
k=4: 16 24 36 40 54 56 60 81 84 88 90 100 104 126 132 135 136 140 150 152
k=5: 32 48 72 80 108 112 120 162 168 176 180 200 208 243 252 264 270 272 280 300
k=6: 64 96 144 160 216 224 240 324 336 352 360 400 416 486 504 528 540 544 560 600
k=7: 128 192 288 320 432 448 480 648 672 704 720 800 832 972 1008 1056 1080 1088 1120 1200
k=8: 256 384 576 640 864 896 960 1296 1344 1408 1440 1600 1664 1944 2016 2112 2160 2176 2240 2400
k=9: 512 768 1152 1280 1728 1792 1920 2592 2688 2816 2880 3200 3328 3888 4032 4224 4320 4352 4480 4800
k=10: 1024 1536 2304 2560 3456 3584 3840 5184 5376 5632 5760 6400 6656 7776 8064 8448 8640 8704 8960 9600
k=11: 2048 3072 4608 5120 6912 7168 7680 10368 10752 11264 11520 12800 13312 15552 16128 16896 17280 17408 17920 19200
k=12: 4096 6144 9216 10240 13824 14336 15360 20736 21504 22528 23040 25600 26624 31104 32256 33792 34560 34816 35840 38400
0.885 seconds
</pre>
{{out|Output parameters 20 16 615000}}
<pre style="font-size:70%">
REXX-Regina_3.9.6(MT) 5.00 29 Apr 2024 16 digits
k-Almost primes
Direct approach using Factors
k=1: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
k=2: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57
k=3: 8 12 18 20 27 28 30 42 44 45 50 52 63 66 68 70 75 76 78 92
k=4: 16 24 36 40 54 56 60 81 84 88 90 100 104 126 132 135 136 140 150 152
k=5: 32 48 72 80 108 112 120 162 168 176 180 200 208 243 252 264 270 272 280 300
k=6: 64 96 144 160 216 224 240 324 336 352 360 400 416 486 504 528 540 544 560 600
k=7: 128 192 288 320 432 448 480 648 672 704 720 800 832 972 1008 1056 1080 1088 1120 1200
k=8: 256 384 576 640 864 896 960 1296 1344 1408 1440 1600 1664 1944 2016 2112 2160 2176 2240 2400
k=9: 512 768 1152 1280 1728 1792 1920 2592 2688 2816 2880 3200 3328 3888 4032 4224 4320 4352 4480 4800
k=10: 1024 1536 2304 2560 3456 3584 3840 5184 5376 5632 5760 6400 6656 7776 8064 8448 8640 8704 8960 9600
k=11: 2048 3072 4608 5120 6912 7168 7680 10368 10752 11264 11520 12800 13312 15552 16128 16896 17280 17408 17920 19200
k=12: 4096 6144 9216 10240 13824 14336 15360 20736 21504 22528 23040 25600 26624 31104 32256 33792 34560 34816 35840 38400
k=13: 8192 12288 18432 20480 27648 28672 30720 41472 43008 45056 46080 51200 53248 62208 64512 67584 69120 69632 71680 76800
k=14: 16384 24576 36864 40960 55296 57344 61440 82944 86016 90112 92160 102400 106496 124416 129024 135168 138240 139264 143360 153600
k=15: 32768 49152 73728 81920 110592 114688 122880 165888 172032 180224 184320 204800 212992 248832 258048 270336 276480 278528 286720 307200
k=16: 65536 98304 147456 163840 221184 229376 245760 331776 344064 360448 368640 409600 425984 497664 516096 540672 552960 557056 573440 614400
25.129 seconds
</pre>
Not too bad! By the way, Version 3 can also generate lists of almost prime over other number ranges. Say you change the first do in 'do 1000000 to m' and run as follows, you get
{{out|Output parameters 10 10 1002000}}
<pre>
REXX-Regina_3.9.6(MT) 5.00 29 Apr 2024 16 digits
k-Almost primes
Direct approach using Factors
k=1: 1000003 1000033 1000037 1000039 1000081 1000099 1000117 1000121 1000133 1000151
k=2: 1000001 1000007 1000009 1000011 1000015 1000018 1000019 1000021 1000023 1000031
k=3: 1000002 1000006 1000013 1000014 1000022 1000028 1000029 1000030 1000043 1000046
k=4: 1000005 1000010 1000012 1000017 1000024 1000027 1000034 1000038 1000041 1000042
k=5: 1000004 1000016 1000025 1000035 1000036 1000044 1000056 1000060 1000062 1000072
k=6: 1000020 1000026 1000040 1000048 1000050 1000065 1000076 1000090 1000096 1000100
k=7: 1000125 1000152 1000176 1000200 1000256 1000352 1000368 1000404 1000428 1000431
k=8: 1000008 1000032 1000128 1000272 1000296 1000400 1000416 1000440 1000500 1000560
k=9: 1000064 1000080 1000160 1000192 1000224 1000350 1000480 1000640 1000832 1000896
k=10: 1000320 1000384 1000704 1000800 1001160 1001280 1001376 1001600 1001664 1001952
0.116 seconds
</pre>
=={{header|Ring}}==
<
for ap = 1 to 5
see "k = " + ap + ":"
Line 3,295 ⟶ 4,721:
next
return 1
</syntaxhighlight>
Output:
<pre>
Line 3,303 ⟶ 4,729:
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}}==
<
def almost_primes(k=2)
Line 3,313 ⟶ 4,796:
end
(1..5).each{|k| puts almost_primes(k).take(10).join(", ")}</
{{out}}
<pre>
Line 3,324 ⟶ 4,807:
{{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,338 ⟶ 4,821:
=={{header|Rust}}==
<
let mut primes = 0;
let mut f = 2;
Line 3,376 ⟶ 4,859:
println!("{}: {:?}", k, kprime_generator(k).take(10).collect::<Vec<_>>());
}
}</
{{out}}
<pre>
Line 3,387 ⟶ 4,870:
=={{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,402 ⟶ 4,885:
for (k <- 1 to 5) {
println( s"$k: [${ kPrimeStream(k).take(10) mkString " " }]" )
}</
{{out}}
Line 3,414 ⟶ 4,897:
=={{header|Seed7}}==
<
const func boolean: kprime (in var integer: number, in integer: k) is func
Line 3,450 ⟶ 4,933:
writeln;
end for;
end func;</
{{out}}
Line 3,462 ⟶ 4,945:
=={{header|SequenceL}}==
<
import <Utilities/Sequence.sl>;
Line 3,483 ⟶ 4,966:
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,498 ⟶ 4,981:
=={{header|Sidef}}==
Efficient algorithm for generating all the k-almost prime numbers in a given range '''[a,b]''':
<
a
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) {
var (x=10, lo=1,
arr +=
break if (arr.len >=
hi =
}
say arr.first(x)
}</syntaxhighlight>
{{out}}
<pre>
Line 3,525 ⟶ 5,036:
[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}}==
<
let k: Int
private(set) var n: Int
Line 3,562 ⟶ 5,082:
for k in 1..<6 {
print("\(k): \(Array(KPrimeGen(k: k, n: 1).lazy.prefix(10)))")
}</
{{out}}
Line 3,575 ⟶ 5,095:
{{works with|Tcl|8.6}}
{{tcllib|math::numtheory}}
<
package require math::numtheory
Line 3,605 ⟶ 5,125:
for {set K 1} {$K <= 5} {incr K} {
puts "$K => [firstN_KalmostPrimes 10 $K]"
}</
{{out}}
<pre>
Line 3,615 ⟶ 5,135:
</pre>
== {{header|
{{trans|FreeBASIC}}
<syntaxhighlight
function isKPrime(n: number, k: number): bool {
for (var
while (n % i == 0) {
n = Math.floor(n / i);
}
return f == k;
}
for (var k = 1; k <= 5; k++) {
process.stdout.write(`k = ${k}:`);
var i
while (c < 10) {
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 = 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|VBA}}==
{{trans|Phix}}<
Dim p As Integer, factors As Integer
p = 2
Line 3,782 ⟶ 5,205:
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 3,791 ⟶ 5,214:
=={{header|VBScript}}==
Repurposed the VBScript code for the Prime Decomposition task.
<syntaxhighlight lang="vb">
For k = 1 To 5
count = 0
Line 3,846 ⟶ 5,269:
Next
End Function
</syntaxhighlight>
{{Out}}
Line 3,857 ⟶ 5,280:
</pre>
=={{header|
{{trans|
<syntaxhighlight lang="v (vlang)">
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 {
mut r :=
mut nx := 2
for i in 0..n {
for !k_prime(nx, k) {nx++}
r[i] = nx
nx++
}
return r
}
fn main(){
for k in 1..6 {println('$k ${gen(k,10)}')}
}
</syntaxhighlight>
{{out}}
<pre>
4 [16 24 36 40 54 56 60 81 84 88]
5 [32 48 72 80 108 112 120 162 168 176]
</pre>
=={{header|Wren}}==
{{trans|Go}}
<
var nf = 0
var i = 2
Line 3,937 ⟶ 5,347:
}
for (k in 1..5) System.print("%(k) %(gen.call(k, 10))") </
{{out}}
Line 3,948 ⟶ 5,358:
</pre>
=={{header|
<syntaxhighlight lang="xpl0">func Factors(N); \Return number of (prime) factors in N
int N, F, C;
[C:= 0; F:= 2;
repeat if rem(N/F) = 0 then
[C:= C+1;
N:= N/F;
]
else F:= F+1;
until F > N;
return C;
];
int K, C, N;
[for K:= 1 to 5 do
[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>
{{out}}
<pre>
</pre>
=={{header|zkl}}==
Line 4,046 ⟶ 5,401:
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,059 ⟶ 5,414:
L(32,48,72,80,108,112,120,162,168,176)
</pre>
|