Code Select
> (apply (fn(x y) (+ (int x) (int y))) (find-all {[1-9]d*} "o123p0010iru5") 2)
138
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu[code]
> (apply + (map int (find-all {[1-9][0-9]*} "o123p010iru5")))
138
>
(define (parse-str str)
(apply + (map int (clean empty? (parse str {[^0-9]} 0)))))
(define (parse-str str)
(let (total 0)
(dolist (s (parse str {[^0-9]} 0))
(unless (empty? s)
(inc total (int s))))
total))
> (setq lst (sequence 0 9))
(0 1 2 3 4 5 6 7 8 9)
> (explode lst 2)
((0 1) (2 3) (4 5) (6 7) (8 9))
> (explode lst 3)
((0 1 2) (3 4 5) (6 7 8) (9))
> (setq lst (sequence 1 12))
(1 2 3 4 5 6 7 8 9 10 11 12)
> (explode (explode lst 2)2)
(((1 2) (3 4)) ((5 6) (7 8)) ((9 10) (11 12)))
;; primes.lsp - return all primes in a list, up to n
(define (primes n , p)
(dotimes (e n)
(if (= (length (factor e)) 1)
(push e p -1))) p)
> > (time primes 1000000)
937.407
> (length (primes 1000000))
78498
(define (primes-to n , (p-list '(2)))
(for (x 3 n 2)
(unless (rest (factor x))
(push x p-list -1)))
p-list
)
> (time (primes-to 1000000))
569.302
(define (sieve n)
(set 'arr (array (+ n 1)) 'lst '(2))
(for (x 3 n 2)
(when (not (arr x))
(push x lst -1)
(for (y (* x x) n (* 2 x) (> y n))
(setf (arr y) true))))
lst
)
> (time (sieve 1000000))
259.257
(define (funlist lst func rev)
(if rev
(map func (chop lst) (rest lst))
(map func (rest lst) (chop lst))))
> (funlist (sieve 1000) -)
(1 2 2 4 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 14 4 6 2 10 2 6 6 4 6
6 2 10 2 4 2 12 12 4 2 4 6 2 10 6 6 6 2 6 4 2 10 14 4 2 4 14 6 10 2 4 6 8 6 6 4
6 8 4 8 10 2 10 2 6 4 6 8 4 2 4 12 8 4 8 4 6 12 2 18 6 10 6 6 2 6 10 6 6 2 6 6 4
2 12 10 2 4 6 6 2 12 4 6 8 10 8 10 8 6 6 4 8 6 4 8 4 14 10 12 2 10 2 4 2 10 14 4
2 4 14 4 2 4 20 4 8 10 8 4 6 6 14 4 6 6 8 6)
(define (freq lst)
(let (ulist (unique (sort lst)))
(map list ulist (count ulist lst))))
> (freq (funlist (sieve 1000) -))
((1 1) (2 35) (4 40) (6 44) (8 15) (10 16) (12 7) (14 7) (18 1) (20 1))
(define (comp x y)
(>= (last x) (last y)))
(sort(freq(funlist (sieve 1000000) -)) comp)
((6 13549) (2 8169) (4 8143) (12 8005) (10 7079) (8 5569) (18 4909) (14 4233) (16
2881)
(24 2682)
(20 2401)
(22 2172)
(30 1914)
(28 1234)
(26 1175)
(36 767)
(34 557)
(32 550)
(set (sym "name") 3)
name ->3
(set (sym {"name"}) 3)
(eval (sym {"name"})) -> 3