June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,21 @@
|
|||
using Primes
|
||||
|
||||
sum = 2
|
||||
currentprime = 2
|
||||
for i in 2:100000
|
||||
currentprime = nextprime(currentprime + 1)
|
||||
sum += currentprime
|
||||
end
|
||||
println("The sum of the first 100,000 primes is $sum")
|
||||
|
||||
curprime = 1
|
||||
arr = zeros(Int, 20)
|
||||
for i in 1:20
|
||||
curprime = nextprime(curprime + 1)
|
||||
arr[i] = curprime
|
||||
end
|
||||
println("The first 20 primes are ", arr)
|
||||
|
||||
println("the primes between 100 and 150 are ", primes(100,150))
|
||||
println("The number of primes between 7,700 and 8,000 is ", length(primes(7700, 8000)))
|
||||
println("The 10,000th prime is ", prime(10000))
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
-- parent script "sieve"
|
||||
property _sieve
|
||||
|
||||
----------------------------------------
|
||||
-- @constructor
|
||||
----------------------------------------
|
||||
on new (me)
|
||||
me._sieve = []
|
||||
me._primeSieve(100) -- arbitrary initial size of sieve
|
||||
return me
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Returns sorted list of first n primes p with p >= a (default: a=1)
|
||||
----------------------------------------
|
||||
on getNPrimes (me, n, a)
|
||||
if voidP(a) then a = 1
|
||||
i = a
|
||||
res = []
|
||||
repeat while TRUE
|
||||
if i>me._sieve.count then me._primeSieve(2*i)
|
||||
if me._sieve[i] then res.add(i)
|
||||
if res.count=n then return res
|
||||
i = i +1
|
||||
end repeat
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Returns sorted list of primes p with a <= p <= b
|
||||
----------------------------------------
|
||||
on getPrimesInRange (me, a, b)
|
||||
if me._sieve.count<b then me._primeSieve(b)
|
||||
primes = []
|
||||
repeat with i = a to b
|
||||
if me._sieve[i] then primes.add(i)
|
||||
end repeat
|
||||
return primes
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Returns nth prime
|
||||
----------------------------------------
|
||||
on getNthPrime (me, n)
|
||||
if me._sieve.count<2*n then me._primeSieve(2*n)
|
||||
i = 0
|
||||
found = 0
|
||||
repeat while TRUE
|
||||
i = i +1
|
||||
if i>me._sieve.count then me._primeSieve(2*i)
|
||||
if me._sieve[i] then found=found+1
|
||||
if found=n then return i
|
||||
end repeat
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Sieve of Eratosthenes
|
||||
----------------------------------------
|
||||
on _primeSieve (me, limit)
|
||||
if me._sieve.count>=limit then
|
||||
return
|
||||
else if me._sieve.count>0 then
|
||||
return me._complementSieve(limit)
|
||||
end if
|
||||
me._sieve = [0]
|
||||
repeat with i = 2 to limit
|
||||
me._sieve[i] = 1
|
||||
end repeat
|
||||
c = sqrt(limit)
|
||||
repeat with i = 2 to c
|
||||
if (me._sieve[i]=0) then next repeat
|
||||
j = i*i
|
||||
repeat while (j<=limit)
|
||||
me._sieve[j] = 0
|
||||
j = j + i
|
||||
end repeat
|
||||
end repeat
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Expands existing sieve to new limit
|
||||
----------------------------------------
|
||||
on _complementSieve (me, n)
|
||||
n1 = me._sieve.count
|
||||
repeat with i = n1+1 to n
|
||||
me._sieve[i] = 1
|
||||
end repeat
|
||||
c1 = sqrt(n1)
|
||||
repeat with i = 2 to c1
|
||||
if (me._sieve[i]=0) then next repeat
|
||||
j = n1 - (n1 mod i)
|
||||
repeat while (j<=n)
|
||||
me._sieve[j] = 0
|
||||
j = j + i
|
||||
end repeat
|
||||
end repeat
|
||||
c = sqrt(n)
|
||||
repeat with i = c1+1 to c
|
||||
if (me._sieve[i]=0) then next repeat
|
||||
j = i*i
|
||||
repeat while (j<=n)
|
||||
me._sieve[j] = 0
|
||||
j = j + i
|
||||
end repeat
|
||||
end repeat
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
sieve = script("sieve").new()
|
||||
put "First twenty primes: " & sieve.getNPrimes(20)
|
||||
put "Primes between 100 and 150: "& sieve.getPrimesInRange(100, 150)
|
||||
put "Number of primes between 7,700 and 8,000: " & sieve.getPrimesInRange(7700, 8000).count
|
||||
put "The 10,000th prime: " & sieve.getNthPrime(10000)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
(de prime? (N Lst)
|
||||
(let S (sqrt N)
|
||||
(for D Lst
|
||||
(T (> D S) T)
|
||||
(T (=0 (% N D)) NIL) ) ) )
|
||||
(de primeseq (A B)
|
||||
(let (I 1 R)
|
||||
(nth
|
||||
(make
|
||||
(link 2)
|
||||
(while (> A (inc 'I 2))
|
||||
(and (prime? I (made)) (link I)) )
|
||||
(setq R (length (made)))
|
||||
(while (> B I)
|
||||
(and (prime? I (made)) (link I))
|
||||
(inc 'I 2) ) )
|
||||
(inc R) ) ) )
|
||||
(de take (N)
|
||||
(let I 1
|
||||
(make
|
||||
(link 2)
|
||||
(do (dec N)
|
||||
(until (prime? (inc 'I 2) (made)))
|
||||
(link I) ) ) ) )
|
||||
|
||||
(prin "First 20 primes: ")
|
||||
(println (take 20))
|
||||
(prin "Primes between 100 and 150: ")
|
||||
(println (primeseq 100 150))
|
||||
(prinl
|
||||
"Number of primes between 7700 and 8000: "
|
||||
(length (primeseq 7700 8000)) )
|
||||
(for N (10 100 1000 10000 100000 1000000)
|
||||
(prinl
|
||||
N
|
||||
"th prime: "
|
||||
(last (take N)) ) )
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
Option Explicit
|
||||
|
||||
Sub Main()
|
||||
Dim Primes() As Long, n As Long, temp$
|
||||
Dim t As Single
|
||||
t = Timer
|
||||
|
||||
n = 133218295 'limit for an Array of Longs with VBA on my computer
|
||||
Primes = ListPrimes(n)
|
||||
Debug.Print "For N = " & Format(n, "#,##0") & ", execution time : " & _
|
||||
Format(Timer - t, "0.000 s") & ", " & _
|
||||
Format(UBound(Primes) + 1, "#,##0") & " primes numbers."
|
||||
|
||||
'First twenty primes
|
||||
For n = 0 To 19
|
||||
temp = temp & ", " & Primes(n)
|
||||
Next
|
||||
Debug.Print "First twenty primes : "; Mid(temp, 3)
|
||||
'Primes between 100 and 150
|
||||
n = 0: temp = vbNullString
|
||||
Do While Primes(n) < 100
|
||||
n = n + 1
|
||||
Loop
|
||||
Do While Primes(n) < 150
|
||||
temp = temp & ", " & Primes(n)
|
||||
n = n + 1
|
||||
Loop
|
||||
Debug.Print "Primes between 100 and 150 : " & Mid(temp, 3)
|
||||
'Number of primes between 7,700 and 8,000
|
||||
Dim ccount As Long
|
||||
n = 0
|
||||
Do While Primes(n) < 7700
|
||||
n = n + 1
|
||||
Loop
|
||||
Do While Primes(n) < 8000
|
||||
ccount = ccount + 1
|
||||
n = n + 1
|
||||
Loop
|
||||
Debug.Print "Number of primes between 7,700 and 8,000 : " & ccount
|
||||
'The 10 x Xth prime
|
||||
n = 1
|
||||
Do While n <= 100000
|
||||
n = n * 10
|
||||
Debug.Print "The " & n & "th prime: "; Format(Primes(n - 1), "#,##0")
|
||||
Loop
|
||||
Debug.Print "VBA has a limit in array's dim"
|
||||
Debug.Print "With my computer, the limit for an array of Long is : 133 218 295"
|
||||
Debug.Print "The last prime I could find is the : " & _
|
||||
Format(UBound(Primes), "#,##0") & "th, Value : " & _
|
||||
Format(Primes(UBound(Primes)), "#,##0")
|
||||
End Sub
|
||||
|
||||
Function ListPrimes(MAX As Long) As Long()
|
||||
Dim t() As Boolean, L() As Long, c As Long, s As Long, i As Long, j As Long
|
||||
ReDim t(2 To MAX)
|
||||
ReDim L(MAX \ 2)
|
||||
s = Sqr(MAX)
|
||||
For i = 3 To s Step 2
|
||||
If t(i) = False Then
|
||||
For j = i * i To MAX Step i
|
||||
t(j) = True
|
||||
Next
|
||||
End If
|
||||
Next i
|
||||
L(0) = 2
|
||||
For i = 3 To MAX Step 2
|
||||
If t(i) = False Then
|
||||
c = c + 1
|
||||
L(c) = i
|
||||
End If
|
||||
Next i
|
||||
ReDim Preserve L(c)
|
||||
ListPrimes = L
|
||||
End Function
|
||||
Loading…
Add table
Add a link
Reference in a new issue