euler

problem43

import itertools def is_ok(ns): for i, x in zip(range(1, 9), (2, 3, 5, 7, 11, 13, 17)): if not ((100*ns[i]+10*ns[i+1]+ns[i+2]) % x == 0): return False return True cnt = 0 for ns in itertools.permutations(range(10), 10): if is_ok(ns): cnt +…

problem42

アルファベットを数字に対応させた際の単語の和が三角数になるものの数を数える問題。 まだ、どのように書けば十分なのかというのが把握できていない。これはクラスにした方が良かったかもしれない。 def _str_to_val(offset): def _calc(s): n = 0 for c in…

problem41

長さが4の場合。 N = 1000a + 100b + 10c + d 3x = a + b + c + d の時 d = 3x - a - b -c 整理すると N = 999a + 99b + 9c + 3x # N is not prime後は、各数字の和を調べて、7桁が最大ということが分かる。組み合わせを作って総当たり。 いやー、combinatio…

euler40

汚いな。 L = [9*(i+1)*10**i for i in xrange(10)] def nth(n): n = n-1 def idx_and_rest(n): for i, e in enumerate(L): n -= e if n < 0: return (i, e+n) i, rest = idx_and_rest(n) for j in xrange(10**i, 10**(i+1)): size = len(str(j)) if size > …

euler39

まだ、python慣れないな。最大だから、本当はリストで持つ意味ないし、もっと効率よく求められそうだけれど。 # If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. # …

problem39

a^2+b^2+c^2にa+b+c=pを代入して式を作成。 pは必ず偶数 ということで (use srfi-42) (define (ok? p) ;; a,b,c : a < b < c (any?-ec (: b p) (: a b) (zero? (+ (* p p) (- (* 2 (+ a b) p)) (* 2 a b))))) (max-ec (: p 120 1000 2) (if (ok? p)) p) 以下…

problem 36 37 38

今回はどれも問題どおりに実装するだけ。あまり難しくない。

problem35

すごく強引な計算方法。100万以下の素数をナイーブに集めて、rotateしながらhash-tableに加える。(keyとvalueの関係は以下の通り '(1 9 7) => '(197 971 719))。後は、keyのlistの長さと束縛されているリストの要素数が同じであれば循環素数だとみなす。(11…

problem 32 33

32は方針をミスった気がする。とても遅い。

problem 1から7

problem 29,30,31

29はとても簡単な問題。 30は面倒なのでmacroを使う。6個まで調べる。 31は簡単なdp