#clojure 入門者向け勉強会 #mitori_clj 第二週担当分
Project Euler Problem 10
2,000,000 未満の素数の総和を求めよ. とのことです.
結果を出すために手に入るリソースを有効活用するという考えのもとでは --この問題自体の解答を述べている
#clojure 入門者向け勉強会 #mitori_clj 第二週担当分
Project Euler Problem 10
2,000,000 未満の素数の総和を求めよ. とのことです.
結果を出すために手に入るリソースを有効活用するという考えのもとでは --この問題自体の解答を述べている
;; 去年最初に解いたとき答え | |
;; | |
;; "Elapsed time: 106406.52058 msecs" | |
;; 今やっても時間が1分40秒もかかってしまうので、だめー。 | |
;; 考えかた | |
;; ある数Nが素数であるかどうかは、√N以下の素数に割りきれるものがあるかどうかで判定します。 | |
;; そのためには、そこまでに発見した素数を保持する必要があります。 | |
;; あとは、2以上の数について、素数であるかどうかでフィルターします。 |
;;;A palindromic number reads the same both ways. | |
;;;The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. | |
;;;Find the largest palindrome made from the product of two 3-digit numbers. | |
;;;左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、 | |
;;;最大のものは 9009 = 91 × 99 である。 | |
;;;では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。 | |
(ns projecteuler.problem-4 | |
(:require [clojure.string :as str]) |
(use 'clojure.test) | |
;; ピタゴラス数チェック | |
(defn pythagorean? | |
[a b c] | |
(= (* c c) (+ (* a a) (* b b)))) | |
(is (pythagorean? 3 4 5)) | |
;; 合計が a+b+c かつ 0 < a < b < c になるような組の列挙 |
(defn fib [n] | |
(let [a-fib (atom false)] | |
(reset! a-fib (memoize (fn [n] (if (< 1 n) (+ (@a-fib (- n 1)) (@a-fib (- n 2))) 1)))) | |
(@a-fib n) | |
)) | |
;;; inspired by @tnoda | |
(def ^:dynamic *m-fib*) | |
(defn fibv [n] | |
(binding [*m-fib* (memoize (fn [x] (if (< 1 x) (+ (*m-fib* (- x 1)) (*m-fib* (- x 2))) 1)))] |
(use 'clojure.test) | |
(with-test | |
(defn f | |
[& {:as options}] | |
options) | |
(is (nil? (f))) | |
(is (thrown? IllegalArgumentException (f nil))) | |
(is (thrown? IllegalArgumentException (f :a))) | |
(is (= (f nil nil) {nil nil})) |
(require '[clojure.test :refer (with-test are run-tests)]) | |
(with-test | |
(defn str-drop-while-same [& args] | |
(if (some nil? args) args | |
(->> (map #(concat % (repeat nil)) args) | |
(apply map vector) | |
(drop-while #(apply = %)) | |
(take-while #(some identity %)) | |
(apply map str)))) |
;;; Project Euler #8 | |
;;; http://projecteuler.net/problem=8 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%208 | |
(use 'clojure.test) | |
(def input | |
(str | |
"73167176531330624919225119674426574742355349194934" | |
"96983520312774506326239578318016984801869478851843" |
Notes on how to use AWS SNS: | |
1. Subscribe an HTTP endpoint (i.e. http://myhost/sns_endpoint) on AWS Console | |
2. AWS will send subscription confirmation right away | |
3. SNS_controller responds to subscription confirmation by sending confirmation using Fog. | |
4. Once AWS is happy, you can start sending notifications to your end point via SNS. |