The Way 2 Inner Peace.

欧拉项目第28题.

https://projecteuler.net/problem=28

#lang racket

;give a n 
;output the outermost diagonal numbers of the n x n spiral form
(define (outermost n)
  (if (= n 1)
      (list 1)
      (build-list 4 (λ(x) 
                        (+ (* (sub1 n)(add1 x))
                           (sqr(- n 2)))))))

(define (build-diagonal n)
  (define (iter i r)
    (if (> i n)
        r
        (iter (+ 2 i)
              (append r (outermost i)))))
  (iter 1 '()))

(display (apply + (build-diagonal 1001)))

深度优先搜索和广度优先搜索(非递归).

来自udacity cs215 unit3-12

深度优先:

  1. 将openlist中的最后一项取出.
  2. 将其未标记的邻项标记为已访问.并放入openlist.

  3. 重复此步骤1.2.直到openlist为空.

例:

 

上图中图从P开始,openlist:

[p]

[r,s,q]

[r,s,t]

[r,s,u]

[r,s]

[r]

[]

广度优先则是将openlist中的第一项取出.有:

[p]

[r,s,q]

[s,q,u]

[q,u]

[u,t]

[t]

[]

Grabbing the last element means you are using a "stack". Grabbing the first means you are using a "queue". -MLL

深度优先使用的是栈.广度优先使用的是队列.

参考代码

欧拉项目第12题.

https://projecteuler.net/problem=12

计算一个自然数的因子个数,用到了这里

racket...

#lang racket

(define (triangle x)
  (quotient (* x (add1 x)) 2))

(define (prime-factors n)
  (let loop ([n n] [m 2] [factors (list)])
    (cond [(= n 1) factors]
          [(= 0 (modulo n m)) (loop (/ n m) 2 (cons m factors))]
          [else (loop n (add1 m) factors)]))) 

;;cont n in l
(define (count-n l n)
  (cons n (count (lambda (x) (= n x)) l)))

;;count all n in l
(define (count-all l)
  (let loop ([l l] [visited '()] [r '()])
    (cond
      [(null? l) r]
      [(member (car l) visited) (loop (cdr l) visited r)]
      [else (loop (cdr l) 
                  (cons (car l) visited)
                  (cons (count-n l (car l)) r))])))

;;count all divisors of a number
;more in http://bit.ly/WLfeZN
(define (count-factor n)
  (let loop ([l (count-all(prime-factors n))] [r 1])
    (cond
      [(null? l) (cons r n)]
      [else (loop (cdr l) (* r (add1(cdar l))))])))

(define (tri-count-factor n)
  (count-factor (triangle n)))

(time(car(filter (lambda (x) (< 500 (car x))) 
                 (map tri-count-factor (range 10000 20000)))))

 

欧拉项目第14题.

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))

关于使用google搜索的一点小问题

一切感谢我们伟大的墙!

一.无法打开google搜索页:

事实证明.google搜索是最好用的,完胜baidu,duckduckgo,bing.特别是找英文资料的时候.(其实好多时候搜索的第一个结果都是指向StackOverflow).但是常常会有google搜索页无法载入的情况.这个问题很好解决.那就是用https加密.即使用https://google.com.hk (或者https://encrypted.google.com/).就我个人而言.我使用一款超好用的火狐扩展InstantFox,它可以自定义的快捷搜索.比如在地址栏输入g 搜索内容 就可以打开google的搜索页了,就此.只需将InstantFox中google的搜索引擎网址的http改为https就ok了.问题解决

二.无法打开google搜索跳转页:

蛋疼的事情又来了.搜索页是打开了.但是每个搜索页的条目都是有google跳转页面的.遇上GFW抽风.跳转页就无法打开了.一直显示正在载入可是却从来没载入成功过.跳转页url如下:

http://www.google.com.hk/url?...url=http%3A%2F%2F....

解决办法有三个:

  1. 禁止google跳转链接.一个好用的油猴脚本: https://userscripts.org/scripts/show/82547
  2. 把给google的跳转链接从http改为https
  3. 把http://www.google.com.hk/url加入proxy生效列表.