fork(1) download
  1. ; prime power triples
  2.  
  3. (define-syntax fold-of
  4. (syntax-rules (range in is)
  5. ((_ "z" f b e) (set! b (f b e)))
  6. ((_ "z" f b e (v range fst pst stp) c ...)
  7. (let* ((x fst) (p pst) (s stp)
  8. (le? (if (positive? s) <= >=)))
  9. (do ((v x (+ v s))) ((le? p v) b)
  10. (fold-of "z" f b e c ...))))
  11. ((_ "z" f b e (v range fst pst) c ...)
  12. (let* ((x fst) (p pst) (s (if (< x p) 1 -1)))
  13. (fold-of "z" f b e (v range x p s) c ...)))
  14. ((_ "z" f b e (v range pst) c ...)
  15. (fold-of "z" f b e (v range 0 pst) c ...))
  16. ((_ "z" f b e (x in xs) c ...)
  17. (do ((t xs (cdr t))) ((null? t) b)
  18. (let ((x (car t)))
  19. (fold-of "z" f b e c ...))))
  20. ((_ "z" f b e (x is y) c ...)
  21. (let ((x y)) (fold-of "z" f b e c ...)))
  22. ((_ "z" f b e p? c ...)
  23. (if p? (fold-of "z" f b e c ...)))
  24. ((_ f i e c ...)
  25. (let ((b i)) (fold-of "z" f b e c ...)))))
  26.  
  27. (define-syntax list-of (syntax-rules ()
  28. ((_ arg ...) (reverse (fold-of
  29. (lambda (d a) (cons a d)) '() arg ...)))))
  30.  
  31. (define sort #f)
  32. (define merge #f)
  33. (let ()
  34. (define dosort
  35. (lambda (pred? ls n)
  36. (if (= n 1)
  37. (list (car ls))
  38. (let ((i (quotient n 2)))
  39. (domerge pred?
  40. (dosort pred? ls i)
  41. (dosort pred? (list-tail ls i) (- n i)))))))
  42. (define domerge
  43. (lambda (pred? l1 l2)
  44. (cond
  45. ((null? l1) l2)
  46. ((null? l2) l1)
  47. ((pred? (car l2) (car l1))
  48. (cons (car l2) (domerge pred? l1 (cdr l2))))
  49. (else (cons (car l1) (domerge pred? (cdr l1) l2))))))
  50. (set! sort
  51. (lambda (pred? l)
  52. (if (null? l) l (dosort pred? l (length l)))))
  53. (set! merge
  54. (lambda (pred? l1 l2)
  55. (domerge pred? l1 l2))))
  56.  
  57. (define (unique eql? xs)
  58. (cond ((null? xs) '())
  59. ((null? (cdr xs)) xs)
  60. ((eql? (car xs) (cadr xs))
  61. (unique eql? (cdr xs)))
  62. (else (cons (car xs) (unique eql? (cdr xs))))))
  63.  
  64. (define (iroot k n)
  65. (let ((k-1 (- k 1)))
  66. (let loop ((u n) (s (+ n 1)))
  67. (if (<= s u) s
  68. (let ((t (+ (* k-1 u) (quotient n (expt u k-1)))))
  69. (loop (quotient t k) u))))))
  70.  
  71. (define (primes n) ; list of primes not exceeding n
  72. (let* ((len (quotient (- n 1) 2)) (bits (make-vector len #t)))
  73. (let loop ((i 0) (p 3) (ps (list 2)))
  74. (cond ((< n (* p p))
  75. (do ((i i (+ i 1)) (p p (+ p 2))
  76. (ps ps (if (vector-ref bits i) (cons p ps) ps)))
  77. ((= i len) (reverse ps))))
  78. ((vector-ref bits i)
  79. (do ((j (+ (* 2 i i) (* 6 i) 3) (+ j p)))
  80. ((<= len j) (loop (+ i 1) (+ p 2) (cons p ps)))
  81. (vector-set! bits j #f)))
  82. (else (loop (+ i 1) (+ p 2) ps))))))
  83.  
  84. (define (euler87 n)
  85. (length (unique = (sort < (list-of s
  86. (a in (primes (iroot 2 n)))
  87. (b in (primes (iroot 3 n)))
  88. (c in (primes (iroot 4 n)))
  89. (s is (+ (* a a) (* b b b) (* c c c c)))
  90. (< s n))))))
  91.  
  92. (display (euler87 50)) (newline)
Success #stdin #stdout 0.02s 8496KB
stdin
Standard input is empty
stdout
4