FRACTRAN
code:fractran.clj
(ns prime
(:require
;; エラトステネスの篩で素數列を生成する
(deftype Primes
clojure.lang.ISeq
;; cons は意味を成さない
(more
(let [[prime numbers] (->> numbers (filter #(not (zero? (mod % prime)))) (split-at 1))]
(Primes. prime numbers)))
(next
(let [[prime numbers] (->> numbers (filter #(not (zero? (mod % prime)))) (split-at 1))]
(Primes. prime numbers)))
(def primes
"エラトステネスの篩で素數列を生成する"
(Primes. 2 (drop 2 (range))))
(defn composite
"素因數の指數列から自然數を復元する
(->> factors
(map vector primes)
(map (fn l r (math/pow l r)))
(reduce (fn l r (* l r))) int))
(defn factorization
"素因數分解し、指數を素數列の昇順に竝べた列
(loop [factors []
primes primes
n n]
(if (= n 1)
factors
n n]
(if (zero? (mod n prime))
(recur (inc factor)
(/ n prime))
(recur (conj factors factor)
(drop 1 primes)
n))))))
(defn product
"積
(map (fn i (+ (nth l i 0) (nth r i 0))) (range (max (count l) (count r)))))
(ns rational
"素因數分解された分子と分母の組
2/3 = 1] [0 1")
(defn reduction
"約分"
n d
[(->> n
(map vector (range))
reverse
reverse)
(->> d
(map vector (range))
reverse
reverse)])
(defn clj->rational
"Clojure の有理數から、素因數分解された有理數に變換する"
[(prime/factorization (if (integer? clj-rational) clj-rational (numerator clj-rational)))
(prime/factorization (if (integer? clj-rational) 1 (denominator clj-rational)))])
(defn rational->clj
"素因數分解された有理數から、Clojure の有理數に變換する"
n d
(/ (prime/composite n) (prime/composite d)))
(defn product
"積"
ln ld] [rn rd
(reduction [(prime/product ln rn)
(prime/product ld rd)]))
(defn integerr?
"整數? 分母が 1?"
n d
(= (count d) 0))
(ns fractran
(:require
(defn- fractran-in-rational
""
(->> program
first
(if (nil? n)
input
(fractran-in-rational program n))))))
(defn fractran
"FRACTRAN.
(nth (fractran-in-rational (map rational/clj->rational program)
[input []])
0))
(ns user)
(defn eval-print-fractran
"FRACTRAN を實行し、結果を表示する"
(println "program: " program)
(println "input: " input)
(println "output: " (fractran/fractran program input))
(println))
(println "加算")
(println "乘算")
(println "除算")