fork download
  1. ; prime -- a modest library
  2.  
  3. ; primes n -- list of primes not greater than n in ascending order
  4. (define (primes n) ; assumes n is an integer greater than one
  5. (let* ((len (quotient (- n 1) 2)) (bits (make-vector len #t)))
  6. (let loop ((i 0) (p 3) (ps (list 2))) ; sieve of eratosthenes
  7. (cond ((< n (* p p))
  8. (do ((i i (+ i 1)) (p p (+ p 2))
  9. (ps ps (if (vector-ref bits i) (cons p ps) ps)))
  10. ((= i len) (reverse ps))))
  11. ((vector-ref bits i)
  12. (do ((j (+ (* 2 i i) (* 6 i) 3) (+ j p)))
  13. ((<= len j) (loop (+ i 1) (+ p 2) (cons p ps)))
  14. (vector-set! bits j #f)))
  15. (else (loop (+ i 1) (+ p 2) ps))))))
  16.  
  17. ; prime? n -- #f if provably composite, else #t if probably prime
  18. (define prime? ; strong pseudoprime to prime bases less than 100
  19. (let ((ps (primes 100))) ; assumes n an integer greater than one
  20. (lambda (n)
  21. (define (expm b e m)
  22. (define (times p q) (modulo (* p q) m))
  23. (let loop ((b b) (e e) (x 1))
  24. (if (zero? e) x
  25. (loop (times b b) (quotient e 2)
  26. (if (odd? e) (times b x) x)))))
  27. (define (spsp? n a) ; #t if n is a strong pseudoprime base a
  28. (do ((d (- n 1) (/ d 2)) (s 0 (+ s 1)))
  29. ((odd? d)
  30. (if (= (expm a d n) 1) #t
  31. (do ((r 0 (+ r 1)))
  32. ((or (= (expm a (* d (expt 2 r)) n) (- n 1))
  33. (= r s))
  34. (< r s)))))))
  35. (if (member n ps) #t
  36. (do ((ps ps (cdr ps)))
  37. ((or (null? ps) (not (spsp? n (car ps))))
  38. (null? ps)))))))
  39.  
  40. ; factors n -- list of prime factors of n in ascending order
  41. (define (factors n) ; assumes n is an integer, may be negative
  42. (if (<= -1 n 1) (list n) ; pollard's rho algorithm
  43. (if (< n 0) (cons -1 (factors (- n)))
  44. (let fact ((n n) (c 1) (fs (list)))
  45. (define (f x) (modulo (+ (* x x) c) n))
  46. (if (even? n) (fact (/ n 2) c (cons 2 fs))
  47. (if (= n 1) fs
  48. (if (prime? n) (sort (cons n fs) <)
  49. (let loop ((t 2) (h 2) (d 1))
  50. (cond ((= d 1) (let ((t (f t)) (h (f (f h))))
  51. (loop t h (gcd (- t h) n))))
  52. ((= d n) (fact n (+ c 1) fs)) ; cyclic
  53. ((prime? d)
  54. (fact (/ n d) (+ c 1) (cons d fs)))
  55. (else (fact n (+ c 1) fs)))))))))))
  56.  
  57. (display (primes 50)) (newline)
  58.  
  59. (do ((n 1 (+ n 1)))
  60. ((< 70 n))
  61. (display n)
  62. (display ": ")
  63. (for-each
  64. (lambda (x)
  65. (display " ")
  66. (display x))
  67. (factors (- (expt 2 n) 1)))
  68. (newline))
Success #stdin #stdout 0.62s 5336KB
stdin
Standard input is empty
stdout
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47)
1:  1
2:  3
3:  7
4:  3 5
5:  31
6:  3 3 7
7:  127
8:  3 5 17
9:  7 73
10:  3 11 31
11:  23 89
12:  3 3 5 7 13
13:  8191
14:  3 43 127
15:  7 31 151
16:  3 5 17 257
17:  131071
18:  3 3 3 7 19 73
19:  524287
20:  3 5 5 11 31 41
21:  7 7 127 337
22:  3 23 89 683
23:  47 178481
24:  3 3 5 7 13 17 241
25:  31 601 1801
26:  3 2731 8191
27:  7 73 262657
28:  3 5 29 43 113 127
29:  233 1103 2089
30:  3 3 7 11 31 151 331
31:  2147483647
32:  3 5 17 257 65537
33:  7 23 89 599479
34:  3 43691 131071
35:  31 71 127 122921
36:  3 3 3 5 7 13 19 37 73 109
37:  223 616318177
38:  3 174763 524287
39:  7 79 8191 121369
40:  3 5 5 11 17 31 41 61681
41:  13367 164511353
42:  3 3 7 7 43 127 337 5419
43:  431 9719 2099863
44:  3 5 23 89 397 683 2113
45:  7 31 73 151 631 23311
46:  3 47 178481 2796203
47:  2351 4513 13264529
48:  3 3 5 7 13 17 97 241 257 673
49:  127 4432676798593
50:  3 11 31 251 601 1801 4051
51:  7 103 2143 11119 131071
52:  3 5 53 157 1613 2731 8191
53:  6361 69431 20394401
54:  3 3 3 3 7 19 73 87211 262657
55:  23 31 89 881 3191 201961
56:  3 5 17 29 43 113 127 15790321
57:  7 32377 524287 1212847
58:  3 59 233 1103 2089 3033169
59:  179951 3203431780337
60:  3 3 5 5 7 11 13 31 41 61 151 331 1321
61:  2305843009213693951
62:  3 715827883 2147483647
63:  7 7 73 127 337 92737 649657
64:  3 5 17 257 641 65537 6700417
65:  31 8191 145295143558111
66:  3 3 7 23 67 89 683 20857 599479
67:  193707721 761838257287
68:  3 5 137 953 26317 43691 131071
69:  7 47 178481 10052678938039
70:  3 11 31 43 71 127 281 86171 122921