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
use utf8; | |
use strict; | |
use IO::File; | |
use IPC::Open2; | |
my $ofh = IO::File->new_tmpfile; | |
for (my $i = 1; $i < 20; ++$i) { | |
print $ofh ('x' x $i), "\n"; | |
} |
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
use utf8; | |
use strict; | |
use IO::File; | |
use IPC::Open2; | |
my $ifh0 = IO::Handle->new; | |
my $ifh1 = IO::File->new_tmpfile; | |
my $ofh0 = IO::Handle->new; | |
my $pid0 = open2($ifh0, $ofh0, qw(cat -n)); |
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
a :: Int | |
b :: Int | |
c :: Int | |
c = a + b | |
a = 5 | |
b = 7 | |
main = print c |
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
(use 'clojure.set) | |
(defn n-multiples [n end] (set (range 0 end n))) | |
(defn solve [] | |
(apply + (union (n-multiples 3 1000) | |
(n-multiples 5 1000)))) |
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
;; seq-end-inclusive を割れそうな数の候補 | |
(defn denominators [seq-end-inclusive] | |
(for [d (range 2 (+ seq-end-inclusive 1)) | |
:while (<= (* d d) seq-end-inclusive) | |
] | |
d)) | |
;; end-inclusive 以下の素数列をエラトステネスの篩で | |
(defn gen-primes [end-inclusive] | |
(reduce (fn [primes d] (filter #(or (= %1 d) |
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
(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 になるような組の列挙 |
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
;;; Project Euler Problem 23 solution | |
;;; http://projecteuler.net/problem=23 | |
(use 'clojure.test) | |
(def limit 28123) | |
(defn gen-sigma1-list | |
"original implementation by @ypsilon-takai: https://gist.github.com/4284814" | |
[^long size] |
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
;;; :s/0\ze\d/ /g | |
(def original-grid | |
[[ 8 2 22 97 38 15 0 40 0 75 4 5 7 78 52 12 50 77 91 8] | |
[49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 4 56 62 0] | |
[81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 3 49 13 36 65] | |
[52 70 95 23 4 60 11 42 69 24 68 56 1 32 56 71 37 2 36 91] | |
[22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80] | |
[24 47 32 60 99 3 45 2 44 75 33 53 78 36 84 20 35 17 12 50] | |
[32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70] | |
[67 26 20 68 2 62 12 20 95 63 94 39 63 8 40 91 66 49 94 21] |
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
(def solve | |
(memoize | |
(fn [w h] | |
(if (or (zero? w) | |
(zero? h)) | |
1 | |
(+ (solve (dec w) h) | |
(solve w (dec h))) | |
)))) |
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 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)))] |
OlderNewer