Four is magic: Difference between revisions

→‎{{header|Clojure}}: Add implementation.
(Added AppleScript.)
(→‎{{header|Clojure}}: Add implementation.)
Line 761:
Ten quintillion three hundred forty-four quadrillion six hundred fifty-eight trillion five hundred thirty-one billion two hundred seventy-seven million two hundred thousand nine hundred seventy-two is one hundred ninety-seven, one hundred ninety-seven is twenty-four, twenty-four is eleven, eleven is six, six is three, three is five, five is four, four is magic.
</pre>
 
=={{header|Clojure}}==
{{trans|UNIX Shell}}
<lang clojure>(require '[clojure.edn :as edn])
(def names { 0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five"
6 "six" 7 "seven" 8 "eight" 9 "nine" 10 "ten" 11 "eleven"
12 "twelve" 13 "thirteen" 14 "fourteen" 15 "fifteen"
16 "sixteen" 17 "seventeen" 18 "eighteen" 19 "nineteen"
20 "twenty" 30 "thirty" 40 "forty" 50 "fifty" 60 "sixty"
70 "seventy" 80 "eighty" 90 "ninety" 100 "hundred"
 
1000 "thousand" 1000000 "million" 1000000000 "billion"
1000000000000 "trillion" 1000000000000000 "quadrillion"
1000000000000000000 "quintillion" })
 
(def powers-of-10 (reverse (sort (filter #(clojure.string/ends-with? (str %) "00") (keys names)))))
 
(defn name-of [n]
(let [p (first (filter #(>= n %) powers-of-10))]
(cond
(not (nil? p))
(let [quotient (quot n p)
remainder (rem n p)]
(str (name-of quotient) " " (names p) (if (> remainder 0) (str " " (name-of remainder)))))
 
(and (nil? p) (> n 20))
(let [remainder (rem n 10)
tens (- n remainder)]
(str (names tens) (if (> remainder 0) (str " " (name-of remainder)))))
 
true
(names n))))
 
(defn four-is-magic
([n] (four-is-magic n ""))
([n prefix]
(let [name ((if (empty? prefix) clojure.string/capitalize identity) (name-of n))
new-prefix (str prefix (if (not (empty? prefix)) ", "))]
(if (= n 4)
(str new-prefix name " is magic.")
(let [len (count name)]
(four-is-magic len (str new-prefix name " is " (name-of len))))))))
 
(defn report [n]
(println (str n ": " (four-is-magic n))))
 
(defn -main [& args]
(doall (map (comp report edn/read-string) args)))
 
 
(if (not= "repl" *command-line-args*)
(apply -main *command-line-args*))
</lang>
 
{{Out}}
 
<pre>$ clj four-is-magic.clj 3252003274489856000 1114111 42 23 {0..9}
3252003274489856000: Three quintillion two hundred fifty two quadrillion three trillion two hundred seventy four billion four hundred eighty nine million eight hundred fifty six thousand is one hundred sixty five, one hundred sixty five is twenty two, twenty two is ten, ten is three, three is five, five is four, four is magic.
1114111: One million one hundred fourteen thousand one hundred eleven is sixty, sixty is five, five is four, four is magic.
42: Forty two is nine, nine is four, four is magic.
23: Twenty three is twelve, twelve is six, six is three, three is five, five is four, four is magic.
0: Zero is four, four is magic.
1: One is three, three is five, five is four, four is magic.
2: Two is three, three is five, five is four, four is magic.
3: Three is five, five is four, four is magic.
4: Four is magic.
5: Five is four, four is magic.
6: Six is three, three is five, five is four, four is magic.
7: Seven is five, five is four, four is magic.
8: Eight is five, five is four, four is magic.
9: Nine is four, four is magic.</pre>
 
=={{header|Common Lisp}}==
Line 775 ⟶ 846:
"One thousand twenty-four is twenty-four, twenty-four is eleven, eleven is six, six is three, three is five, five is four, four is magic."
</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
1,480

edits