Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,36 @@
PROGRAM ALMOST_PRIME
!
! for rosettacode.org
!
!$INTEGER
PROCEDURE KPRIME(N,K->KP)
LOCAL P,F
FOR P=2 TO 999 DO
EXIT IF NOT((F<K) AND (P*P<=N))
WHILE (N MOD P)=0 DO
N/=P
F+=1
END WHILE
END FOR
KP=(F-(N>1)=K)
END PROCEDURE
BEGIN
PRINT(CHR$(12);) !CLS
FOR K=1 TO 5 DO
PRINT("k =";K;":";)
C=0
FOR I=2 TO 999 DO
EXIT IF NOT(C<10)
KPRIME(I,K->KP)
IF KP THEN
PRINT(I;)
C+=1
END IF
END FOR
PRINT
END FOR
END PROGRAM

View file

@ -0,0 +1,11 @@
(define (almost-prime? p k)
(= k (length (prime-factors p))))
(define (almost-primes k nmax)
(take (filter (rcurry almost-prime? k) [2 ..]) nmax))
(define (task (kmax 6) (nmax 10))
(for ((k [1 .. kmax]))
(write 'k= k '|)
(for-each write (almost-primes k nmax))
(writeln)))

View file

@ -0,0 +1,7 @@
(task)
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

View file

