2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,8 +1,16 @@
|
|||
A [[wp:Almost prime|k-Almost-prime]] is a natural number <math>n</math> that is the product of <math>k</math> (possibly identical) primes.
|
||||
So, for example, 1-almost-primes, where <math>k=1</math>, are the prime numbers themselves; 2-almost-primes are the [[Semiprime|semiprimes]].
|
||||
A [[wp:Almost prime|k-Almost-prime]] is a natural number <math>n</math> that is the product of <math>k</math> (possibly identical) primes.
|
||||
|
||||
The task is to write a function/method/subroutine/... that generates k-almost primes and use it to create a table here of the first ten members of k-Almost primes for <math>1 <= K <= 5</math>.
|
||||
|
||||
;Cf.
|
||||
* [[Semiprime]]
|
||||
* [[:Category:Prime Numbers]]
|
||||
;Example:
|
||||
1-almost-primes, where <math>k=1</math>, are the prime numbers themselves.
|
||||
<br>2-almost-primes, where <math>k=2</math>, are the [[Semiprime|semiprimes]].
|
||||
|
||||
|
||||
;Task:
|
||||
Write a function/method/subroutine/... that generates k-almost primes and use it to create a table here of the first ten members of k-Almost primes for <math>1 <= K <= 5</math>.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Semiprime]]
|
||||
* [[:Category:Prime Numbers]]
|
||||
<br><br>
|
||||
|
|
|
|||
25
Task/Almost-prime/AWK/almost-prime.awk
Normal file
25
Task/Almost-prime/AWK/almost-prime.awk
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# syntax: GAWK -f ALMOST_PRIME.AWK
|
||||
BEGIN {
|
||||
for (k=1; k<=5; k++) {
|
||||
printf("%d:",k)
|
||||
c = 0
|
||||
i = 1
|
||||
while (c < 10) {
|
||||
if (kprime(++i,k)) {
|
||||
printf(" %d",i)
|
||||
c++
|
||||
}
|
||||
}
|
||||
printf("\n")
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
function kprime(n,k, f,p) {
|
||||
for (p=2; f<k && p*p<=n; p++) {
|
||||
while (n % p == 0) {
|
||||
n /= p
|
||||
f++
|
||||
}
|
||||
}
|
||||
return(f + (n > 1) == k)
|
||||
}
|
||||
32
Task/Almost-prime/C++/almost-prime.cpp
Normal file
32
Task/Almost-prime/C++/almost-prime.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#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;
|
||||
}
|
||||
23
Task/Almost-prime/Clojure/almost-prime.clj
Normal file
23
Task/Almost-prime/Clojure/almost-prime.clj
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(ns clojure.examples.almostprime
|
||||
(:gen-class))
|
||||
|
||||
(defn divisors [n]
|
||||
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
|
||||
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
|
||||
(if div ; div = nil (if no divisor found else its the divisor)
|
||||
(into [] (concat (divisors div) (divisors (/ n div)))) ; Concat the two divisors of the two divisors
|
||||
[n]))) ; Number is prime so only itself as a divisor
|
||||
|
||||
(defn divisors-k [k n]
|
||||
" Finds n numbers with k divisors. Does this by looping through integers 2, 3, ... filtering (passing) ones with k divisors and
|
||||
taking the first n "
|
||||
(->> (iterate inc 2) ; infinite sequence of numbers starting at 2
|
||||
(map divisors) ; compute divisor of each element of sequence
|
||||
(filter #(= (count %) k)) ; filter to take only elements with k divisors
|
||||
(take n) ; take n elements from filtered sequence
|
||||
(map #(apply * %)))) ; compute number by taking product of divisors
|
||||
|
||||
(println (for [k (range 1 6)]
|
||||
(println "k:" k (divisors-k k 10))))
|
||||
|
||||
}
|
||||
25
Task/Almost-prime/Kotlin/almost-prime.kotlin
Normal file
25
Task/Almost-prime/Kotlin/almost-prime.kotlin
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
fun Int.k_prime(x: Int): Boolean {
|
||||
var n = x
|
||||
var f = 0
|
||||
var p = 2
|
||||
while (f < this && p * p <= n) {
|
||||
while (0 == n % p) { n /= p; f++ }
|
||||
p++
|
||||
}
|
||||
return f + (if (n > 1) 1 else 0) == this
|
||||
}
|
||||
|
||||
fun Int.primes(n : Int) : List<Int> {
|
||||
var i = 2
|
||||
var list = listOf<Int>()
|
||||
while (list.size < n) {
|
||||
if (k_prime(i)) list += i
|
||||
i++
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for (k in 1..5)
|
||||
println("k = $k: " + k.primes(10))
|
||||
}
|
||||
34
Task/Almost-prime/Lua/almost-prime.lua
Normal file
34
Task/Almost-prime/Lua/almost-prime.lua
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
-- Returns boolean indicating whether n is k-almost prime
|
||||
function almostPrime (n, k)
|
||||
local divisor, count = 2, 0
|
||||
while count < k + 1 and n ~= 1 do
|
||||
if n % divisor == 0 then
|
||||
n = n / divisor
|
||||
count = count + 1
|
||||
else
|
||||
divisor = divisor + 1
|
||||
end
|
||||
end
|
||||
return count == k
|
||||
end
|
||||
|
||||
-- Generates table containing first ten k-almost primes for given k
|
||||
function kList (k)
|
||||
local n, kTab = 2^k, {}
|
||||
while #kTab < 10 do
|
||||
if almostPrime(n, k) then
|
||||
table.insert(kTab, n)
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
return kTab
|
||||
end
|
||||
|
||||
-- Main procedure, displays results from five calls to kList()
|
||||
for k = 1, 5 do
|
||||
io.write("k=" .. k .. ": ")
|
||||
for _, v in pairs(kList(k)) do
|
||||
io.write(v .. ", ")
|
||||
end
|
||||
print("...")
|
||||
end
|
||||
|
|
@ -6,6 +6,6 @@ sub is-k-almost-prime($n is copy, $k) returns Bool {
|
|||
}
|
||||
|
||||
for 1 .. 5 -> $k {
|
||||
say .[^10]
|
||||
say ~.[^10]
|
||||
given grep { is-k-almost-prime($_, $k) }, 2 .. *
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,23 @@
|
|||
constant factory = 0..* Z=> (0, 0, map { +factors($_) }, 2..*);
|
||||
constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;
|
||||
|
||||
sub almost($n) { map *.key, grep *.value == $n, factory }
|
||||
multi sub factors(1) { 1 }
|
||||
multi sub factors(Int $remainder is copy) {
|
||||
gather for @primes -> $factor {
|
||||
# if remainder < factor², we're done
|
||||
if $factor * $factor > $remainder {
|
||||
take $remainder if $remainder > 1;
|
||||
last;
|
||||
}
|
||||
# How many times can we divide by this prime?
|
||||
while $remainder %% $factor {
|
||||
take $factor;
|
||||
last if ($remainder div= $factor) === 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
say almost($_)[^10] for 1..5;
|
||||
constant @factory = lazy 0..* Z=> flat (0, 0, map { +factors($_) }, 2..*);
|
||||
|
||||
sub almost($n) { map *.key, grep *.value == $n, @factory }
|
||||
|
||||
put almost($_)[^10] for 1..5;
|
||||
|
|
|
|||
49
Task/Almost-prime/R/almost-prime.r
Normal file
49
Task/Almost-prime/R/almost-prime.r
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#===============================================================
|
||||
# Find k-Almost-primes
|
||||
# R implementation
|
||||
#===============================================================
|
||||
#---------------------------------------------------------------
|
||||
# Function for prime factorization from Rosetta Code
|
||||
#---------------------------------------------------------------
|
||||
|
||||
findfactors <- function(n) {
|
||||
d <- c()
|
||||
div <- 2; nxt <- 3; rest <- n
|
||||
while( rest != 1 ) {
|
||||
while( rest%%div == 0 ) {
|
||||
d <- c(d, div)
|
||||
rest <- floor(rest / div)
|
||||
}
|
||||
div <- nxt
|
||||
nxt <- nxt + 2
|
||||
}
|
||||
d
|
||||
}
|
||||
|
||||
#---------------------------------------------------------------
|
||||
# Find k-Almost-primes
|
||||
#---------------------------------------------------------------
|
||||
|
||||
almost_primes <- function(n = 10, k = 5) {
|
||||
|
||||
# Set up matrix for storing of the results
|
||||
|
||||
res <- matrix(NA, nrow = k, ncol = n)
|
||||
rownames(res) <- paste("k = ", 1:k, sep = "")
|
||||
colnames(res) <- rep("", n)
|
||||
|
||||
# Loop over k
|
||||
|
||||
for (i in 1:k) {
|
||||
|
||||
tmp <- 1
|
||||
|
||||
while (any(is.na(res[i, ]))) { # Keep looping if there are still missing entries in the result-matrix
|
||||
if (length(findfactors(tmp)) == i) { # Check number of factors
|
||||
res[i, which.max(is.na(res[i, ]))] <- tmp
|
||||
}
|
||||
tmp <- tmp + 1
|
||||
}
|
||||
}
|
||||
print(res)
|
||||
}
|
||||
|
|
@ -1,28 +1,32 @@
|
|||
/*REXX program computes & displays N numbers of the first K k─almost primes.*/
|
||||
parse arg N K . /*get optional arguments from the C.L. */
|
||||
if N=='' then N=10 /*N not specified? Then use default.*/
|
||||
if K=='' then K= 5; w=length(k) /*K " " " " " */
|
||||
/*W: is the width of K, used for output*/
|
||||
do m=1 for K; $=2**m; fir=$ /*generate & assign 1st k─almost prime.*/
|
||||
#=1; if #==N then leave /*#: k─almost primes; Enough are found?*/
|
||||
#=2; $=$ 3*(2**(m-1)) /*generate & append 2nd k─almost prime.*/
|
||||
do j=fir+fir+1 until #==N /*process an almost-prime N times.*/
|
||||
if #factr(j)\==m then iterate /*not the correct k─almost prime? */
|
||||
#=#+1; $=$ j /*bump K─almost counter; append it to $*/
|
||||
end /*j*/ /* [↑] generate N k─almost primes.*/
|
||||
say right(m,w)"─almost ("N') primes:' $ /*displays " " " " */
|
||||
end /*m*/ /* [↑] display a line for each K-prime*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
#factr: procedure; parse arg x 1 z /*defines X and Z to the argument. */
|
||||
do f=0 while z//2==0; z=z%2; end /*÷ by 2s.*/
|
||||
do f=f while z//3==0; z=z%3; end /*÷ " 3s.*/
|
||||
do f=f while z//5==0; z=z%5; end /*÷ " 5s.*/
|
||||
j=5
|
||||
do y=0 by 2; j=j+2+y//4 /*insure J isn't divisible by three. */
|
||||
parse var j '' -1 _ /*obtain the right─most decimal digit. */
|
||||
if _==5 then iterate /*fast check for divisible by five. */
|
||||
if j>z then leave /*is number reduced to the smallest # ?*/
|
||||
do f=f+1 while z//j==0; z=z%j; end; f=f-1 /*÷ by Js.*/
|
||||
end /*y*/ /* [↑] find all the factors in X. */
|
||||
return max(f,1) /*if prime (f==0), then return 1. */
|
||||
/*REXX program computes and displays the first N K─almost primes from 1 ── ►K. */
|
||||
parse arg N K . /*get optional arguments from the C.L. */
|
||||
if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
|
||||
if K=='' | K=="," then K= 5 /*K " " " " " */
|
||||
/*W: is the width of K, used for output*/
|
||||
do m=1 for K; $=2**m; fir=$ /*generate & assign 1st K─almost prime.*/
|
||||
#=1; if #==N then leave /*#: K─almost primes; Enough are found?*/
|
||||
#=2; $=$ 3*(2**(m-1)) /*generate & append 2nd K─almost prime.*/
|
||||
if #==N then leave /*#: K─almost primes; Enough are found?*/
|
||||
if m==1 then _=fir+fir /* [↓] gen & append 3rd K─almost prime*/
|
||||
else do; _=9*(2**(m-2)); #=3; $=$ _; end
|
||||
do j=_+m-1 until #==N /*process an K─almost prime N times.*/
|
||||
if #factr()\==m then iterate /*not the correct K─almost prime? */
|
||||
#=#+1; $=$ j /*bump K─almost counter; append it to $*/
|
||||
end /*j*/ /* [↑] generate N K─almost primes.*/
|
||||
say right(m, length(K))"─almost ("N') primes:' $
|
||||
end /*m*/ /* [↑] display a line for each K─prime*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
#factr: z=j; do f=0 while z//2==0; z=z%2; end /*divisible by 2. */
|
||||
do f=f while z//3==0; z=z%3; end /*divisible " 3. */
|
||||
do f=f while z//5==0; z=z%5; end /*divisible " 5. */
|
||||
do f=f while z//7==0; z=z%7; end /*divisible " 7. */
|
||||
do i=11 by 6 while i<=z /*insure I isn't divisible by three. */
|
||||
parse var i '' -1 _ /*obtain the right─most decimal digit. */
|
||||
/* [↓] fast check for divisible by 5. */
|
||||
if _\==5 then do; do f=f+1 while z//i==0; z=z%i; end; f=f-1; end /*divisible by I. */
|
||||
if _==3 then iterate /*fast check for X divisible by five.*/
|
||||
x=i+2; do f=f+1 while z//x==0; z=z%x; end; f=f-1 /*divisible by X. */
|
||||
end /*i*/ /* [↑] find all the factors in Z. */
|
||||
|
||||
return max(f, 1) /*if prime (f==0), then return unity.*/
|
||||
|
|
|
|||
18
Task/Almost-prime/ZX-Spectrum-Basic/almost-prime.zx
Normal file
18
Task/Almost-prime/ZX-Spectrum-Basic/almost-prime.zx
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue