The Way 2 Inner Peace.

又十诫
欧拉项目第12题.

欧拉项目第14题.

Qians posted @ 2012年10月24日 15:15 in scheme with tags projecteuler racket , 3302 阅读

https://projecteuler.net/problem=14

racekt.. 大概9秒.

#lang racket
(require racket/trace)
(define (col x)
  (cond
    [(> 0 x)x]
    [(even? x) (/ x 2)]
    [else (add1(* 3 x))]))
(define (gene-col x ht)
  (cond
    [(number? (hash-ref ht x null)) (hash-ref ht x)]
    [(= 1 x) (begin(hash-set! ht x 1)(hash-ref ht x))]
    [else (begin(hash-set! ht x (add1 (gene-col (col x) ht)))
                (hash-ref ht x))]))
;;alop is a list of pairs
(define (find-cdr alop h)
  (if(= h (cdr(car alop)))
     (car alop)
     (find-cdr (cdr alop) h)))
(define ht (make-hash))
(define answers(map (lambda (x) (gene-col x ht))(build-list 1000000 (lambda (x) (add1 x)))))
(find-cdr (hash->list ht) (apply max answers))

Avatar_small
依云 说:
2012年10月24日 18:30

排版有误,> 后多了个分号。

Avatar_small
Qians 说:
2012年10月24日 21:14

囧...不知道怎么处理.输入的的确只有>,编辑哪里看源码也确实被转义成>了.但是就是有个分号.索性不用高亮了.

Avatar_small
依云 说:
2012年10月24日 21:25

去 http://bugs.i11r.com/ 报 bug 吧。以前 Haskell 的高亮更糟糕,后来修好了。

Fermat618 说:
2012年11月10日 20:44

我写的不加缓存的版本10s跑完。你这个在我这里需要4s。
#!/usr/bin/env racket
#lang racket

(define (total_steps n)
(define (total_steps_sub n acc)
(if (= 1 n)
(+ 1 acc)
(total_steps_sub (next_a n) (+ 1 acc))))
(define (next_a a)
(if (even? a)
(/ a 2)
(+ (* a 3) 1)))

(total_steps_sub n 0))

(define (findmax i max_index max_steps)
(if (= 1000000 i)
max_index
(let ((steps (total_steps i)))
(if (> steps max_steps)
(findmax (+ i 1) i steps)
(findmax (+ i 1) max_index max_steps)))))
(display (findmax 1 1 1))(newline)

Avatar_small
Qians 说:
2012年11月13日 08:59

....看完你的这个代码.我又想去看sicp了.......


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter