Almost prime: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 20: Line 20:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=11l>F k_prime(k, =n)
<syntaxhighlight lang="11l">F k_prime(k, =n)
V f = 0
V f = 0
V p = 2
V p = 2
Line 49: Line 49:


=={{header|Action!}}==
=={{header|Action!}}==
<syntaxhighlight lang=Action!>BYTE FUNC IsAlmostPrime(INT num BYTE k)
<syntaxhighlight lang="action!">BYTE FUNC IsAlmostPrime(INT num BYTE k)
INT f,p,v
INT f,p,v


Line 102: Line 102:
This imports the package '''Prime_Numbers''' from [[Prime decomposition#Ada]].
This imports the package '''Prime_Numbers''' from [[Prime decomposition#Ada]].


<syntaxhighlight lang=ada>with Prime_Numbers, Ada.Text_IO;
<syntaxhighlight lang="ada">with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
procedure Test_Kth_Prime is
Line 137: Line 137:
=={{header|ALGOL 68}}==
=={{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.
Worth noticing is the n(...)(...) picture in the printf and the WHILE ... DO SKIP OD idiom which is quite common in ALgol 68.
<syntaxhighlight lang=algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
INT examples=10, classes=5;
INT examples=10, classes=5;
MODE SEMIPRIME = STRUCT ([examples]INT data, INT count);
MODE SEMIPRIME = STRUCT ([examples]INT data, INT count);
Line 187: Line 187:


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<syntaxhighlight lang=algolm>begin
<syntaxhighlight lang="algolm">begin


integer function mod(a, b);
integer function mod(a, b);
Line 239: Line 239:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
{{Trans|C}} with tweaks to the factorisation routine.
{{Trans|C}} with tweaks to the factorisation routine.
<syntaxhighlight lang=algolw>begin
<syntaxhighlight lang="algolw">begin
logical procedure kPrime( integer value nv, k ) ;
logical procedure kPrime( integer value nv, k ) ;
begin
begin
Line 288: Line 288:
{{libheader|pco}}
{{libheader|pco}}
Works in [[Dyalog APL]]
Works in [[Dyalog APL]]
<syntaxhighlight lang=APL>f←{↑r⊣⍵∘{r,∘⊂←⍺↑∪{⍵[⍋⍵]},f∘.×⍵}⍣(⍺-1)⊃r←⊂f←pco¨⍳⍵}</syntaxhighlight>
<syntaxhighlight lang="apl">f←{↑r⊣⍵∘{r,∘⊂←⍺↑∪{⍵[⍋⍵]},f∘.×⍵}⍣(⍺-1)⊃r←⊂f←pco¨⍳⍵}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 301: Line 301:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program kprime.s */
/* program kprime.s */
Line 535: Line 535:
=={{header|Arturo}}==
=={{header|Arturo}}==


<syntaxhighlight lang=rebol>almostPrime: function [k, listLen][
<syntaxhighlight lang="rebol">almostPrime: function [k, listLen][
result: new []
result: new []
test: 2
test: 2
Line 575: Line 575:
ASIC has both FOR and WHILE loops, but it had better not go out from the loop. So, in the subroutine CHECKKPRIME they are simulated by the constructs with GOTO statements.
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>
<syntaxhighlight lang="basic">
REM Almost prime
REM Almost prime
FOR K = 1 TO 5
FOR K = 1 TO 5
Line 635: Line 635:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Translation of the C Version
Translation of the C Version
<syntaxhighlight lang=AutoHotkey>kprime(n,k) {
<syntaxhighlight lang="autohotkey">kprime(n,k) {
p:=2, f:=0
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while( (f<k) && (p*p<=n) ) {
Line 671: Line 671:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang=AWK>
<syntaxhighlight lang="awk">
# syntax: GAWK -f ALMOST_PRIME.AWK
# syntax: GAWK -f ALMOST_PRIME.AWK
BEGIN {
BEGIN {
Line 708: Line 708:


=={{header|BASIC}}==
=={{header|BASIC}}==
<syntaxhighlight lang=basic>10 DEFINT A-Z
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR K=1 TO 5
20 FOR K=1 TO 5
30 PRINT USING "K = #:";K;
30 PRINT USING "K = #:";K;
Line 732: Line 732:
=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang=freebasic>function kPrime(n, k)
<syntaxhighlight lang="freebasic">function kPrime(n, k)
f = 0
f = 0
for i = 2 to n
for i = 2 to n
Line 762: Line 762:
=={{header|BCPL}}==
=={{header|BCPL}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=BCPL>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let kprime(n, k) = valof
let kprime(n, k) = valof
Line 802: Line 802:
The extra spaces are to ensure it's readable on buggy interpreters that don't include a space after numeric output.
The extra spaces are to ensure it's readable on buggy interpreters that don't include a space after numeric output.


<syntaxhighlight lang=befunge>1>::48*"= k",,,,02p.":",01v
<syntaxhighlight lang="befunge">1>::48*"= k",,,,02p.":",01v
|^ v0!`\*:g40:<p402p300:+1<
|^ v0!`\*:g40:<p402p300:+1<
K| >2g03g`*#v_ 1`03g+02g->|
K| >2g03g`*#v_ 1`03g+02g->|
Line 817: Line 817:


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang=c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int kprime(int n, int k)
int kprime(int n, int k)
Line 857: Line 857:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 923: Line 923:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=cpp>#include <cstdlib>
<syntaxhighlight lang="cpp">#include <cstdlib>
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
Line 964: Line 964:
=={{header|Clojure}}==
=={{header|Clojure}}==


<syntaxhighlight lang=clojure>
<syntaxhighlight lang="clojure">


(ns clojure.examples.almostprime
(ns clojure.examples.almostprime
Line 1,000: Line 1,000:


=={{header|CLU}}==
=={{header|CLU}}==
<syntaxhighlight lang=clu>kprime = proc (n,k: int) returns (bool)
<syntaxhighlight lang="clu">kprime = proc (n,k: int) returns (bool)
f: int := 0
f: int := 0
p: int := 2
p: int := 2
Line 1,039: Line 1,039:
=={{header|COBOL}}==
=={{header|COBOL}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ALMOST-PRIME.
PROGRAM-ID. ALMOST-PRIME.
Line 1,116: Line 1,116:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<syntaxhighlight lang=lisp>(defun start ()
<syntaxhighlight lang="lisp">(defun start ()
(loop for k from 1 to 5
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
Line 1,140: Line 1,140:
=={{header|Cowgol}}==
=={{header|Cowgol}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub kprime(n: uint8, k: uint8): (kp: uint8) is
sub kprime(n: uint8, k: uint8): (kp: uint8) is
Line 1,191: Line 1,191:
This contains a copy of the function <code>decompose</code> from the Prime decomposition task.
This contains a copy of the function <code>decompose</code> from the Prime decomposition task.
{{trans|Ada}}
{{trans|Ada}}
<syntaxhighlight lang=d>import std.stdio, std.algorithm, std.traits;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.traits;


Unqual!T[] decompose(T)(in T number) pure nothrow
Unqual!T[] decompose(T)(in T number) pure nothrow
Line 1,238: Line 1,238:
{{trans|C}}
{{trans|C}}


<syntaxhighlight lang=Delphi>
<syntaxhighlight lang="delphi">
program AlmostPrime;
program AlmostPrime;


Line 1,289: Line 1,289:


=={{header|Draco}}==
=={{header|Draco}}==
<syntaxhighlight lang=draco>proc nonrec kprime(word n, k) bool:
<syntaxhighlight lang="draco">proc nonrec kprime(word n, k) bool:
word f, p;
word f, p;
f := 0;
f := 0;
Line 1,330: Line 1,330:
=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
Small numbers : filter the sequence [ 2 .. n]
Small numbers : filter the sequence [ 2 .. n]
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(define (almost-prime? p k)
(define (almost-prime? p k)
(= k (length (prime-factors p))))
(= k (length (prime-factors p))))
Line 1,344: Line 1,344:
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(task)
(task)


Line 1,354: Line 1,354:
</syntaxhighlight>
</syntaxhighlight>
Large numbers : generate - combinations with repetitions - k-almost-primes up to pmax.
Large numbers : generate - combinations with repetitions - k-almost-primes up to pmax.
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(lib 'match)
(lib 'match)
(define-syntax-rule (: v i) (vector-ref v i))
(define-syntax-rule (: v i) (vector-ref v i))
Line 1,408: Line 1,408:


{{out}}
{{out}}
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
;; we want 500-almost-primes from the 10000-th.
;; we want 500-almost-primes from the 10000-th.
(take (drop (list-sort < (almost-primes 500 10000)) 10000 ) 10)
(take (drop (list-sort < (almost-primes 500 10000)) 10000 ) 10)
Line 1,422: Line 1,422:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Erlang}}
{{trans|Erlang}}
<syntaxhighlight lang=elixir>defmodule Factors do
<syntaxhighlight lang="elixir">defmodule Factors do
def factors(n), do: factors(n,2,[])
def factors(n), do: factors(n,2,[])
Line 1,458: Line 1,458:
Using the factors function from [[Prime_decomposition#Erlang]].
Using the factors function from [[Prime_decomposition#Erlang]].


<syntaxhighlight lang=erlang>
<syntaxhighlight lang="erlang">
-module(factors).
-module(factors).
-export([factors/1,kfactors/0,kfactors/2]).
-export([factors/1,kfactors/0,kfactors/2]).
Line 1,509: Line 1,509:


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE>
<syntaxhighlight lang="erre">
PROGRAM ALMOST_PRIME
PROGRAM ALMOST_PRIME


Line 1,555: Line 1,555:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang=fsharp>let rec genFactor (f, n) =
<syntaxhighlight lang="fsharp">let rec genFactor (f, n) =
if f > n then None
if f > n then None
elif n % f = 0 then Some (f, (f, n/f))
elif n % f = 0 then Some (f, (f, n/f))
Line 1,582: Line 1,582:


=={{header|Factor}}==
=={{header|Factor}}==
<syntaxhighlight lang=factor>USING: formatting fry kernel lists lists.lazy locals
<syntaxhighlight lang="factor">USING: formatting fry kernel lists lists.lazy locals
math.combinatorics math.primes.factors math.ranges sequences ;
math.combinatorics math.primes.factors math.ranges sequences ;
IN: rosetta-code.almost-prime
IN: rosetta-code.almost-prime
Line 1,605: Line 1,605:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<syntaxhighlight lang=focal>01.10 F K=1,5;D 3
<syntaxhighlight lang="focal">01.10 F K=1,5;D 3
01.20 Q
01.20 Q


Line 1,633: Line 1,633:


=={{header|Fortran}}==
=={{header|Fortran}}==
<syntaxhighlight lang=fortran>
<syntaxhighlight lang="fortran">
program almost_prime
program almost_prime
use iso_fortran_env, only: output_unit
use iso_fortran_env, only: output_unit
Line 1,691: Line 1,691:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Function kPrime(n As Integer, k As Integer) As Boolean
Function kPrime(n As Integer, k As Integer) As Boolean
Line 1,734: Line 1,734:


=={{header|Frink}}==
=={{header|Frink}}==
<syntaxhighlight lang=frink>for k = 1 to 5
<syntaxhighlight lang="frink">for k = 1 to 5
{
{
n=2
n=2
Line 1,763: Line 1,763:
=={{header|Futhark}}==
=={{header|Futhark}}==


<syntaxhighlight lang=Futhark>
<syntaxhighlight lang="futhark">
let kprime(n: i32, k: i32): bool =
let kprime(n: i32, k: i32): bool =
let (p,f) = (2, 0)
let (p,f) = (2, 0)
Line 1,785: Line 1,785:


=={{header|Go}}==
=={{header|Go}}==
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,831: Line 1,831:


=={{header|Groovy}}==
=={{header|Groovy}}==
<syntaxhighlight lang=Groovy>
<syntaxhighlight lang="groovy">
public class almostprime
public class almostprime
{
{
Line 1,884: Line 1,884:
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang=qbasic>
<syntaxhighlight lang="qbasic">
10 'Almost prime
10 'Almost prime
20 FOR K% = 1 TO 5
20 FOR K% = 1 TO 5
Line 1,921: Line 1,921:


=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell>isPrime :: Integral a => a -> Bool
<syntaxhighlight lang="haskell">isPrime :: Integral a => a -> Bool
isPrime n = not $ any ((0 ==) . (mod n)) [2..(truncate $ sqrt $ fromIntegral n)]
isPrime n = not $ any ((0 ==) . (mod n)) [2..(truncate $ sqrt $ fromIntegral n)]


Line 1,947: Line 1,947:


Larger ''k''s require more complicated methods:
Larger ''k''s require more complicated methods:
<syntaxhighlight lang=haskell>primes = 2:3:[n | n <- [5,7..], foldr (\p r-> p*p > n || rem n p > 0 && r)
<syntaxhighlight lang="haskell">primes = 2:3:[n | n <- [5,7..], foldr (\p r-> p*p > n || rem n p > 0 && r)
True (drop 1 primes)]
True (drop 1 primes)]


Line 1,990: Line 1,990:


Works in both languages.
Works in both languages.
<syntaxhighlight lang=unicon>link "factors"
<syntaxhighlight lang="unicon">link "factors"


procedure main()
procedure main()
Line 2,013: Line 2,013:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang=J> (10 {. [:~.[:/:~[:,*/~)^:(i.5)~p:i.10
<syntaxhighlight lang="j"> (10 {. [:~.[:/:~[:,*/~)^:(i.5)~p:i.10
2 3 5 7 11 13 17 19 23 29
2 3 5 7 11 13 17 19 23 29
4 6 9 10 14 15 21 22 25 26
4 6 9 10 14 15 21 22 25 26
Line 2,029: Line 2,029:


=={{header|Java}}==
=={{header|Java}}==
<syntaxhighlight lang=java>public class AlmostPrime {
<syntaxhighlight lang="java">public class AlmostPrime {
public static void main(String[] args) {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
for (int k = 1; k <= 5; k++) {
Line 2,066: Line 2,066:


=={{header|Javascript}}==
=={{header|Javascript}}==
<syntaxhighlight lang=javascript>function almostPrime (n, k) {
<syntaxhighlight lang="javascript">function almostPrime (n, k) {
var divisor = 2, count = 0
var divisor = 2, count = 0
while(count < k + 1 && n != 1) {
while(count < k + 1 && n != 1) {
Line 2,101: Line 2,101:
{{Works with| jq|1.4}}
{{Works with| jq|1.4}}
'''Infrastructure:'''
'''Infrastructure:'''
<syntaxhighlight lang=jq># Recent versions of jq (version > 1.4) have the following definition of "until":
<syntaxhighlight lang="jq"># Recent versions of jq (version > 1.4) have the following definition of "until":
def until(cond; next):
def until(cond; next):
def _until:
def _until:
Line 2,186: Line 2,186:
end;</syntaxhighlight>
end;</syntaxhighlight>
'''isalmostprime'''
'''isalmostprime'''
<syntaxhighlight lang=jq>def isalmostprime(k): (prime_factors_with_multiplicities | length) == k;
<syntaxhighlight lang="jq">def isalmostprime(k): (prime_factors_with_multiplicities | length) == k;


# Emit a stream of the first N almost-k primes
# Emit a stream of the first N almost-k primes
Line 2,201: Line 2,201:
end;</syntaxhighlight>
end;</syntaxhighlight>
'''The task:'''
'''The task:'''
<syntaxhighlight lang=jq>range(1;6) as $k | "k=\($k): \([almostprimes(10;$k)])"</syntaxhighlight>
<syntaxhighlight lang="jq">range(1;6) as $k | "k=\($k): \([almostprimes(10;$k)])"</syntaxhighlight>
{{out}}
{{out}}
<syntaxhighlight lang=sh>$ jq -c -r -n -f Almost_prime.jq
<syntaxhighlight lang="sh">$ jq -c -r -n -f Almost_prime.jq
k=1: [2,3,5,7,11,13,17,19,23,29]
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=2: [4,6,9,10,14,15,21,22,25,26]
Line 2,212: Line 2,212:
=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|1.1}}
{{works with|Julia|1.1}}
<syntaxhighlight lang=julia>using Primes
<syntaxhighlight lang="julia">using Primes


isalmostprime(n::Integer, k::Integer) = sum(values(factor(n))) == k
isalmostprime(n::Integer, k::Integer) = sum(values(factor(n))) == k
Line 2,239: Line 2,239:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=scala>fun Int.k_prime(x: Int): Boolean {
<syntaxhighlight lang="scala">fun Int.k_prime(x: Int): Boolean {
var n = x
var n = x
var f = 0
var f = 0
Line 2,274: Line 2,274:
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
{{works with|Just BASIC}}
{{works with|Just BASIC}}
<syntaxhighlight lang=lb>
<syntaxhighlight lang="lb">
' Almost prime
' Almost prime
for k = 1 to 5
for k = 1 to 5
Line 2,313: Line 2,313:


=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang=Lua>-- Returns boolean indicating whether n is k-almost prime
<syntaxhighlight lang="lua">-- Returns boolean indicating whether n is k-almost prime
function almostPrime (n, k)
function almostPrime (n, k)
local divisor, count = 2, 0
local divisor, count = 2, 0
Line 2,355: Line 2,355:


=={{header|Maple}}==
=={{header|Maple}}==
<syntaxhighlight lang=Maple>AlmostPrimes:=proc(k, numvalues::posint:=10)
<syntaxhighlight lang="maple">AlmostPrimes:=proc(k, numvalues::posint:=10)
local aprimes, i, intfactors;
local aprimes, i, intfactors;
aprimes := Array([]);
aprimes := Array([]);
Line 2,380: Line 2,380:


=={{header|MAD}}==
=={{header|MAD}}==
<syntaxhighlight lang=MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(NN,KK)
INTERNAL FUNCTION(NN,KK)
Line 2,430: Line 2,430:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>kprimes[k_,n_] :=
<syntaxhighlight lang="mathematica">kprimes[k_,n_] :=
(* generates a list of the n smallest k-almost-primes *)
(* generates a list of the n smallest k-almost-primes *)
Module[{firstnprimes, runningkprimes = {}},
Module[{firstnprimes, runningkprimes = {}},
Line 2,452: Line 2,452:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<syntaxhighlight lang=modula2>MODULE AlmostPrime;
<syntaxhighlight lang="modula2">MODULE AlmostPrime;
FROM FormatString IMPORT FormatString;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,502: Line 2,502:
{{trans|GW-BASIC}}
{{trans|GW-BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang=basic>
<syntaxhighlight lang="basic">
10 REM Almost prime
10 REM Almost prime
20 FOR K=1 TO 5
20 FOR K=1 TO 5
Line 2,542: Line 2,542:


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang=Nim>proc prime(k: int, listLen: int): seq[int] =
<syntaxhighlight lang="nim">proc prime(k: int, listLen: int): seq[int] =
result = @[]
result = @[]
var
var
Line 2,574: Line 2,574:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=objeck>class Kth_Prime {
<syntaxhighlight lang="objeck">class Kth_Prime {
function : native : kPrime(n : Int, k : Int) ~ Bool {
function : native : kPrime(n : Int, k : Int) ~ Bool {
f := 0;
f := 0;
Line 2,612: Line 2,612:
=={{header|Oforth}}==
=={{header|Oforth}}==


<syntaxhighlight lang=Oforth>: kprime?( n k -- b )
<syntaxhighlight lang="oforth">: kprime?( n k -- b )
| i |
| i |
0 2 n for: i [
0 2 n for: i [
Line 2,638: Line 2,638:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<syntaxhighlight lang=parigp>almost(k)=my(n); for(i=1,10,while(bigomega(n++)!=k,); print1(n", "));
<syntaxhighlight lang="parigp">almost(k)=my(n); for(i=1,10,while(bigomega(n++)!=k,); print1(n", "));
for(k=1,5,almost(k);print)</syntaxhighlight>
for(k=1,5,almost(k);print)</syntaxhighlight>
{{out}}
{{out}}
Line 2,650: Line 2,650:
{{libheader|primTrial}}
{{libheader|primTrial}}
{{works with|Free Pascal}}
{{works with|Free Pascal}}
<syntaxhighlight lang=Pascal>program AlmostPrime;
<syntaxhighlight lang="pascal">program AlmostPrime;
{$IFDEF FPC}
{$IFDEF FPC}
{$Mode Delphi}
{$Mode Delphi}
Line 2,691: Line 2,691:
Using a CPAN module, which is simple and fast:
Using a CPAN module, which is simple and fast:
{{libheader|ntheory}}
{{libheader|ntheory}}
<syntaxhighlight lang=perl>use ntheory qw/factor/;
<syntaxhighlight lang="perl">use ntheory qw/factor/;
sub almost {
sub almost {
my($k,$n) = @_;
my($k,$n) = @_;
Line 2,707: Line 2,707:
</pre>
</pre>
or writing everything by hand:
or writing everything by hand:
<syntaxhighlight lang=perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 2,781: Line 2,781:
=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
{{trans|OForth}}
{{trans|OForth}}
<syntaxhighlight lang=Phixmonti>/# Rosetta Code problem: http://rosettacode.org/wiki/Almost_prime
<syntaxhighlight lang="phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Almost_prime
by Galileo, 06/2022 #/
by Galileo, 06/2022 #/


Line 2,816: Line 2,816:
=={{header|PHP}}==
=={{header|PHP}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang=php>
<syntaxhighlight lang="php">
<?php
<?php
// Almost prime
// Almost prime
Line 2,860: Line 2,860:
=={{header|Picat}}==
=={{header|Picat}}==
{{trans|J}}
{{trans|J}}
<syntaxhighlight lang=Picat>go =>
<syntaxhighlight lang="picat">go =>
N = 10,
N = 10,
Ps = primes(100).take(N),
Ps = primes(100).take(N),
Line 2,911: Line 2,911:
=={{header|PL/I}}==
=={{header|PL/I}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=pli>almost_prime: procedure options(main);
<syntaxhighlight lang="pli">almost_prime: procedure options(main);
kprime: procedure(nn, k) returns(bit);
kprime: procedure(nn, k) returns(bit);
declare (n, nn, k, p, f) fixed;
declare (n, nn, k, p, f) fixed;
Line 2,946: Line 2,946:
=={{header|PL/M}}==
=={{header|PL/M}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=plm>100H:
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 3,006: Line 3,006:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span> <span style="color: #000080;font-style:italic;">-- ie {{1},{2},{3},{4},{5}}</span>
<span style="color: #004080;">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>
<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,032: Line 3,032:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(de factor (N)
<syntaxhighlight lang="picolisp">(de factor (N)
(make
(make
(let
(let
Line 3,061: Line 3,061:


=={{header|Potion}}==
=={{header|Potion}}==
<syntaxhighlight lang=potion># Converted from C
<syntaxhighlight lang="potion"># Converted from C
kprime = (n, k):
kprime = (n, k):
p = 2, f = 0
p = 2, f = 0
Line 3,085: Line 3,085:


=={{header|Prolog}}==
=={{header|Prolog}}==
<syntaxhighlight lang=prolog>% almostPrime(K, +Take, List) succeeds if List can be unified with the
<syntaxhighlight lang="prolog">% almostPrime(K, +Take, List) succeeds if List can be unified with the
% first Take K-almost-primes.
% first Take K-almost-primes.
% Notice that K need not be specified.
% Notice that K need not be specified.
Line 3,106: Line 3,106:
take(Take, Sorted, List).
take(Take, Sorted, List).
</syntaxhighlight>That's it. The rest is machinery. For portability, a compatibility section is included below.
</syntaxhighlight>That's it. The rest is machinery. For portability, a compatibility section is included below.
<syntaxhighlight lang=Prolog>nPrimes( M, Primes) :- nPrimes( [2], M, Primes).
<syntaxhighlight lang="prolog">nPrimes( M, Primes) :- nPrimes( [2], M, Primes).


nPrimes( Accumulator, I, Primes) :-
nPrimes( Accumulator, I, Primes) :-
Line 3,147: Line 3,147:
append(Head,X,List).
append(Head,X,List).
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang=Prolog>%%%%% compatibility section %%%%%
<syntaxhighlight lang="prolog">%%%%% compatibility section %%%%%


:- if(current_prolog_flag(dialect, yap)).
:- if(current_prolog_flag(dialect, yap)).
Line 3,192: Line 3,192:


=={{header|Processing}}==
=={{header|Processing}}==
<syntaxhighlight lang=processing>void setup() {
<syntaxhighlight lang="processing">void setup() {
for (int i = 1; i <= 5; i++) {
for (int i = 1; i <= 5; i++) {
int count = 0;
int count = 0;
Line 3,238: Line 3,238:
=={{header|PureBasic}}==
=={{header|PureBasic}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=PureBasic>EnableExplicit
<syntaxhighlight lang="purebasic">EnableExplicit


Procedure.b kprime(n.i, k.i)
Procedure.b kprime(n.i, k.i)
Line 3,290: Line 3,290:
=={{header|Python}}==
=={{header|Python}}==
This imports [[Prime decomposition#Python]]
This imports [[Prime decomposition#Python]]
<syntaxhighlight lang=python>from prime_decomposition import decompose
<syntaxhighlight lang="python">from prime_decomposition import decompose
from itertools import islice, count
from itertools import islice, count
try:
try:
Line 3,321: Line 3,321:




<syntaxhighlight lang=python>
<syntaxhighlight lang="python">
# k-Almost-primes
# k-Almost-primes
# Python 3.6.3
# Python 3.6.3
Line 3,375: Line 3,375:
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].


<syntaxhighlight lang=Quackery> [ stack ] is quantity ( --> s )
<syntaxhighlight lang="quackery"> [ stack ] is quantity ( --> s )
[ stack ] is factors ( --> s )
[ stack ] is factors ( --> s )


Line 3,406: Line 3,406:
=={{header|R}}==
=={{header|R}}==
This uses the function from [[Prime decomposition#R]]
This uses the function from [[Prime decomposition#R]]
<syntaxhighlight lang=rsplus>#===============================================================
<syntaxhighlight lang="rsplus">#===============================================================
# Find k-Almost-primes
# Find k-Almost-primes
# R implementation
# R implementation
Line 3,467: Line 3,467:
=={{header|Racket}}==
=={{header|Racket}}==


<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require (only-in math/number-theory factorize))
(require (only-in math/number-theory factorize))


Line 3,504: Line 3,504:
{{trans|C}}
{{trans|C}}
{{works with|Rakudo|2015.12}}
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang=perl6>sub is-k-almost-prime($n is copy, $k) returns Bool {
<syntaxhighlight lang="raku" line>sub is-k-almost-prime($n is copy, $k) returns Bool {
loop (my ($p, $f) = 2, 0; $f < $k && $p*$p <= $n; $p++) {
loop (my ($p, $f) = 2, 0; $f < $k && $p*$p <= $n; $p++) {
$n /= $p, $f++ while $n %% $p;
$n /= $p, $f++ while $n %% $p;
Line 3,523: Line 3,523:
Here is a solution with identical output based on the <tt>factors</tt> routine from [[Count_in_factors#Raku]] (to be included manually until we decide where in the distribution to put it).
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=perl6>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;
<syntaxhighlight lang="raku" line>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;


multi sub factors(1) { 1 }
multi sub factors(1) { 1 }
Line 3,552: Line 3,552:


The first three &nbsp; '''k-almost''' &nbsp; primes for each &nbsp; '''K''' &nbsp; group are computed directly &nbsp; (rather than found).
The first three &nbsp; '''k-almost''' &nbsp; primes for each &nbsp; '''K''' &nbsp; group are computed directly &nbsp; (rather than found).
<syntaxhighlight lang=rexx>/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
<syntaxhighlight lang="rexx">/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
parse arg N K . /*get optional arguments from the C.L. */
parse arg N K . /*get optional arguments from the C.L. */
if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
Line 3,623: Line 3,623:


Once the required primes are generated, the finding of the K─almost primes is almost instantaneous.
Once the required primes are generated, the finding of the K─almost primes is almost instantaneous.
<syntaxhighlight lang=rexx>/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
<syntaxhighlight lang="rexx">/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
parse arg N K . /*obtain optional arguments from the CL*/
parse arg N K . /*obtain optional arguments from the CL*/
if N=='' | N==',' then N=10 /*N not specified? Then use default.*/
if N=='' | N==',' then N=10 /*N not specified? Then use default.*/
Line 3,712: Line 3,712:


=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang=ring>
<syntaxhighlight lang="ring">
for ap = 1 to 5
for ap = 1 to 5
see "k = " + ap + ":"
see "k = " + ap + ":"
Line 3,757: Line 3,757:


=={{header|Ruby}}==
=={{header|Ruby}}==
<syntaxhighlight lang=ruby>require 'prime'
<syntaxhighlight lang="ruby">require 'prime'


def almost_primes(k=2)
def almost_primes(k=2)
Line 3,775: Line 3,775:


{{trans|J}}
{{trans|J}}
<syntaxhighlight lang=ruby>require 'prime'
<syntaxhighlight lang="ruby">require 'prime'


p ar = pr = Prime.take(10)
p ar = pr = Prime.take(10)
Line 3,789: Line 3,789:


=={{header|Rust}}==
=={{header|Rust}}==
<syntaxhighlight lang=rust>fn is_kprime(n: u32, k: u32) -> bool {
<syntaxhighlight lang="rust">fn is_kprime(n: u32, k: u32) -> bool {
let mut primes = 0;
let mut primes = 0;
let mut f = 2;
let mut f = 2;
Line 3,838: Line 3,838:


=={{header|Scala}}==
=={{header|Scala}}==
<syntaxhighlight lang=Scala>def isKPrime(n: Int, k: Int, d: Int = 2): Boolean = (n, k, d) match {
<syntaxhighlight lang="scala">def isKPrime(n: Int, k: Int, d: Int = 2): Boolean = (n, k, d) match {
case (n, k, _) if n == 1 => k == 0
case (n, k, _) if n == 1 => k == 0
case (n, _, d) if n % d == 0 => isKPrime(n / d, k - 1, d)
case (n, _, d) if n % d == 0 => isKPrime(n / d, k - 1, d)
Line 3,865: Line 3,865:


=={{header|Seed7}}==
=={{header|Seed7}}==
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const func boolean: kprime (in var integer: number, in integer: k) is func
const func boolean: kprime (in var integer: number, in integer: k) is func
Line 3,913: Line 3,913:


=={{header|SequenceL}}==
=={{header|SequenceL}}==
<syntaxhighlight lang=sequencel>import <Utilities/Conversion.sl>;
<syntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
import <Utilities/Sequence.sl>;


Line 3,950: Line 3,950:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=ruby>func is_k_almost_prime(n, k) {
<syntaxhighlight lang="ruby">func is_k_almost_prime(n, k) {
for (var (p, f) = (2, 0); (f < k) && (p*p <= n); ++p) {
for (var (p, f) = (2, 0); (f < k) && (p*p <= n); ++p) {
(n /= p; ++f) while (p `divides` n)
(n /= p; ++f) while (p `divides` n)
Line 3,979: Line 3,979:
=={{header|Swift}}==
=={{header|Swift}}==


<syntaxhighlight lang=swift>struct KPrimeGen: Sequence, IteratorProtocol {
<syntaxhighlight lang="swift">struct KPrimeGen: Sequence, IteratorProtocol {
let k: Int
let k: Int
private(set) var n: Int
private(set) var n: Int
Line 4,026: Line 4,026:
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
{{tcllib|math::numtheory}}
{{tcllib|math::numtheory}}
<syntaxhighlight lang=tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6
package require math::numtheory
package require math::numtheory


Line 4,067: Line 4,067:


=={{header|Tiny BASIC}}==
=={{header|Tiny BASIC}}==
<syntaxhighlight lang=tinybasic>
<syntaxhighlight lang="tinybasic">
REM Almost prime
REM Almost prime
LET K=1
LET K=1
Line 4,162: Line 4,162:
== {{header|TypeScript}} ==
== {{header|TypeScript}} ==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang=javascript>// Almost prime
<syntaxhighlight lang="javascript">// Almost prime


function isKPrime(n: number, k: number): bool {
function isKPrime(n: number, k: number): bool {
Line 4,200: Line 4,200:
=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
{{trans|C}}
{{trans|C}}
<lang>Local(3)
<syntaxhighlight lang="text">Local(3)


For c@ = 1 To 5
For c@ = 1 To 5
Line 4,231: Line 4,231:
Return (b@ = (d@ + (a@ > 1)))</syntaxhighlight>
Return (b@ = (d@ + (a@ > 1)))</syntaxhighlight>
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang>For k = 1 To 5
<syntaxhighlight lang="text">For k = 1 To 5
Print "k = "; k; " : ";
Print "k = "; k; " : ";
i = 2
i = 2
Line 4,268: Line 4,268:


=={{header|VBA}}==
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang=vb>Private Function kprime(ByVal n As Integer, k As Integer) As Boolean
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function kprime(ByVal n As Integer, k As Integer) As Boolean
Dim p As Integer, factors As Integer
Dim p As Integer, factors As Integer
p = 2
p = 2
Line 4,308: Line 4,308:
=={{header|VBScript}}==
=={{header|VBScript}}==
Repurposed the VBScript code for the Prime Decomposition task.
Repurposed the VBScript code for the Prime Decomposition task.
<syntaxhighlight lang=vb>
<syntaxhighlight lang="vb">
For k = 1 To 5
For k = 1 To 5
count = 0
count = 0
Line 4,376: Line 4,376:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Class KPrime
Class KPrime
Line 4,429: Line 4,429:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<syntaxhighlight lang=vlang>fn k_prime(n int, k int) bool {
<syntaxhighlight lang="vlang">fn k_prime(n int, k int) bool {
mut nf := 0
mut nf := 0
mut nn := n
mut nn := n
Line 4,473: Line 4,473:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=ecmascript>var kPrime = Fn.new { |n, k|
<syntaxhighlight lang="ecmascript">var kPrime = Fn.new { |n, k|
var nf = 0
var nf = 0
var i = 2
var i = 2
Line 4,512: Line 4,512:
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
{{works with|Windows XBasic}}
{{works with|Windows XBasic}}
<syntaxhighlight lang=xbasic>
<syntaxhighlight lang="xbasic">
' Almost prime
' Almost prime
PROGRAM "almostprime"
PROGRAM "almostprime"
Line 4,560: Line 4,560:


=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0>func Factors(N); \Return number of (prime) factors in N
<syntaxhighlight lang="xpl0">func Factors(N); \Return number of (prime) factors in N
int N, F, C;
int N, F, C;
[C:= 0; F:= 2;
[C:= 0; F:= 2;
Line 4,599: Line 4,599:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|Lua}}
{{trans|Lua}}
<syntaxhighlight lang=Yabasic>// Returns boolean indicating whether n is k-almost prime
<syntaxhighlight lang="yabasic">// Returns boolean indicating whether n is k-almost prime
sub almostPrime(n, k)
sub almostPrime(n, k)
local divisor, count
local divisor, count
Line 4,646: Line 4,646:


Can't say I entirely understand this algorithm. Uses list comprehension to calculate the outer/tensor product (p10 ⊗ ar).
Can't say I entirely understand this algorithm. Uses list comprehension to calculate the outer/tensor product (p10 ⊗ ar).
<syntaxhighlight lang=zkl>primes:=Utils.Generator(Import("sieve").postponed_sieve);
<syntaxhighlight lang="zkl">primes:=Utils.Generator(Import("sieve").postponed_sieve);
(p10:=ar:=primes.walk(10)).println();
(p10:=ar:=primes.walk(10)).println();
do(4){
do(4){
Line 4,662: Line 4,662:
=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
{{trans|AWK}}
<syntaxhighlight lang=zxbasic>10 FOR k=1 TO 5
<syntaxhighlight lang="zxbasic">10 FOR k=1 TO 5
20 PRINT k;":";
20 PRINT k;":";
30 LET c=0: LET i=1
30 LET c=0: LET i=1