September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
34
Task/Almost-prime/Groovy/almost-prime.groovy
Normal file
34
Task/Almost-prime/Groovy/almost-prime.groovy
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
public class almostprime
|
||||
{
|
||||
public static boolean kprime(int n,int k)
|
||||
{
|
||||
int i,div=0;
|
||||
for(i=2;(i*i <= n) && (div<k);i++)
|
||||
{
|
||||
while(n%i==0)
|
||||
{
|
||||
n = n/i;
|
||||
div++;
|
||||
}
|
||||
}
|
||||
return div + ((n > 1)?1:0) == k;
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int i,l,k;
|
||||
for(k=1;k<=5;k++)
|
||||
{
|
||||
println("k = " + k + ":");
|
||||
l = 0;
|
||||
for(i=2;l<10;i++)
|
||||
{
|
||||
if(kprime(i,k))
|
||||
{
|
||||
print(i + " ");
|
||||
l++;
|
||||
}
|
||||
}
|
||||
println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +1,27 @@
|
|||
public class AlmostPrime
|
||||
{
|
||||
public static void main(String args[])
|
||||
{
|
||||
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++)
|
||||
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)
|
||||
{
|
||||
public static boolean kprime(int n, int k) {
|
||||
int f = 0;
|
||||
for (int p = 2; f < k && p*p <= n; p++)
|
||||
while (0 == n % p){
|
||||
n /= p;
|
||||
f++;
|
||||
for (int p = 2; f < k && p * p <= n; p++) {
|
||||
while (n % p == 0) {
|
||||
n /= p;
|
||||
f++;
|
||||
}
|
||||
return f + ((n > 1)?1:0) == k;
|
||||
}
|
||||
return f + ((n > 1) ? 1 : 0) == k;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
24
Task/Almost-prime/JavaScript/almost-prime.js
Normal file
24
Task/Almost-prime/JavaScript/almost-prime.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function almostPrime (n, k) {
|
||||
var divisor = 2, count = 0
|
||||
while(count < k + 1 && n != 1) {
|
||||
if (n % divisor == 0) {
|
||||
n = n / divisor
|
||||
count = count + 1
|
||||
} else {
|
||||
divisor++
|
||||
}
|
||||
}
|
||||
return count == k
|
||||
}
|
||||
|
||||
for (var k = 1; k <= 5; k++) {
|
||||
document.write("<br>k=", k, ": ")
|
||||
var count = 0, n = 0
|
||||
while (count <= 10) {
|
||||
n++
|
||||
if (almostPrime(n, k)) {
|
||||
document.write(n, " ")
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Task/Almost-prime/Nim/almost-prime.nim
Normal file
23
Task/Almost-prime/Nim/almost-prime.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
proc prime(k: int, listLen: int): seq[int] =
|
||||
result = @[]
|
||||
var
|
||||
test: int = 2
|
||||
curseur: int = 0
|
||||
while curseur < listLen:
|
||||
var
|
||||
i: int = 2
|
||||
compte = 0
|
||||
n = test
|
||||
while i <= n:
|
||||
if (n mod i)==0:
|
||||
n = n div i
|
||||
compte += 1
|
||||
else:
|
||||
i += 1
|
||||
if compte == k:
|
||||
result.add(test)
|
||||
curseur += 1
|
||||
test += 1
|
||||
|
||||
for k in 1..5:
|
||||
echo "k = ",k," : ",prime(k,10)
|
||||
|
|
@ -1,33 +1,35 @@
|
|||
/*REXX program displays the N numbers of the first K k-almost primes*/
|
||||
parse arg N K . /*get the arguments from the C.L.*/
|
||||
if N=='' then N=10 /*No N? Then use the default.*/
|
||||
if K=='' then K=5 /* " K? " " " " */
|
||||
/* [↓] display one line per K.*/
|
||||
do m=1 for K; $=2**m; fir=$ /*generate the 1st k_almost prime*/
|
||||
#=1; if #==N then leave /*# k-almost primes; 'nuff found?*/
|
||||
sec=3*(2**(m-1)); $=$ sec; #=2 /*generate the 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 /*bump the k-almost prime counter*/
|
||||
$=$ j /*append k-almost prime to list. */
|
||||
end /*j*/ /* [↑] gen N k-almost primes.*/
|
||||
say N right(m,4)"-almost primes:" $ /*display the k-almost primes.*/
|
||||
end /*m*/ /* [↑] display a line for each K*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────#FACTR subroutine───────────────────*/
|
||||
#factr: procedure;parse arg x 1 z; f=0 /*defines X and Z to the arg.*/
|
||||
if x<2 then return 0 /*invalid number? Then return 0.*/
|
||||
do j=2 to 5; if j\==4 then call .#factr; end /*fast factoring.*/
|
||||
j=5 /*start were we left off (J=5). */
|
||||
do y=0 by 2; j=j+2 + y//4 /*insure it's not divisible by 3.*/
|
||||
if right(j,1)==5 then iterate /*fast check for divisible by 5.*/
|
||||
if j>z then leave /*number reduced to a wee number?*/
|
||||
call .#factr /*go add other factors to count. */
|
||||
end /*y*/ /* [↑] find all factors in X. */
|
||||
return max(f,1) /*if prime (f==0), then return 1.*/
|
||||
/*──────────────────────────────────.#FACTR subroutine──────────────────*/
|
||||
.#factr: do f=f+1 while z//j==0 /*keep dividing until we can't. */
|
||||
z=z%j /*perform an (%) integer divide.*/
|
||||
end /*while*/ /* [↑] whittle down the Z num.*/
|
||||
f=f-1 /*adjust the count of factors. */
|
||||
return
|
||||
/*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 f=f while z//11==0; z=z%11; end /*divisible " 11.*/
|
||||
do f=f while z//13==0; z=z%13; end /*divisible " 13.*/
|
||||
do p=17 by 6 while p<=z /*insure P isn't divisible by three. */
|
||||
parse var p '' -1 _ /*obtain the right─most decimal digit. */
|
||||
/* [↓] fast check for divisible by 5. */
|
||||
if _\==5 then do; do f=f+1 while z//p==0; z=z%p; end; f=f-1; end /*÷ by P? */
|
||||
if _ ==3 then iterate /*fast check for X divisible by five.*/
|
||||
x=p+2; do f=f+1 while z//x==0; z=z%x; end; f=f-1 /*÷ by X? */
|
||||
end /*i*/ /* [↑] find all the factors in Z. */
|
||||
|
||||
if f==0 then return 1 /*if prime (f==0), then return unity.*/
|
||||
return f /*return to invoker the number of divs.*/
|
||||
|
|
|
|||
|
|
@ -1,35 +1,66 @@
|
|||
/*REXX program displays the N numbers of the first K k-almost primes*/
|
||||
parse arg N K . /*get the arguments from the C.L.*/
|
||||
if N=='' then N=10 /*No N? Then use the default.*/
|
||||
if K=='' then K=5 /* " K? " " " " */
|
||||
/* [↓] display one line per K.*/
|
||||
do m=1 for K; $=2**m; fir=$ /*generate the 1st k_almost prime*/
|
||||
#=1; if #==N then leave /*# k-almost primes; 'nuff found?*/
|
||||
sec=3*(2**(m-1)); $=$ sec; #=2 /*generate the 2nd k-almost prime*/
|
||||
do j=fir+fir+1 until #==N /*process an almost-prime N times*/
|
||||
if #factL(j,m)\==m then iterate /*not the correct k-almost prime?*/
|
||||
#=#+1 /*bump the k-almost prime counter*/
|
||||
$=$ j /*append k-almost prime to list. */
|
||||
end /*j*/ /* [↑] gen N k-almost primes.*/
|
||||
say N right(m,4)"-almost primes:" $ /*display the k-almost primes.*/
|
||||
end /*m*/ /* [↑] display a line for each K*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────#FACTL subroutine───────────────────*/
|
||||
#factL: procedure; parse arg x 1 z,L /*defines X and Z to the arg.*/
|
||||
f=0; if x<2 then return 0 /*invalid number? Then return 0.*/
|
||||
do j=2 to 5; if j\==4 then call .#factL; end /*fast factoring.*/
|
||||
if f>L then return f /*#factors > L ? Then too many.*/
|
||||
j=5 /*start were we left off (J=5). */
|
||||
do y=0 by 2; j=j+2 + y//4 /*insure it's not divisible by 3.*/
|
||||
if right(j,1)==5 then iterate /*fast check for divisible by 5.*/
|
||||
if j>z then leave /*number reduced to a wee number?*/
|
||||
call .#factL /*go add other factors to count. */
|
||||
if f>L then return f /*#factors > L ? Then too many.*/
|
||||
end /*y*/ /* [↑] find all factors in X. */
|
||||
return max(f,1) /*if prime (f==0), then return 1.*/
|
||||
/*──────────────────────────────────.#FACTL subroutine──────────────────*/
|
||||
.#factL: do f=f+1 while z//j==0 /*keep dividing until we can't. */
|
||||
z=z%j /*perform an (%) integer divide.*/
|
||||
end /*while*/ /* [↑] whittle down the Z num.*/
|
||||
f=f-1 /*adjust the count of factors. */
|
||||
return
|
||||
/*REXX program computes and displays the first N K─almost primes from 1 ──► K. */
|
||||
parse arg N K . /*obtain optional arguments from the CL*/
|
||||
if N=='' | N==',' then N=10 /*N not specified? Then use default.*/
|
||||
if K=='' | K==',' then K= 5 /*K " " " " " */
|
||||
nn=N; N=abs(N); w=length(K) /*N positive? Then show K─almost primes*/
|
||||
limit= (2**K) * N / 2 /*this is the limit for most K-primes. */
|
||||
if N==1 then limit=limit * 2 /* " " " " " a N of 1.*/
|
||||
if K==1 then limit=limit * 4 /* " " " " " a K─prime " 2.*/
|
||||
if K==2 then limit=limit * 2 /* " " " " " " " " 4.*/
|
||||
if K==3 then limit=limit * 3 % 2 /* " " " " " " " " 8.*/
|
||||
call genPrimes limit + 1 /*generate primes up to the LIMIT + 1.*/
|
||||
say 'The highest prime computed: ' @.# " (under the limit of " limit').'
|
||||
say /* [↓] define where 1st K─prime is odd*/
|
||||
d.=0; d.2= 2; d.3 = 4; d.4 = 7; d.5 = 13; d.6 = 22; d.7 = 38; d.8=63
|
||||
d.9=102; d.10=168; d.11=268; d.12=426; d.13=673; d.14=1064
|
||||
d!=0
|
||||
do m=1 for K; d!=max(d!,d.m) /*generate & assign 1st K─almost prime.*/
|
||||
mr=right(m,w); mm=m-1
|
||||
|
||||
$=; do #=1 to min(N, d!) /*assign some doubled K─almost primes. */
|
||||
$=$ d.mm.# * 2
|
||||
end /*#*/
|
||||
#=#-1
|
||||
if m==1 then from=2
|
||||
else from=1 + word($, words($) )
|
||||
|
||||
do j=from 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.*/
|
||||
|
||||
if nn>0 then say mr"─almost ("N') primes:' $
|
||||
else say ' the last' mr "K─almost prime: " word($, words($))
|
||||
/* [↓] assign K─almost primes.*/
|
||||
do q=1 for #; d.m.q=word($,q) ; end /*q*/
|
||||
do q=1 for #; if d.m.q\==d.mm.q*2 then leave; end /*q*/
|
||||
/* [↑] count doubly-duplicates*/
|
||||
/*──── say copies('─',40) 'for ' m", " q-1 'numbers were doubly─duplicated.' ────*/
|
||||
/*──── say ────*/
|
||||
end /*m*/ /* [↑] display a line for each K─prime*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
factr: if #.j\==. then return #.j
|
||||
z=j; do f=0 while z// 2==0; z=z% 2; end /*÷ by 2*/
|
||||
do f=f while z// 3==0; z=z% 3; end /*÷ " 3*/
|
||||
do f=f while z// 5==0; z=z% 5; end /*÷ " 5*/
|
||||
do f=f while z// 7==0; z=z% 7; end /*÷ " 7*/
|
||||
do f=f while z//11==0; z=z%11; end /*÷ " 11*/
|
||||
do f=f while z//13==0; z=z%13; end /*÷ " 13*/
|
||||
do f=f while z//17==0; z=z%17; end /*÷ " 17*/
|
||||
do f=f while z//19==0; z=z%19; end /*÷ " 19*/
|
||||
|
||||
do i=9 while @.i<=z; d=@.i /*divide by some higher primes. */
|
||||
do f=f while z//d==0; z=z%d; end /*is Z divisible by the prime D ? */
|
||||
end /*i*/ /* [↑] find all factors in Z. */
|
||||
|
||||
if f==0 then f=1; #.j=f; return f /*Is prime (f≡0)? Then return unity. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
genPrimes: arg x; @.=; @.1=2; @.2=3; #.=.; #=2; s.#=@.#**2
|
||||
do j=@.# +2 by 2 to x /*only find odd primes from here on. */
|
||||
do p=2 while s.p<=j /*divide by some known low odd primes. */
|
||||
if j//@.p==0 then iterate j /*Is J divisible by X? Then ¬ prime.*/
|
||||
end /*p*/ /* [↓] a prime (J) has been found. */
|
||||
#=#+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.*/
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
/*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.*/
|
||||
|
|
@ -1,17 +1,18 @@
|
|||
func is_k_almost_prime(n, k) {
|
||||
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)
|
||||
}
|
||||
n > 1 ? (f.inc == k) : (f == k)
|
||||
}
|
||||
|
||||
5.times { |k|
|
||||
{ |k|
|
||||
var x = 10
|
||||
say gather {
|
||||
Math.inf.times { |i|
|
||||
{ |i|
|
||||
if (is_k_almost_prime(i, k)) {
|
||||
take(i); (--x).is_zero && break;
|
||||
take(i)
|
||||
--x == 0 && break
|
||||
}
|
||||
}
|
||||
} << 1..Inf
|
||||
}
|
||||
}
|
||||
} << 1..5
|
||||
|
|
|
|||
5
Task/Almost-prime/Zkl/almost-prime.zkl
Normal file
5
Task/Almost-prime/Zkl/almost-prime.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
primes:=Utils.Generator(Import("sieve").postponed_sieve);
|
||||
(p10:=ar:=primes.walk(10)).println();
|
||||
do(4){
|
||||
(ar=([[(x,y);ar;p10;'*]] : Utils.Helpers.listUnique(_).sort()[0,10])).println();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue