WeixiangYan/CodeTransOcean huggingface dataset:
Task: Almost prime




Go: package main

import "fmt"

func kPrime(n, k int) bool {
    nf := 0
    for i := 2; i <= n; i++ {
        for n%i == 0 {
            if nf == k {
                return false
            }
            nf++
            n /= i
        }
    }
    return nf == k
}

func gen(k, n int) []int {
    r := make([]int, n)
    n = 2
    for i := range r {
        for !kPrime(n, k) {
            n++
        }
        r[i] = n
        n++
    }
    return r
}

func main() {
    for k := 1; k <= 5; k++ {
        fmt.Println(k, gen(k, 10))
    }
}








Ada: with Prime_Numbers, Ada.Text_IO; 
 
procedure Test_Kth_Prime is
   
   package Integer_Numbers is new 
     Prime_Numbers (Natural, 0, 1, 2); 
   use Integer_Numbers;
   
   Out_Length: constant Positive := 10; 
   N: Positive; 
   
begin
   for K in 1 .. 5 loop
      Ada.Text_IO.Put("K =" & Integer'Image(K) &":  ");
      N := 2;
      for I in 1 .. Out_Length loop
	 while Decompose(N)'Length /= K loop
	    N := N + 1;
	 end loop; 
	 Ada.Text_IO.Put(Integer'Image(Integer(N)));
	 N := N + 1;
      end loop;
      Ada.Text_IO.New_Line;
   end loop;
end Test_Kth_Prime;
Task: Almost prime





Java: public class AlmostPrime {
    public static void main(String[] args) {
        for (int k = 1; k <= 5; k++) {
            System.out.print("k = " + k + ":");

            for (int i = 2, c = 0; c < 10; i++) {
                if (kprime(i, k)) {
                    System.out.print(" " + i);
                    c++;
                }
            }

            System.out.println("");
        }
    }

    public static boolean kprime(int n, int k) {
        int f = 0;
        for (int p = 2; f < k && p * p <= n; p++) {
            while (n % p == 0) {
                n /= p;
                f++;
            }
        }
        return f + ((n > 1) ? 1 : 0) == k;
    }
}







Ada: with Prime_Numbers, Ada.Text_IO; 
 
procedure Test_Kth_Prime is
   
   package Integer_Numbers is new 
     Prime_Numbers (Natural, 0, 1, 2); 
   use Integer_Numbers;
   
   Out_Length: constant Positive := 10; 
   N: Positive; 
   
begin
   for K in 1 .. 5 loop
      Ada.Text_IO.Put("K =" & Integer'Image(K) &":  ");
      N := 2;
      for I in 1 .. Out_Length loop
	 while Decompose(N)'Length /= K loop
	    N := N + 1;
	 end loop; 
	 Ada.Text_IO.Put(Integer'Image(Integer(N)));
	 N := N + 1;
      end loop;
      Ada.Text_IO.New_Line;
   end loop;
end Test_Kth_Prime;
Task: Almost prime







Python: from prime_decomposition import decompose
from itertools import islice, count
try: 
    from functools import reduce
except: 
    pass


def almostprime(n, k=2):
    d = decompose(n)
    try:
        terms = [next(d) for i in range(k)]
        return reduce(int.__mul__, terms, 1) == n
    except:
        return False

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))))





Ada: with Prime_Numbers, Ada.Text_IO; 
 
procedure Test_Kth_Prime is
   
   package Integer_Numbers is new 
     Prime_Numbers (Natural, 0, 1, 2); 
   use Integer_Numbers;
   
   Out_Length: constant Positive := 10; 
   N: Positive; 
   
