Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -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)) ) )

View file

@ -0,0 +1,67 @@
(load "plcommon/pairing-heap.l") # Pairing heap from RC task "Priority Queue"
(setq *WHEEL-2357*
(10 2 4 2 4 6 2 6
4 2 4 6 6 2 6 4
2 6 4 6 8 4 2 4
2 4 8 6 4 6 2 4
6 2 6 6 4 2 4 6
2 6 4 2 4 2 10 2 .))
(de "prime?" (N Primes)
(let S (sqrt N)
(for P Primes
(T (> P S) T)
(T (=0 (% N P)) NIL))))
(de "adjust-heap" (N H)
(while (= (caar H) N)
(let (
Wheel (cadar H)
P (cddar H)
)
(setq H
(heap-insert
(cons
(+ N (* P (car Wheel)))
(cdr Wheel)
P)
(heap-rest H)))))
H)
(de primes (Run?)
(if (not Run?)
(co 'primegen) # stop
(co 'primegen
(yield 2)
(yield 3)
(yield 5)
(yield 7)
(let (
P 11
Q 121
Wp (cdr *WHEEL-2357*)
N P
Wn (cdr *WHEEL-2357*)
H (heap-insert (cons Q Wp P) NIL)
)
(make
(link P)
(loop
(cond
((= N (caar H))
(setq H ("adjust-heap" N H))
(inc 'N (pop 'Wn)))
((< N Q)
(yield N)
(inc 'N (pop 'Wn)))
(T
(loop
(inc 'P (pop 'Wp))
(T ("prime?" P (made))))
(link P)
(setq
Q (* P P)
H (heap-insert (cons Q Wp P) H))))))))))

View file

@ -0,0 +1,28 @@
(prin "The first 20 primes: ")
(do 20 (printsp (primes T)))
(prinl)
(prin "between 100 and 150: ")
(while (< (setq P (primes T)) 150)
(when (> P 100)
(printsp P)))
(prinl)
(setq Count 0)
(while (< (setq P (primes T)) 8000)
(when (> P 7700)
(inc 'Count)))
(prinl "There are " Count " primes between 7700 and 8000.")
(de nthprime (N)
(primes NIL)
(do (dec N)
(primes T))
(primes T))
(de comma_fmt (N) (format N 0 "." ","))
(prinl "nth prime:")
(for N (10 100 1000 10000 100000 1000000)
(prinl (align 9 (comma_fmt N)) " " (align 12 (comma_fmt (nthprime N)))))
(bye)