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,35 @@
; the first twenty primes
(primes 20)
→ { 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 }
; a stream to generate primes from a
(define (primes-from a)
(let ((p (next-prime a)))
(stream-cons p (primes-from p))))
; primes between 100,150
(for/list ((p (primes-from 100))) #:break (> p 150) p)
→ (101 103 107 109 113 127 131 137 139 149)
; the built-in function (primes-pi )counts the number of primes < a
; count in [7700 ... 8000]
(- (primes-pi 8000) (primes-pi 7700) → 30
; nth-prime
(nth-prime 10000) → 104729
;; big ones
(lib 'bigint)
(define (p-digits n)
(printf "(next-prime %d ! ) has %d digits" n
(number-length (next-prime (factorial n )))))
(next-prime 0! ) has 1 digits
(next-prime 10! ) has 7 digits
(next-prime 100! ) has 158 digits
(next-prime 200! ) has 375 digits
(next-prime 300! ) has 615 digits
(next-prime 400! ) has 869 digits ;; 9400 msec (FireFox)
; is prime (1 + 116!) ?
(prime? (1+ (factorial 116))) → #t

View file

@ -0,0 +1,100 @@
' FB 1.05.0
Enum SieveLimitType
number
between
countBetween
End Enum
Sub printPrimes(low As Integer, high As Integer, slt As SieveLimitType)
If high < low OrElse low < 1 Then Return ' too small
If slt <> number AndAlso slt <> between AndAlso slt <> countBetween Then Return
If slt <> number AndAlso (low < 2 OrElse high < 2) Then Return
If slt <> number AndAlso high > 1000000000 Then Return ' too big
If slt = number AndAlso high > 50000000 Then Return ' too big
Dim As Integer n
If slt = number Then
n = 20 * high '' big enough to accomodate 50 million primes to which this procedure is limited
Else
n = high
End If
Dim a(2 To n) As Boolean '' only uses 1 byte per element
For i As Integer = 2 To n : a(i) = True : Next '' set all elements to True to start with
Dim As Integer p = 2, q
' mark non-prime numbers by setting the corresponding array element to False
Do
For j As Integer = p * p To n Step p
a(j) = False
Next j
' look for next True element in array after 'p'
q = 0
For j As Integer = p + 1 To Sqr(n)
If a(j) Then
q = j
Exit For
End If
Next j
If q = 0 Then Exit Do
p = q
Loop
Select Case As Const slt
Case number
Dim count As Integer = 0
For i As Integer = 2 To n
If a(i) Then
count += 1
If count >= low AndAlso count <= high Then
Print i; " ";
End If
If count = high Then Exit Select
End If
Next
Case between
For i As Integer = low To high
If a(i) Then
Print i; " ";
End if
Next
Case countBetween
Dim count As Integer = 0
For i As Integer = low To high
If a(i) Then count += 1
Next
Print count;
End Select
Print
End Sub
Print "The first 20 primes are :"
Print
printPrimes(1, 20, number)
Print
Print "The primes between 100 and 150 are :"
Print
printPrimes(100, 150, between)
Print
Print "The number of primes between 7700 and 8000 is :";
printPrimes(7700, 8000, countBetween)
Print
Print "The 10000th prime is :";
Dim t As Double = timer
printPrimes(10000, 10000, number)
Print "Computed in "; CInt((timer - t) * 1000 + 0.5); " ms"
Print
Print "The 1000000th prime is :";
t = timer
printPrimes(1000000, 1000000, number)
Print "Computed in ";CInt((timer - t) * 1000 + 0.5); " ms"
Print
Print "The 50000000th prime is :";
t = timer
printPrimes(50000000, 50000000, number)
Print "Computed in ";CInt((timer - t) * 1000 + 0.5); " ms"
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,38 @@
see "first twenty primes : "
i = 1
nr = 0
while i <= 20
nr += 1
if isPrime(nr) see " " + nr i += 1 ok
end
see "primes between 100 and 150 : "
for nr = 100 to 150
if isPrime(nr) see " " + nr ok
next
see nl
see "primes between 7,700 and 8,000 : "
i = 0
for nr = 7700 to 8000
if isPrime(nr) i += 1 ok
next
see i + nl
see "The 10,000th prime : "
i = 1
nr = 0
while i <= 10000
nr += 1
if isPrime(nr) i += 1 ok
end
see nr + nl
func isPrime n
if n <= 1 return false ok
if n <= 3 return true ok
if (n & 1) = 0 return false ok
for t = 3 to sqrt(n) step 2
if (n % t) = 0 return false ok
next
return true

View file

@ -0,0 +1,6 @@
var nt = frequire('ntheory')
say ("First 20: ", nt.primes(nt.nth_prime(20)).join(' '))
say ("Between 100 and 150: ", nt.primes(100,150).join(' '))
say (nt.prime_count(7700,8000), " primes between 7700 and 8000")
say ("10,000th prime: ", nt.nth_prime(10_000))

View file

@ -0,0 +1,9 @@
# Recent versions of jq include the following definition:
# until/2 loops until cond is satisfied,
# and emits the value satisfying the condition:
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;
def count(cond): reduce .[] as $x (0; if $x|cond then .+1 else . end);

View file

@ -0,0 +1,33 @@
# Is the input integer a prime?
# "previous" must be the array of sorted primes greater than 1 up to (.|sqrt)
def is_prime(previous):
. as $in
| (previous|length) as $plength
| [false, 0] # state: [found, ix]
| until( .[0] or .[1] >= $plength;
[ ($in % previous[.[1]]) == 0, .[1] + 1] )
| .[0] | not ;
# extend_primes expects its input to be an array consisting of
# previously found primes, in order, and extends that array:
def extend_primes:
if . == null or length == 0 then [2]
else . as $previous
| if . == [2] then [2,3]
else . + [(2 + .[length-1]) | until( is_prime($previous) ; . + 2)]
end
end;
# If . is an integer > 0 then produce an array of . primes;
# otherwise emit an unbounded stream of primes:
def primes:
. as $n
| if type == "number" and $n > 0 then
null | until( length == $n; extend_primes )
else [2] | recurse(extend_primes) | .[length - 1]
end;
# Primes up to and possibly including n:
def primes_upto(n):
until( .[length-1] > n; extend_primes )
| if .[length-1] > n then .[0:length-1] else . end;

View file

@ -0,0 +1,9 @@
"First 20 primes:", (20 | primes), "",
"Primes between 100 and 150:",
(primes_upto(150) | map(select( 100 < .))), "",
"The 10,000th prime is \( 10000 | primes | .[length - 1] )", "",
(( primes_upto(8000) | count( . > 7700) | length) as $length
| "There are \($length) primes twixt 7700 and 8000.")

View file

@ -0,0 +1,10 @@
$ jq -r -c -n -f Extensible_prime_generator.jq
First 20 primes:
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71]
Primes between 100 and 150:
[101,103,107,109,113,127,131,137,139,149]
The 10,000th prime is 104729
There are 30 primes twixt 7700 and 8000.