Almost prime: Difference between revisions
→functional
(→{{header|PHP}}: Spaces _before_ numbers. Function floor instead of casting to int.) |
|||
(24 intermediate revisions by 15 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 661 ⟶ 600:
}
MsgBox % results</
'''Output (Msgbox):'''
Line 671 ⟶ 610:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f ALMOST_PRIME.AWK
BEGIN {
Line 697 ⟶ 636:
return(f + (n > 1) == k)
}
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 708 ⟶ 647:
=={{header|BASIC}}==
<
20 FOR K=1 TO 5
30 PRINT USING "K = #:";K;
Line 722 ⟶ 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 730 ⟶ 669:
K = 5: 32 48 72 80 108 112 120 162 168 176</pre>
==={{header|
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}}
<
f = 0
for i = 2 to n
Line 757:
print
next k
end</
==={{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 790 ⟶ 1,546:
wrch('*N')
$)
$)</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 802 ⟶ 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 817 ⟶ 1,573:
=={{header|C}}==
<
int kprime(int n, int k)
Line 846 ⟶ 1,602:
return 0;
}</
{{out}}
<pre>
Line 857 ⟶ 1,613:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Linq;
Line 911 ⟶ 1,667:
}
}
}</
{{out}}
<pre>
Line 923 ⟶ 1,679:
=={{header|C++}}==
{{trans|Kotlin}}
<
#include <iostream>
#include <sstream>
Line 954 ⟶ 1,710:
return EXIT_SUCCESS;
}</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 964 ⟶ 1,720:
=={{header|Clojure}}==
<
(ns clojure.examples.almostprime
Line 988 ⟶ 1,744:
(println "k:" k (divisors-k k 10))))
}</
{{out}}
<pre>
Line 1,000 ⟶ 1,756:
=={{header|CLU}}==
<
f: int := 0
p: int := 2
Line 1,029 ⟶ 1,785:
stream$putl(po, "")
end
end start_up</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,039 ⟶ 1,795:
=={{header|COBOL}}==
{{trans|C}}
<
PROGRAM-ID. ALMOST-PRIME.
Line 1,107 ⟶ 1,863:
MOVE NEXT-N TO N.
ADD 1 TO F.
DIVIDE N BY P GIVING N-DIV-P.</
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,116 ⟶ 1,872:
=={{header|Common Lisp}}==
<
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
Line 1,128 ⟶ 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 1,140 ⟶ 1,896:
=={{header|Cowgol}}==
{{trans|C}}
<
sub kprime(n: uint8, k: uint8): (kp: uint8) is
Line 1,180 ⟶ 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,191 ⟶ 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,226 ⟶ 1,982:
writeln;
}
}</
{{out}}
Line 1,238 ⟶ 1,994:
{{trans|C}}
<syntaxhighlight lang="delphi">
program AlmostPrime;
Line 1,279 ⟶ 2,035:
end;
end.
</syntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,289 ⟶ 2,045:
=={{header|Draco}}==
<
word f, p;
f := 0;
Line 1,320 ⟶ 2,076:
writeln()
od
corp</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,327 ⟶ 2,083:
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,342 ⟶ 2,133:
(for-each write (almost-primes k nmax))
(writeln)))
</syntaxhighlight>
{{out}}
<
(task)
Line 1,352 ⟶ 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,405 ⟶ 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,418 ⟶ 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,444 ⟶ 2,235:
end
Factors.kfactors(10,5)</
{{out}}
Line 1,458 ⟶ 2,249:
Using the factors function from [[Prime_decomposition#Erlang]].
<
-module(factors).
-export([factors/1,kfactors/0,kfactors/2]).
Line 1,483 ⟶ 2,274:
_ ->
kfactors(Tn,Tk, N+1,K, Acc) end.
</
{{out}}
Line 1,509 ⟶ 2,300:
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM ALMOST_PRIME
Line 1,546 ⟶ 2,337:
END FOR
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 1,555 ⟶ 2,346:
=={{header|F_Sharp|F#}}==
<
if f > n then None
elif n % f = 0 then Some (f, (f, n/f))
Line 1,573 ⟶ 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,582 ⟶ 2,373:
=={{header|Factor}}==
<
math.combinatorics math.primes.factors math.ranges sequences ;
IN: rosetta-code.almost-prime
Line 1,593 ⟶ 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,605 ⟶ 2,396:
=={{header|FOCAL}}==
<
01.20 Q
Line 1,624 ⟶ 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,633 ⟶ 2,424:
=={{header|Fortran}}==
<
program almost_prime
use iso_fortran_env, only: output_unit
Line 1,679 ⟶ 2,470:
end function kprime
end program almost_prime
</syntaxhighlight>
{{out}}
Line 1,688 ⟶ 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,750 ⟶ 2,498:
println[]
}</
Output:
Line 1,763 ⟶ 2,511:
=={{header|Futhark}}==
<syntaxhighlight lang="futhark">
let kprime(n: i32, k: i32): bool =
let (p,f) = (2, 0)
Line 1,782 ⟶ 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,820 ⟶ 2,613:
fmt.Println(k, gen(k, 10))
}
}</
{{out}}
<pre>
Line 1,831 ⟶ 2,624:
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
public class almostprime
{
Line 1,866 ⟶ 2,659:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,879 ⟶ 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,938 ⟶ 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,947 ⟶ 2,701:
Larger ''k''s require more complicated methods:
<
True (drop 1 primes)]
Line 1,973 ⟶ 2,727:
putStrLn "\n10000th to 10100th 500-amost primes:"
mapM_ print $ take 100 $ drop 10000 $ kprimes 500</
{{out}}
<pre>
Line 1,990 ⟶ 2,744:
Works in both languages.
<
procedure main()
Line 1,999 ⟶ 2,753:
procedure genKap(k)
suspend (k = *factors(n := seq(q)), n)
end</
Output:
Line 2,010 ⟶ 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 2,029 ⟶ 2,827:
=={{header|Java}}==
<
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
Line 2,055 ⟶ 2,853:
return f + ((n > 1) ? 1 : 0) == k;
}
}</
{{out}}
<pre>
Line 2,066 ⟶ 2,864:
=={{header|Javascript}}==
<
var divisor = 2, count = 0
while(count < k + 1 && n != 1) {
Line 2,089 ⟶ 2,887:
}
}
}</
{{out}}
<pre>
Line 2,101 ⟶ 2,899:
{{Works with| jq|1.4}}
'''Infrastructure:'''
<
def until(cond; next):
def _until:
Line 2,184 ⟶ 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 2,199 ⟶ 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,228 ⟶ 3,026:
for k in 1:5
println("$k-Almost-primes: ", join(almostprimes(10, k), ", "), "...")
end</
{{out}}
Line 2,239 ⟶ 3,037:
=={{header|Kotlin}}==
{{trans|Java}}
<
var n = x
var f = 0
Line 2,263 ⟶ 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,270 ⟶ 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,346 ⟶ 3,103:
end
print("...")
end</
{{out}}
<pre>k=1: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ...
Line 2,355 ⟶ 3,112:
=={{header|Maple}}==
<
local aprimes, i, intfactors;
aprimes := Array([]);
Line 2,370 ⟶ 3,127:
aprimes;
end proc:
<seq( AlmostPrimes(i), i = 1..5 )>;</
{{out}}
<pre>
Line 2,380 ⟶ 3,137:
=={{header|MAD}}==
<
INTERNAL FUNCTION(NN,KK)
Line 2,415 ⟶ 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,430 ⟶ 3,187:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
(* generates a list of the n smallest k-almost-primes *)
Module[{firstnprimes, runningkprimes = {}},
Line 2,443 ⟶ 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,451 ⟶ 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,497 ⟶ 3,319:
ReadChar;
END AlmostPrime.</
=={{header|Nim}}==
<
result = @[]
var
Line 2,564 ⟶ 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,574 ⟶ 3,354:
=={{header|Objeck}}==
{{trans|C}}
<
function : native : kPrime(n : Int, k : Int) ~ Bool {
f := 0;
Line 2,600 ⟶ 3,380:
};
}
}</
{{out}}
Line 2,609 ⟶ 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,625 ⟶ 3,443:
2 while (l size 10 <>) [ dup k kprime? if dup l add then 1+ ]
drop
;</
{{out}}
Line 2,635 ⟶ 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,650 ⟶ 3,559:
{{libheader|primTrial}}
{{works with|Free Pascal}}
<
{$IFDEF FPC}
{$Mode Delphi}
Line 2,675 ⟶ 3,584:
inc(k);
until k > 10;
END.</
;output:
<pre>K= 1 : 2 3 5 7 11 13 17 19 23 29
Line 2,691 ⟶ 3,600:
Using a CPAN module, which is simple and fast:
{{libheader|ntheory}}
<
sub almost {
my($k,$n) = @_;
Line 2,697 ⟶ 3,606:
map { $i++ while scalar factor($i) != $k; $i++ } 1..$n;
}
say "$_ : ", join(" ", almost($_,10)) for 1..5;</
{{out}}
<pre>
Line 2,707 ⟶ 3,616:
</pre>
or writing everything by hand:
<
use warnings;
Line 2,770 ⟶ 3,679:
return $primes[$n];
}
}</
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 19, 23, 29
Line 2,781 ⟶ 3,690:
=={{header|Phixmonti}}==
{{trans|OForth}}
<
by Galileo, 06/2022 #/
Line 2,807 ⟶ 3,716:
endfor
pstack</
{{out}}
<pre>
Line 2,816 ⟶ 3,725:
=={{header|PHP}}==
{{trans|FreeBASIC}}
<
<?php
// Almost prime
Line 2,848 ⟶ 3,757:
}
?>
</syntaxhighlight>
{{out}}
<pre>
Line 2,860 ⟶ 3,769:
=={{header|Picat}}==
{{trans|J}}
<
N = 10,
Ps = primes(100).take(N),
Line 2,879 ⟶ 3,788:
mul_take(L1,L2,N) = [I*J : I in L1, J in L2, I<=J].sort_remove_dups().take(N).
take(L,N) = [L[I] : I in 1..N].</
{{out}}
Line 2,911 ⟶ 3,820:
=={{header|PL/I}}==
{{trans|C}}
<
kprime: procedure(nn, k) returns(bit);
declare (n, nn, k, p, f) fixed;
Line 2,936 ⟶ 3,845:
put skip;
end;
end almost_prime;</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 2,946 ⟶ 3,855:
=={{header|PL/M}}==
{{trans|C}}
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 2,997 ⟶ 3,906:
END;
CALL EXIT;
EOF</
{{out}}
<pre>K = 1: 2 3 5 7 11 13 17 19 23 29
Line 3,006 ⟶ 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 3,021 ⟶ 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 3,032 ⟶ 3,941:
=={{header|PicoLisp}}==
<
(make
(let
Line 3,058 ⟶ 3,967:
(println I '-> (almost I) ) )
(bye)</
=={{header|Potion}}==
<
kprime = (n, k):
p = 2, f = 0
Line 3,080 ⟶ 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 3,105 ⟶ 4,014:
sort(Long, Sorted), % uniquifies
take(Take, Sorted, List).
</
<
nPrimes( Accumulator, I, Primes) :-
Line 3,146 ⟶ 4,055:
length(Head, N),
append(Head,X,List).
</syntaxhighlight>
<
:- if(current_prolog_flag(dialect, yap)).
Line 3,175 ⟶ 4,084:
between(Min, Max, I).
:- endif.
</syntaxhighlight>
Example using SWI-Prolog:<pre>
?- between(1,5,I),
Line 3,192 ⟶ 4,101:
=={{header|Processing}}==
<
for (int i = 1; i <= 5; i++) {
int count = 0;
Line 3,228 ⟶ 4,137:
}
return count;
}</
{{out}}
<pre>k = 1: 2 3 5 7 11 13 17 19 23 29
Line 3,235 ⟶ 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 3,308 ⟶ 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 3,321 ⟶ 4,178:
<
# k-Almost-primes
# Python 3.6.3
Line 3,360 ⟶ 4,217:
# try:
#k_almost_primes(6000, 10)
</syntaxhighlight>
{{out}}
<pre>
Line 3,375 ⟶ 4,232:
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
<
[ stack ] is factors ( --> s )
Line 3,393 ⟶ 4,250:
5 times
[ 10 i^ 1+ dup echo sp
almostprimes echo cr ]</
{{out}}
Line 3,406 ⟶ 4,263:
=={{header|R}}==
This uses the function from [[Prime decomposition#R]]
<
# Find k-Almost-primes
# R implementation
Line 3,454 ⟶ 4,311:
}
print(res)
}</
{{out}}
Line 3,467 ⟶ 4,324:
=={{header|Racket}}==
<
(require (only-in math/number-theory factorize))
Line 3,491 ⟶ 4,348:
"\n"))
(displayln (format-table KAP-table-values))</
{{out}}
Line 3,504 ⟶ 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,514 ⟶ 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,523 ⟶ 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,545 ⟶ 4,402:
sub almost($n) { map *.key, grep *.value == $n, @factory }
put almost($_)[^10] for 1..5;</
=={{header|REXX}}==
Line 3,552 ⟶ 4,409:
The first three '''k-almost''' primes for each '''K''' group are computed directly (rather than found).
<
parse arg N K . /*get optional arguments from the C.L. */
if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
Line 3,586 ⟶ 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,623 ⟶ 4,480:
Once the required primes are generated, the finding of the K─almost primes is almost instantaneous.
<
parse arg N K . /*obtain optional arguments from the CL*/
if N=='' | N==',' then N=10 /*N not specified? Then use default.*/
Line 3,688 ⟶ 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,712 ⟶ 4,569:
=={{header|Ring}}==
<
for ap = 1 to 5
see "k = " + ap + ":"
Line 3,746 ⟶ 4,603:
next
return 1
</syntaxhighlight>
Output:
<pre>
Line 3,754 ⟶ 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}}==
<
def almost_primes(k=2)
Line 3,764 ⟶ 4,678:
end
(1..5).each{|k| puts almost_primes(k).take(10).join(", ")}</
{{out}}
<pre>
Line 3,775 ⟶ 4,689:
{{trans|J}}
<
p ar = pr = Prime.take(10)
4.times{p ar = ar.product(pr).map{|(a,b)| a*b}.uniq.sort.take(10)}</
{{out}}
<pre>
Line 3,789 ⟶ 4,703:
=={{header|Rust}}==
<
let mut primes = 0;
let mut f = 2;
Line 3,827 ⟶ 4,741:
println!("{}: {:?}", k, kprime_generator(k).take(10).collect::<Vec<_>>());
}
}</
{{out}}
<pre>
Line 3,838 ⟶ 4,752:
=={{header|Scala}}==
<
case (n, k, _) if n == 1 => k == 0
case (n, _, d) if n % d == 0 => isKPrime(n / d, k - 1, d)
Line 3,853 ⟶ 4,767:
for (k <- 1 to 5) {
println( s"$k: [${ kPrimeStream(k).take(10) mkString " " }]" )
}</
{{out}}
Line 3,865 ⟶ 4,779:
=={{header|Seed7}}==
<
const func boolean: kprime (in var integer: number, in integer: k) is func
Line 3,901 ⟶ 4,815:
writeln;
end for;
end func;</
{{out}}
Line 3,913 ⟶ 4,827:
=={{header|SequenceL}}==
<
import <Utilities/Sequence.sl>;
Line 3,934 ⟶ 4,848:
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,949 ⟶ 4,863:
=={{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,976 ⟶ 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}}==
<
let k: Int
private(set) var n: Int
Line 4,013 ⟶ 4,964:
for k in 1..<6 {
print("\(k): \(Array(KPrimeGen(k: k, n: 1).lazy.prefix(10)))")
}</
{{out}}
Line 4,026 ⟶ 4,977:
{{works with|Tcl|8.6}}
{{tcllib|math::numtheory}}
<
package require math::numtheory
Line 4,056 ⟶ 5,007:
for {set K 1} {$K <= 5} {incr K} {
puts "$K => [firstN_KalmostPrimes 10 $K]"
}</
{{out}}
<pre>
Line 4,064 ⟶ 5,015:
4 => 16 24 36 40 54 56 60 81 84 88
5 => 32 48 72 80 108 112 120 162 168 176
</pre>
== {{header|TypeScript}} ==
{{trans|FreeBASIC}}
<
function isKPrime(n: number, k: number): bool {
Line 4,188 ⟶ 5,045:
console.log();
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,197 ⟶ 5,054:
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 4,299 ⟶ 5,087:
Debug.Print
Next k
End Sub</
<pre>k = 1 : 2 3 5 7 11 13 17 19 23 29
k = 2 : 4 6 9 10 14 15 21 22 25 26
Line 4,308 ⟶ 5,096:
=={{header|VBScript}}==
Repurposed the VBScript code for the Prime Decomposition task.
<syntaxhighlight lang="vb">
For k = 1 To 5
count = 0
Line 4,363 ⟶ 5,151:
Next
End Function
</syntaxhighlight>
{{Out}}
Line 4,374 ⟶ 5,162:
</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
for nn % i == 0 {
if nf == k {return false}
nf++
nn /= i
}
}
Line 4,447 ⟶ 5,181:
mut r := []int{len:n}
mut nx := 2
for i in 0
for !k_prime(nx, k) {nx++}
r[i] = nx
nx++
Line 4,458 ⟶ 5,190:
fn main(){
for k in 1..6 {println('$k ${gen(k,10)}')}
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,473 ⟶ 5,204:
=={{header|Wren}}==
{{trans|Go}}
<
var nf = 0
var i = 2
Line 4,498 ⟶ 5,229:
}
for (k in 1..5) System.print("%(k) %(gen.call(k, 10))") </
{{out}}
Line 4,507 ⟶ 5,238:
4 [16, 24, 36, 40, 54, 56, 60, 81, 84, 88]
5 [32, 48, 72, 80, 108, 112, 120, 162, 168, 176]
</pre>
=={{header|XPL0}}==
<
int N, F, C;
[C:= 0; F:= 2;
Line 4,586 ⟶ 5,267:
CrLf(0);
];
]</
{{out}}
Line 4,596 ⟶ 5,277:
5: 32 48 72 80 108 112 120 162 168 176
</pre>
=={{header|zkl}}==
Line 4,646 ⟶ 5,283:
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,659 ⟶ 5,296:
L(32,48,72,80,108,112,120,162,168,176)
</pre>
|