@ -0,0 +1,50 @@
(lib 'match)
(define-syntax-rule (: v i) (vector-ref v i))
(reader-infix ':) ;; abbrev (vector-ref v i) === [v : i]
(lib 'bigint)
(define cprimes (list->vector (primes 10000)))
;; generates next k-almost-prime < pmax
;; c = vector of k primes indices c[i] <= c[j]
;; p = vector of intermediate products prime[c[0]]*prime[c[1]]*..
;; p[k-1] is the generated k-almost-prime
;; increment one c[i] at each step
(define (almost-next pmax k c p)
(define almost-prime #f)
(define cp 0)
(for ((i (in-range (1- k) -1 -1))) ;; look backwards for c[i] to increment
(vector-set! c i (1+ [c : i])) ;; increment c[i]
(set! cp [cprimes : [c : i]])
(vector-set! p i (if (> i 0) (* [ p : (1- i)] cp) cp)) ;; update partial product
(when (< [p : i) pmax)
(set! almost-prime
(and ;; set followers to c[i] value
(for ((j (in-range (1+ i) k)))
(vector-set! c j [c : i])
(vector-set! p j (* [ p : (1- j)] cp))
#:break (>= [p : j] pmax) => #f )
[p : (1- k)]
) ;; // and
) ;; set!
) ;; when
#:break almost-prime
) ;; // for i
almost-prime )
;; not sorted list of k-almost-primes < pmax
(define (almost-primes k nmax)
(define base (expt 2 k)) ;; first one is 2^k
(define pmax (* base nmax))
(define c (make-vector k #0))
(define p (build-vector k (lambda(i) (expt #2 (1+ i)))))
(cons base
(for/list
((almost-prime (in-producer almost-next pmax k c p )))
almost-prime)))

View file

@ -0,0 +1,8 @@
;; we want 500-almost-primes from the 10000-th.
(take (drop (list-sort < (almost-primes 500 10000)) 10000 ) 10)
(7241149198492252834202927258094752774597239286103014697435725917649659974371690699721153852986
440733637405206125678822081264723636566725108094369093648384
etc ...
;; The first one is 2^497 * 3 * 17 * 347 , same result as Haskell.

View file

@ -0,0 +1,32 @@
' 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

View file

@ -0,0 +1,18 @@
fun kprime(n: int, k: int): bool =
let (p,f) = (2, 0)
loop ((n, p, f)) = while f < k && p*p <= n do
loop ((n,f)) = while 0 == n % p do
(n/p, f+1)
in (n, p+1, f)
in f + (if n > 1 then 1 else 0) == k
fun main(m: int): [][]int =
map (fn k: [10]int =>
let ps = replicate 10 0
loop ((i,c,ps) = (2,0,ps)) = while c < 10 do
if kprime(i,k) then
unsafe let ps[c] = i
in (i+1, c+1, ps)
else (i+1, c, ps)
in ps)
(map (1+) (iota m))

View file

@ -0,0 +1,9 @@
: kprime(n, k)
| i |
0 2 n for: i [ while(n i /mod swap 0 &= ) [ ->n 1+ ] drop ] k == ;
: table(k)
| l |
ListBuffer new ->l
2 while (l size 10 <>) [ dup k kprime ifTrue: [ dup l add ] 1+ ]
drop l ;

View file

@ -0,0 +1,117 @@
-- Naieve stuff, mostly, but coded with enthuiasm!
-- Following the idea behind (but not the code from!) the J submission:
-- Generate 10 primes (kept in p10) -- (print K=1)
-- Multiply each of them by the first ten primes
-- Sort and find unique values, take the first ten of those -- (print K=2)
-- Multiply each of them by the first ten primes
-- Sort and find unique values, take the first ten of those -- (print K=3)
-- ...
-- However I just keep a "top 10", using a bubble insertion, and stop
-- multiplying as soon as everything else for p10[i] will be too big.
-- (as calculated earlier from this routine,
-- or that "return 1" in pi() works just fine.)
--constant f17={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59}
constant f17={2,3,5,7,11,13,17}
function pi(integer n)
-- approximates the number of primes less than or equal to n
-- if n<=10 then return 4 end if
-- -- best estimate
-- return floor(n/(log(n)-1))
-- if n<=20 then return 1 end if -- (or use a table:)
if n<17 then
for i=1 to length(f17) do
if n<=f17[i] then return i end if
end for
end if
-- -- upper bound for n>=17 (Rosser and Schoenfeld 1962):
-- return floor(1.25506*n/log(n))
-- lower bound for n>=17 (Rosser and Schoenfeld 1962):
return floor(n/log(n))
end function
function primes(integer n)
-- return the first n prime numbers (tested 0 to 20,000, which took ~86s)
sequence prime
integer count = 0
integer lowN, highN, midN
-- First, iteratively estimate the sieve size required
lowN = 2*n
highN = n*n+1
while lowN<highN do
midN = floor((lowN+highN)/2)
if pi(midN)>n then
highN = midN
else
lowN = midN+1
end if
end while
-- Then apply standard sieve and store primes as we find
-- them towards the (no longer used) start of the sieve.
prime = repeat(1,highN)
for i=2 to highN do
if prime[i] then
count += 1
prime[count] = i
if count>=n then exit end if
for k=i+i to highN by i do
prime[k] = 0
end for
end if
end for
return prime[1..n]
end function
procedure display(integer k, sequence kprimes)
printf(1,"%d: ",k)
for i=1 to length(kprimes) do
printf(1,"%5d",kprimes[i])
end for
puts(1,"\n")
end procedure
function bubble(sequence next, integer v)
-- insert v into next (discarding next[$]), keeping next in ascending order
-- (relies on next[1] /always/ being smaller that anything that we insert.)
for i=length(next)-1 to 1 by -1 do
if v>next[i] then
next[i+1] = v
exit
end if
next[i+1] = next[i]
end for
return next
end function
procedure almost_prime()
sequence p10 = primes(10)
sequence apk = p10 -- (almostprime[k])
sequence next = repeat(0,length(p10))
integer high, test
for k=1 to 5 do
display(k,apk)
if k=5 then exit end if
next = apk
for i=1 to length(p10) do
-- next[i] = apk[i]*p10[1]
next[i] = apk[i]*2
end for
high = next[$]
for i=2 to length(p10) do
for j=1 to length(next) do
test = apk[j]*p10[i]
if not find(test,next) then
if test>high then exit end if
next = bubble(next,test)
high = next[$]
end if
end for
end for
apk = next
end for
if getc(0) then end if
end procedure
almost_prime()

View file

@ -0,0 +1,44 @@
function kprime(integer n, integer k)
--
-- returns true if n has exactly k factors
--
-- p is a "pseudo prime" in that 2,3,4,5,6,7,8,9,10,11 will behave
-- exactly like 2,3,5,7,11, ie the remainder(n,4)=0 (etc) will never
-- succeed because remainder(n,2) would have succeeded twice first.
-- Hence for larger n consider replacing p+=1 with p=next_prime(),
-- then again, on "" this performs an obscene number of divisions..
--
integer p = 2,
factors = 0
while factors<k and p*p<=n do
while remainder(n,p)=0 do
n = n/p
factors += 1
end while
p += 1
end while
factors += (n>1)
return factors==k
end function
procedure almost_primeC()
integer nextkprime, count
for k=1 to 5 do
printf(1,"k = %d: ", k);
nextkprime = 2
count = 0
while count<10 do
if kprime(nextkprime, k) then
printf(1," %4d", nextkprime)
count += 1
end if
nextkprime += 1
end while
puts(1,"\n")
end for
if getc(0) then end if
end procedure
almost_primeC()

View file

@ -0,0 +1,20 @@
# Converted from C
kprime = (n, k):
p = 2, f = 0
while (f < k && p*p <= n):
while (0 == n % p):
n /= p
f++.
p++.
n = if (n > 1): 1.
else: 0.
f + n == k.
1 to 5 (k):
"k = " print, k print, ":" print
i = 2, c = 0
while (c < 10):
if (kprime(i, k)): " " print, i print, c++.
i++
.
"" say.

View file

@ -0,0 +1,33 @@
for ap = 1 to 5
see "k = " + ap + ":"
aList = []
for n = 1 to 200
num = 0
for nr = 1 to n
if n%nr=0 and isPrime(nr)=1
num = num + 1
pr = nr
while true
pr = pr * nr
if n%pr = 0
num = num + 1
else exit ok
end ok
next
if (ap = 1 and isPrime(n) = 1) or (ap > 1 and num = ap)
add(aList, n)
if len(aList)=10 exit ok ok
next
for m = 1 to len(aList)
see " " + aList[m]
next
see nl
next
func isPrime num
if (num <= 1) return 0 ok
if (num % 2 = 0 and num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1

View file

@ -0,0 +1,22 @@
import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
main(args(2)) :=
let
result := firstNKPrimes(1 ... 5, 10);
output[i] := "k = " ++ intToString(i) ++ ": " ++ delimit(intToString(result[i]), ' ');
in
delimit(output, '\n');
firstNKPrimes(k, N) := firstNKPrimesHelper(k, N, 2, []);
firstNKPrimesHelper(k, N, current, result(1)) :=
let
newResult := result when not isKPrime(k, current) else result ++ [current];
in
result when size(result) = N
else
firstNKPrimesHelper(k, N, current + 1, newResult);
isKPrime(k, n) := size(primeFactorization(n)) = k;

View file

@ -0,0 +1,17 @@
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 > 1 ? (f.inc == k) : (f == k)
}
5.times { |k|
var x = 10
say gather {
Math.inf.times { |i|
if (is_k_almost_prime(i, k)) {
take(i); (--x).is_zero && break;
}
}
}
}

View file

@ -0,0 +1,84 @@
# Recent versions of jq (version > 1.4) have the following definition of "until":
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;
# relatively_prime(previous) tests whether the input integer is prime
# relative to the primes in the array "previous":
def relatively_prime(previous):
. as $in
| (previous|length) as $plen
# state: [found, ix]
| [false, 0]
| until( .[0] or .[1] >= $plen;
[ ($in % previous[.[1]]) == 0, .[1] + 1] )
| .[0] | not ;
# Emit a stream in increasing order of all primes (from 2 onwards)
# that are less than or equal to mx:
def primes(mx):
# The helper function, next, has arity 0 for tail recursion optimization;
# it expects its input to be the array of previously found primes:
def next:
. as $previous
| ($previous | .[length-1]) as $last
| if ($last >= mx) then empty
else ((2 + $last)
| until( relatively_prime($previous) ; . + 2)) as $nextp
| if $nextp <= mx
then $nextp, (( $previous + [$nextp] ) | next)
else empty
end
end;
if mx <= 1 then empty
elif mx == 2 then 2
else (2, 3, ( [2,3] | next))
end
;
# Return an array of the distinct prime factors of . in increasing order
def prime_factors:
# Return an array of prime factors of . given that "primes"
# is an array of relevant primes:
def pf(primes):
if . <= 1 then []
else . as $in
| if ($in | relatively_prime(primes)) then [$in]
else reduce primes[] as $p
([];
if ($in % $p) != 0 then .
else . + [$p] + (($in / $p) | pf(primes))
end)
end
| unique
end;
if . <= 1 then []
else . as $in
| pf( [ primes( (1+$in) | sqrt | floor) ] )
end;
# Return an array of prime factors of . repeated according to their multiplicities:
def prime_factors_with_multiplicities:
# Emit p according to the multiplicity of p
# in the input integer assuming p > 1
def multiplicity(p):
if . < p then empty
elif . == p then p
elif (. % p) == 0 then
((./p) | recurse( if (. % p) == 0 then (. / p) else empty end) | p)
else empty
end;
if . <= 1 then []
else . as $in
| prime_factors as $primes
| if ($in|relatively_prime($primes)) then [$in]
else reduce $primes[] as $p
([];
if ($in % $p) == 0 then . + [$in|multiplicity($p)] else . end )
end
end;

View file

@ -0,0 +1,14 @@
def isalmostprime(k): (prime_factors_with_multiplicities | length) == k;
# Emit a stream of the first N almost-k primes
def almostprimes(N; k):
if N <= 0 then empty
else
# state [remaining, candidate, answer]
[N, 1, null]
| recurse( if .[0] <= 0 then empty
elif (.[1] | isalmostprime(k)) then [.[0]-1, .[1]+1, .[1]]
else [.[0], .[1]+1, null]
end)
| .[2] | select(. != null)
end;

View file

@ -0,0 +1 @@
range(1;6) as $k | "k=\($k): \([almostprimes(10;$k)])"

View file

@ -0,0 +1,6 @@
$ jq -c -r -n -f Almost_prime.jq
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]