begin
   for K in 1 .. 5 loop
      Ada.Text_IO.Put("K =" & Integer'Image(K) &":  ");
      N := 2;
      for I in 1 .. Out_Length loop
	 while Decompose(N)'Length /= K loop
	    N := N + 1;
	 end loop; 
	 Ada.Text_IO.Put(Integer'Image(Integer(N)));
	 N := N + 1;
      end loop;
      Ada.Text_IO.New_Line;
   end loop;
end Test_Kth_Prime;
Task: Almost prime








VB: Private Function kprime(ByVal n As Integer, k As Integer) As Boolean
    Dim p As Integer, factors As Integer
    p = 2
    factors = 0
    Do While factors < k And p * p <= n
        Do While n Mod p = 0
            n = n / p
            factors = factors + 1
        Loop
        p = p + 1
    Loop
    factors = factors - (n > 1) 
    kprime = factors = k
End Function
 
Private Sub almost_primeC()
    Dim nextkprime As Integer, count As Integer
    Dim k As Integer
    For k = 1 To 5
        Debug.Print "k ="; k; ":";
        nextkprime = 2
        count = 0
        Do While count < 10
            If kprime(nextkprime, k) Then
                Debug.Print " "; Format(CStr(nextkprime), "@@@@@");
                count = count + 1
            End If
            nextkprime = nextkprime + 1
        Loop
        Debug.Print
    Next k
End Sub




Ada: with Prime_Numbers, Ada.Text_IO; 
 
procedure Test_Kth_Prime is
   
   package Integer_Numbers is new 
     Prime_Numbers (Natural, 0, 1, 2); 
   use Integer_Numbers;
   
   Out_Length: constant Positive := 10; 
   N: Positive; 
   
begin
   for K in 1 .. 5 loop
      Ada.Text_IO.Put("K =" & Integer'Image(K) &":  ");
      N := 2;
      for I in 1 .. Out_Length loop
	 while Decompose(N)'Length /= K loop
	    N := N + 1;
	 end loop; 
	 Ada.Text_IO.Put(Integer'Image(Integer(N)));
	 N := N + 1;
      end loop;
      Ada.Text_IO.New_Line;
   end loop;
end Test_Kth_Prime;
Task: Almost prime

C: #include <stdio.h>

int kprime(int n, int k)
{
	int p, f = 0;
	for (p = 2; f < k && p*p <= n; p++)
		while (0 == n % p)
			n /= p, f++;

	return f + (n > 1) == k;
}

int main(void)
{
	int i, c, k;

	for (k = 1; k <= 5; k++) {
		printf("k = %d:", k);

		for (i = 2, c = 0; c < 10; i++)
			if (kprime(i, k)) {
				printf(" %d", i);
				c++;
			}

		putchar('\n');
	}

	return 0;
}
Task: Almost prime



C#: using System;
using System.Collections.Generic;
using System.Linq;

namespace AlmostPrime
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (int k in Enumerable.Range(1, 5))
            {
                KPrime kprime = new KPrime() { K = k };
                Console.WriteLine("k = {0}: {1}",
                    k, string.Join<int>(" ", kprime.GetFirstN(10)));
            }
        }
    }

    class KPrime
    {
        public int K { get; set; }

        public bool IsKPrime(int number)
        {
            int primes = 0;
            for (int p = 2; p * p <= number && primes < K; ++p)
            {
                while (number % p == 0 && primes < K)
                {
                    number /= p;
                    ++primes;
                }
            }
            if (number > 1)
            {
                ++primes;
            }
            return primes == K;
        }

        public List<int> GetFirstN(int n)
        {
            List<int> result = new List<int>();
            for (int number = 2; result.Count < n; ++number)
            {
                if (IsKPrime(number))
                {
                    result.Add(number);
                }
            }
            return result;
        }
    }
}
Task: Almost prime


C++: #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>

bool k_prime(unsigned n, unsigned k) {
    unsigned f = 0;
    for (unsigned p = 2; f < k && p * p <= n; p++)
        while (0 == n % p) { n /= p; f++; }
    return f + (n > 1 ? 1 : 0) == k;
}

std::list<unsigned> primes(unsigned k, unsigned n)  {
    std::list<unsigned> list;
    for (unsigned i = 2;list.size() < n;i++)
        if (k_prime(i, k)) list.push_back(i);
    return list;
}

int main(const int argc, const char* argv[]) {
    using namespace std;
    for (unsigned k = 1; k <= 5; k++) {
        ostringstream os("");
        const list<unsigned> l = primes(k, 10);
        for (list<unsigned>::const_iterator i = l.begin(); i != l.end(); i++)
            os << setw(4) << *i;
        cout << "k = " << k << ':' << os.str() << endl;
    }

	return EXIT_SUCCESS;
}
Task: Almost prime





Java: public class AlmostPrime {
    public static void main(String[] args) {
        for (int k = 1; k <= 5; k++) {
            System.out.print("k = " + k + ":");

            for (int i = 2, c = 0; c < 10; i++) {
                if (kprime(i, k)) {
                    System.out.print(" " + i);
                    c++;
                }
            }

            System.out.println("");
        }
    }

    public static boolean kprime(int n, int k) {
        int f = 0;
        for (int p = 2; f < k && p * p <= n; p++) {
            while (n % p == 0) {
                n /= p;
                f++;
            }
        }
        return f + ((n > 1) ? 1 : 0) == k;
    }
}
Task: Almost prime







Python: from prime_decomposition import decompose
from itertools import islice, count
try: 
    from functools import reduce
except: 
    pass


def almostprime(n, k=2):
    d = decompose(n)
    try:
        terms = [next(d) for i in range(k)]
        return reduce(int.__mul__, terms, 1) == n
    except:
        return False

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))))
Task: Almost prime








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
Task: Almost prime




Go: package main

import "fmt"

func kPrime(n, k int) bool {
    nf := 0
    for i := 2; i <= n; i++ {
        for n%i == 0 {
            if nf == k {
                return false
            }
            nf++
            n /= i
        }
    }
    return nf == k
}

func gen(k, n int) []int {
    r := make([]int, n)
    n = 2
    for i := range r {
        for !kPrime(n, k) {
            n++
        }
        r[i] = n
        n++
    }
    return r
}

func main() {
    for k := 1; k <= 5; k++ {
        fmt.Println(k, gen(k, 10))
    }
}
Task: Almost prime



C#: using System;
using System.Collections.Generic;
using System.Linq;

