nptclのブログ

Common Lisp処理系nptの開発メモです。https://github.com/nptcl/npt

整数を英語で表現する5(Lispコード)

だいぶ前に、整数を英語で表現する方法について説明しました。

整数を英語で表現する1 - nptclのブログ
整数を英語で表現する2(中学レベル) - nptclのブログ
整数を英語で表現する3(巨大な数) - nptclのブログ
整数を英語で表現する4(序数と負数) - nptclのブログ

これらの説明に基づいて、Common Lispで実装しましたので配布します。

cwsystem
https://github.com/nptcl/cwsystem

以下、説明です。

radix-string

整数を英語で表現するには、関数radix-stringを使用します。
機能は(format nil "~R" x)と同じですが、速度とメモリが許す限り巨大な数値を表せます。
いくつか例を示します。

通常の使用

(cwsystem:radix-string 123)
"one hundred twenty-three"

マイナス

 (cwsystem:radix-string -4)
"minus four"

序数

(cwsystem:radix-string 20 nil)
"twentieth"

巨大な数

(cwsystem:radix-string (ash 1 200))
"one novendecillion six hundred six octodecillion nine hundred thirty-eight septendecillion forty-four sedecillion two hundred fifty-eight quindecillion nine hundred ninety quattuordecillion two hundred seventy-five tredecillion five hundred forty-one duodecillion nine hundred sixty-two undecillion ninety-two decillion three hundred forty-one nonillion one hundred sixty-two octillion six hundred two septillion five hundred twenty-two sextillion two hundred two quintillion nine hundred ninety-three quadrillion seven hundred eighty-two trillion seven hundred ninety-two billion eight hundred thirty-five million three hundred one thousand three hundred seventy-six"

unit-string

3桁区切りの単位を取得する関数unit-stringもあります。

通常の使用

(cwsystem:unit-string 0)
"thousand"

(cwsystem:unit-string 1)
"million"

(cwsystem:unit-string 2)
"billion"

The Conway-Wechsler Systemの3桁

(cwsystem:unit-string 789)
"novemoctogintaseptingentillion"

序数

(cwsystem:unit-string 345 nil)
"quinquadragintatrecentillionth"

3桁の連結

(cwsystem:unit-string 1234567890)
"milliquattuortrigintaducentilliseptensexagintaquingentillinonagintaoctingentillion"