[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/6ZT1RVxa    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Sep 12:
; tetrahedral numbers

(define (tri n)
  (let loop ((n n) (s 0))
    (if (zero? n) s
      (loop (- n 1) (+ s n)))))

(define (tri n) (* n (+ n 1) 1/2))

(define (tet n)
  (let loop ((n n) (s 0))
    (if (zero? n) s
      (loop (- n 1) (+ s (tri n))))))

(define (tet n) (* n (+ n 1) (+ n 2) 1/6))

(define (prob-18 n)
  (let loop ((lo 1) (hi 2))
    (if (< (tet hi) n)
        (loop hi (* hi 2))
        (let loop ((lo lo) (hi hi))
          (let* ((mid (quotient (+ lo hi) 2))
                 (tet-mid (tet mid)))
            (cond ((< tet-mid n) (loop mid hi))
                  ((< n tet-mid) (loop lo mid))
                  (else mid)))))))

(display (prob-18 169179692512835000)) (newline)

(display (floor (expt (* 6 169179692512835000) 1/3))) (newline)


Output:
1
2
1005000
1005000.0


Create a new paste based on this one


Comments: