This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#light | |
(* 次のような考え方で解きました。 | |
割り算の筆算を考えると、小数部が循環するのは、余りが循環しているからである。 | |
割り算の筆算と同じ計算手順を踏みながら計算途中にあらわれる、余りをチェックして | |
行けばよい。もし以前と同じ余りが出現したら、それ以降は過去と同じ計算が行われる | |
ことになるので、算出される小数部は循環する。 | |
つまり、筆算の余りに同じものが現れるまでの計算回数が、循環小数の循環部の長さである。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn remainders | |
[n m] | |
(let [r (rem n m)] | |
(lazy-seq (cons r (remainders (* r 10) m))))) | |
(defn distance-of-same-value-elements | |
[xs] | |
(loop [i 0, dict {}, xs xs] | |
(let [x (first xs)] | |
(if-let [j (get dict x)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https://gist.github.com/1430157#file_euler26.fs の改良(高速化)版 | |
#light | |
let reccuringLength d = | |
let rec loop i dict n = | |
let r = n % d | |
match Map.tryFind r dict with | |
| Some j -> i - j | |
| None -> loop (i+1) (Map.add r i dict) (r * 10) | |
loop 0 Map.empty 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let distanceOfSameValueElements xs = | |
let rec loop i dict xs = | |
let x = LazyList.head xs | |
match Map.tryFind x dict with | |
| None -> loop (i + 1) (Map.add x i dict) (LazyList.tail xs) | |
| Some j -> i - j | |
loop 0 Map.empty (LazyList.ofSeq xs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see also. https://gist.github.com/1430157 | |
open Microsoft.FSharp.Collections | |
let distanceOfSameValueElements xs = | |
let rec loop i dict xs = | |
let x = LazyList.head xs | |
match Map.tryFind x dict with | |
| None -> loop (i + 1) (Map.add x i dict) (LazyList.tail xs) | |
| Some j -> i - j |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; seealso https://twitter.com/#!/mnzktw/status/155976688021405696 | |
(defn parse-args [args] | |
(->> (concat ["-f" nil] args) | |
(partition-by (partial = "-f") ) | |
(partition 2) | |
(map second) | |
((juxt (comp rest (partial map first)) | |
(partial mapcat rest))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct {unsigned short total, count; } s; | |
#define M_COUNT(x) (((s*)&x)->count) | |
#define M_TOTAL(x) (((s*)&x)->total) | |
int main(){ | |
int x = 0; | |
for(M_COUNT(x)=1; M_COUNT(x)<=100; ++M_COUNT(x)){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn mapn | |
([n f coll] | |
(->> (partition n coll) | |
(map (partial apply f)))) | |
([n m f coll] | |
(->> (partition n m coll) | |
(map (partial apply f)))) | |
([n m k f coll] | |
(->> (partition n m k coll) | |
(map (partial apply f))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro with-open-zipfile | |
[bindings & body] | |
(let [bindings# (->> bindings | |
(mapncat 2 (fn [v f] | |
`(~v (java.util.zip.ZipFile. ~f)))) | |
(into []))] | |
`(with-open ~bindings# ~@body))) | |
;; example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; こうじゃなくて | |
(and (not size) | |
(or width height)) (.setSize (or width 100) (or height 100)) | |
;; こうじゃないか? | |
(and (not size) | |
(not (and width height))) (.setSize (or width 100) (or height 100)) |