namespace AlmostPrime
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (int k in Enumerable.Range(1, 5))
            {
                KPrime kprime = new KPrime() { K = k };
                Console.WriteLine("k = {0}: {1}",
                    k, string.Join<int>(" ", kprime.GetFirstN(10)));
            }
        }
    }

    class KPrime
    {
        public int K { get; set; }

        public bool IsKPrime(int number)
        {
            int primes = 0;
            for (int p = 2; p * p <= number && primes < K; ++p)
            {
                while (number % p == 0 && primes < K)
                {
                    number /= p;
                    ++primes;
                }
            }
            if (number > 1)
            {
                ++primes;
            }
            return primes == K;
        }

        public List<int> GetFirstN(int n)
        {
            List<int> result = new List<int>();
            for (int number = 2; result.Count < n; ++number)
            {
                if (IsKPrime(number))
                {
                    result.Add(number);
                }
            }
            return result;
        }
    }
}








Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime


C++: #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>

bool k_prime(unsigned n, unsigned k) {
    unsigned f = 0;
    for (unsigned p = 2; f < k && p * p <= n; p++)
        while (0 == n % p) { n /= p; f++; }
    return f + (n > 1 ? 1 : 0) == k;
}

std::list<unsigned> primes(unsigned k, unsigned n)  {
    std::list<unsigned> list;
    for (unsigned i = 2;list.size() < n;i++)
        if (k_prime(i, k)) list.push_back(i);
    return list;
}

int main(const int argc, const char* argv[]) {
    using namespace std;
    for (unsigned k = 1; k <= 5; k++) {
        ostringstream os("");
        const list<unsigned> l = primes(k, 10);
        for (list<unsigned>::const_iterator i = l.begin(); i != l.end(); i++)
            os << setw(4) << *i;
        cout << "k = " << k << ':' << os.str() << endl;
    }

	return EXIT_SUCCESS;
}









Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime

C: #include <stdio.h>

int kprime(int n, int k)
{
	int p, f = 0;
	for (p = 2; f < k && p*p <= n; p++)
		while (0 == n % p)
			n /= p, f++;

	return f + (n > 1) == k;
}

int main(void)
{
	int i, c, k;

	for (k = 1; k <= 5; k++) {
		printf("k = %d:", k);

		for (i = 2, c = 0; c < 10; i++)
			if (kprime(i, k)) {
				printf(" %d", i);
				c++;
			}

		putchar('\n');
	}

	return 0;
}










Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime




Go: package main

import "fmt"

func kPrime(n, k int) bool {
    nf := 0
    for i := 2; i <= n; i++ {
        for n%i == 0 {
            if nf == k {
                return false
            }
            nf++
            n /= i
        }
    }
    return nf == k
}

func gen(k, n int) []int {
    r := make([]int, n)
    n = 2
    for i := range r {
        for !kPrime(n, k) {
            n++
        }
        r[i] = n
        n++
    }
    return r
}

func main() {
    for k := 1; k <= 5; k++ {
        fmt.Println(k, gen(k, 10))
    }
}







Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime





Java: public class AlmostPrime {
    public static void main(String[] args) {
        for (int k = 1; k <= 5; k++) {
            System.out.print("k = " + k + ":");

            for (int i = 2, c = 0; c < 10; i++) {
                if (kprime(i, k)) {
                    System.out.print(" " + i);
                    c++;
                }
            }

            System.out.println("");
        }
    }

    public static boolean kprime(int n, int k) {
        int f = 0;
        for (int p = 2; f < k && p * p <= n; p++) {
            while (n % p == 0) {
                n /= p;
                f++;
            }
        }
        return f + ((n > 1) ? 1 : 0) == k;
    }
}






Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime







Python: from prime_decomposition import decompose
from itertools import islice, count
try: 
    from functools import reduce
except: 
    pass


def almostprime(n, k=2):
    d = decompose(n)
    try:
        terms = [next(d) for i in range(k)]
        return reduce(int.__mul__, terms, 1) == n
    except:
        return False

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))))




Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime








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



Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime
Task: Almost prime






PHP: <?php


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;
}
?>





Fortran: program almost_prime
    use iso_fortran_env, only: output_unit
    implicit none

    integer :: i, c, k

    do k = 1, 5
        write(output_unit,'(A3,x,I0,x,A1,x)', advance="no") "k =", k, ":"
        i = 2
        c = 0
        do
            if (c >= 10) exit

            if (kprime(i, k)) then
                write(output_unit,'(I0,x)', advance="no") i
                c = c + 1
            end if
            i = i + 1
        end do
        write(output_unit,*)
    end do
contains
    pure function kprime(n, k)
        integer, intent(in) :: n, k
        logical             :: kprime
        integer             :: p, f, i

        kprime = .false.

        f = 0
        i = n

        do p = 2, n
            do
                if (modulo(i, p) /= 0) exit

                if (f == k) return
                f = f + 1
                i = i / p
            end do
        end do

        kprime = f==k
    end function kprime
end program almost_prime