Four is magic: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 61: | Line 61: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">V Small = [‘zero’, ‘one’, ‘two’, ‘three’, ‘four’, |
||
‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’, |
‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’, |
||
‘ten’, ‘eleven’, ‘twelve’, ‘thirteen’, ‘fourteen’, |
‘ten’, ‘eleven’, ‘twelve’, ‘thirteen’, ‘fourteen’, |
||
Line 117: | Line 117: | ||
L(n) [Int64(0), 4, 6, 11, 13, 75, 100, 337, -164, 7FFF'FFFF'FFFF'FFFF] |
L(n) [Int64(0), 4, 6, 11, 13, 75, 100, 337, -164, 7FFF'FFFF'FFFF'FFFF] |
||
print(fourIsMagic(n))</ |
print(fourIsMagic(n))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 134: | Line 134: | ||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
< |
<syntaxhighlight lang="asm">puts: equ 9h ; MS-DOS syscall to print a string |
||
cpu 8086 |
cpu 8086 |
||
bits 16 |
bits 16 |
||
Line 336: | Line 336: | ||
errhigh: db 'Max input 999999$' |
errhigh: db 'Max input 999999$' |
||
section .bss |
section .bss |
||
numstring: resb 1024</ |
numstring: resb 1024</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 359: | Line 359: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">magic←{ |
||
t20←'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' |
t20←'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' |
||
t20←t20,'ten' 'eleven' 'twelve' 'thirteen' 'fourteen' 'fifteen' 'sixteen' |
t20←t20,'ten' 'eleven' 'twelve' 'thirteen' 'fourteen' 'fifteen' 'sixteen' |
||
Line 385: | Line 385: | ||
n,' is ',(spell ≢n),', ',∇≢n |
n,' is ',(spell ≢n),', ',∇≢n |
||
}⍵ |
}⍵ |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> magic 0 |
<pre> magic 0 |
||
Line 405: | Line 405: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">(* Uses a Foundation number formatter for brevity. *) |
||
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later |
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later |
||
use framework "Foundation" |
use framework "Foundation" |
||
Line 457: | Line 457: | ||
set end of output to fourIsMagic(n) |
set end of output to fourIsMagic(n) |
||
end repeat |
end repeat |
||
return join(output, linefeed)</ |
return join(output, linefeed)</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">"Negative nineteen is seventeen, seventeen is nine, nine is four, four is magic. |
||
Zero is four, four is magic. |
Zero is four, four is magic. |
||
Four is magic. |
Four is magic. |
||
Line 466: | Line 466: | ||
Thirty-two is ten, ten is three, three is five, five is four, four is magic. |
Thirty-two is ten, ten is three, three is five, five is four, four is magic. |
||
One hundred eleven is eighteen, eighteen is eight, eight is five, five is four, four is magic. |
One hundred eleven is eighteen, eighteen is eight, eight is five, five is four, four is magic. |
||
One billion two hundred thirty-four million five hundred sixty-five thousand seven hundred eighty-nine is one hundred two, one hundred two is fifteen, fifteen is seven, seven is five, five is four, four is magic."</ |
One billion two hundred thirty-four million five hundred sixty-five thousand seven hundred eighty-nine is one hundred two, one hundred two is fifteen, fifteen is seven, seven is five, five is four, four is magic."</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Based on [http://www.rosettacode.org/wiki/Number_names#AutoHotkey Number names] |
Based on [http://www.rosettacode.org/wiki/Number_names#AutoHotkey Number names] |
||
< |
<syntaxhighlight lang="autohotkey">Four_is_magic(num){ |
||
nubmer := num |
nubmer := num |
||
while (num <> 4) |
while (num <> 4) |
||
Line 496: | Line 496: | ||
PrettyNumber(n) { ; inserts thousands separators into a number string |
PrettyNumber(n) { ; inserts thousands separators into a number string |
||
Return RegExReplace(n, "\B(?=((\d{3})+$))", ",") |
Return RegExReplace(n, "\B(?=((\d{3})+$))", ",") |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">for i, num in StrSplit("7,54,235,8463,95723,485723,5472539,15750268,853956201,2736452849,94837286837,636478294710", ",") |
||
result .= Four_is_magic(num) "`n" |
result .= Four_is_magic(num) "`n" |
||
MsgBox % result</ |
MsgBox % result</syntaxhighlight> |
||
Outputs:<pre>7 seven is five, five is four, four is magic! |
Outputs:<pre>7 seven is five, five is four, four is magic! |
||
54 fifty-four is ten, ten is three, three is five, five is four, four is magic! |
54 fifty-four is ten, ten is three, three is five, five is four, four is magic! |
||
Line 514: | Line 514: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f FOUR_IS_MAGIC.AWK |
# syntax: GAWK -f FOUR_IS_MAGIC.AWK |
||
BEGIN { |
BEGIN { |
||
Line 577: | Line 577: | ||
split("ten twenty thirty forty fifty sixty seventy eighty ninety",tens," ") |
split("ten twenty thirty forty fifty sixty seventy eighty ninety",tens," ") |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 601: | Line 601: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{libheader|GLib}} |
{{libheader|GLib}} |
||
< |
<syntaxhighlight lang="c">#include <stdint.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <glib.h> |
#include <glib.h> |
||
Line 698: | Line 698: | ||
test_magic(10344658531277200972ULL); |
test_magic(10344658531277200972ULL); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 716: | Line 716: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
Negative numbers are not supported. |
Negative numbers are not supported. |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
#include <cctype> |
#include <cctype> |
||
Line 821: | Line 821: | ||
test_magic(10344658531277200972ULL); |
test_magic(10344658531277200972ULL); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 839: | Line 839: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|UNIX Shell}} |
{{trans|UNIX Shell}} |
||
< |
<syntaxhighlight lang="clojure">(require '[clojure.edn :as edn]) |
||
(def names { 0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five" |
(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" |
6 "six" 7 "seven" 8 "eight" 9 "nine" 10 "ten" 11 "eleven" |
||
Line 888: | Line 888: | ||
(if (not= "repl" *command-line-args*) |
(if (not= "repl" *command-line-args*) |
||
(apply -main *command-line-args*)) |
(apply -main *command-line-args*)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 909: | Line 909: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun integer-to-text (int) |
||
(format nil "~@(~A~)" (with-output-to-string (out) |
(format nil "~@(~A~)" (with-output-to-string (out) |
||
(loop for n = int then (length c) |
(loop for n = int then (length c) |
||
Line 915: | Line 915: | ||
while (/= n 4) |
while (/= n 4) |
||
do (format out "~A is ~R, " c (length c)) |
do (format out "~A is ~R, " c (length c)) |
||
finally (format out "four is magic.")))))</ |
finally (format out "four is magic.")))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 924: | Line 924: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Four_is_magic; |
program Four_is_magic; |
||
Line 1,033: | Line 1,033: | ||
writeln(fourIsMagic(n)); |
writeln(fourIsMagic(n)); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Zero is four, four is magic. |
<pre>Zero is four, four is magic. |
||
Line 1,048: | Line 1,048: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
===The Function=== |
===The Function=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
//Express an Integer in English Language. Nigel Galloway: September 19th., 2018 |
//Express an Integer in English Language. Nigel Galloway: September 19th., 2018 |
||
let fN=[|[|"";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"|]; |
let fN=[|[|"";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"|]; |
||
Line 1,057: | Line 1,057: | ||
|α when α<1000 ->I2α (α-(α/100)*100) (β+fN.[0].[α/100]+" hunred"+if α%100>0 then " and " else "") |
|α when α<1000 ->I2α (α-(α/100)*100) (β+fN.[0].[α/100]+" hunred"+if α%100>0 then " and " else "") |
||
|α when α<1000000->I2α (α%1000) (β+(I2α (α/1000) "")+" thousand"+if α%100=0 then "" else if (α-(α/1000)*1000)<100 then " and " else " ") |
|α when α<1000000->I2α (α%1000) (β+(I2α (α/1000) "")+" thousand"+if α%100=0 then "" else if (α-(α/1000)*1000)<100 then " and " else " ") |
||
</syntaxhighlight> |
|||
</lang> |
|||
===The Task=== |
===The Task=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let rec printI2α=function |0->printf "naught->"; printI2α 6 |
let rec printI2α=function |0->printf "naught->"; printI2α 6 |
||
|4->printfn "four is magic" |
|4->printfn "four is magic" |
||
Line 1,066: | Line 1,066: | ||
let N=System.Random() |
let N=System.Random() |
||
List.init 25 (fun _->N.Next 999999) |> List.iter printI2α |
List.init 25 (fun _->N.Next 999999) |> List.iter printI2α |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,105: | Line 1,105: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Factor's <code>math.text.english</code> vocabulary does most of the heavy lifting. Since <code>number>text</code> produces <tt><i>" and "</i></tt> and <tt><i>","</i></tt> in its output, they are removed with a regular expression. |
Factor's <code>math.text.english</code> vocabulary does most of the heavy lifting. Since <code>number>text</code> produces <tt><i>" and "</i></tt> and <tt><i>","</i></tt> in its output, they are removed with a regular expression. |
||
< |
<syntaxhighlight lang="factor">USING: ascii formatting io kernel make math.text.english regexp |
||
sequences ; |
sequences ; |
||
IN: rosetta-code.four-is-magic |
IN: rosetta-code.four-is-magic |
||
Line 1,138: | Line 1,138: | ||
len-chain [ phrase ] map concat capitalize print ; |
len-chain [ phrase ] map concat capitalize print ; |
||
{ 1 4 -11 100 112719908181724 -612312 } [ say-magic ] each</ |
{ 1 4 -11 100 112719908181724 -612312 } [ say-magic ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,150: | Line 1,150: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran">MODULE FOUR_IS_MAGIC |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
CHARACTER(8), DIMENSION(20) :: SMALL_NUMS |
CHARACTER(8), DIMENSION(20) :: SMALL_NUMS |
||
Line 1,389: | Line 1,389: | ||
ENDDO |
ENDDO |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
five is four, four is magic. |
five is four, four is magic. |
||
Line 1,405: | Line 1,405: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Phix}} |
{{trans|Phix}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
#define floor(x) ((x*2.0-0.5) Shr 1) |
#define floor(x) ((x*2.0-0.5) Shr 1) |
||
Line 1,491: | Line 1,491: | ||
Print Using "#######: &"; tests(i); fourIsMagic(tests(i)) |
Print Using "#######: &"; tests(i); fourIsMagic(tests(i)) |
||
Next i |
Next i |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,521: | Line 1,521: | ||
Uses the <code>say</code> function from the |
Uses the <code>say</code> function from the |
||
[[Number names#Go|Number names]] task. |
[[Number names#Go|Number names]] task. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,600: | Line 1,600: | ||
} |
} |
||
return t |
return t |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,618: | Line 1,618: | ||
Negative numbers are supported. |
Negative numbers are supported. |
||
< |
<syntaxhighlight lang="haskell">module Main where |
||
import Data.List (find) |
import Data.List (find) |
||
Line 1,678: | Line 1,678: | ||
putStrLn $ magic (-13) |
putStrLn $ magic (-13) |
||
putStrLn $ magic 999999 |
putStrLn $ magic 999999 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,692: | Line 1,692: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang J> |
|||
names =. 'one';'two';'three';'four';'five';'six';'seven';'eight';'nine';'ten';'eleven';'twelve';'thirteen';'fourteen';'fifteen';'sixteen';'seventeen';'eighteen';'nineteen' |
names =. 'one';'two';'three';'four';'five';'six';'seven';'eight';'nine';'ten';'eleven';'twelve';'thirteen';'fourteen';'fifteen';'sixteen';'seventeen';'eighteen';'nineteen' |
||
Line 1,737: | Line 1,737: | ||
doall inputs |
doall inputs |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,763: | Line 1,763: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang="java"> |
|||
<lang Java> |
|||
public class FourIsMagic { |
public class FourIsMagic { |
||
Line 1,837: | Line 1,837: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,868: | Line 1,868: | ||
To test whether a particular JavaScript interpreter implements <code>BigInt</code>, we can evaluate a boolean expression like: |
To test whether a particular JavaScript interpreter implements <code>BigInt</code>, we can evaluate a boolean expression like: |
||
< |
<syntaxhighlight lang="javascript">Object.getOwnPropertyNames(this).includes('BigInt')</syntaxhighlight> |
||
< |
<syntaxhighlight lang="javascript">const reverseOrderedNumberToTextMap = (function () { |
||
const rawNumberToTextMapping = { // Ported over from the Python solution. |
const rawNumberToTextMapping = { // Ported over from the Python solution. |
||
[1n]: "one", |
[1n]: "one", |
||
Line 2,050: | Line 2,050: | ||
-4, |
-4, |
||
10n ** 3003n + 42n |
10n ** 3003n + 42n |
||
].map(fourIsMagic).join("\n\n");</ |
].map(fourIsMagic).join("\n\n");</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 2,070: | Line 2,070: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"># The num2text routines are from the "Number names" task, updated for Julia 1.0 |
||
const stext = ["one", "two", "three", "four", "five", |
const stext = ["one", "two", "three", "four", "five", |
||
Line 2,171: | Line 2,171: | ||
magic(n) |
magic(n) |
||
end |
end |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
Zero is four, four is magic. |
Zero is four, four is magic. |
||
Four is magic. |
Four is magic. |
||
Line 2,185: | Line 2,185: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
This uses the code I wrote for the [[Number names]] task, appropriately adjusted to deal with this task. Input is limited to '''signed''' 64 bit integers as Kotlin doesn't currently support unsigned types. |
This uses the code I wrote for the [[Number names]] task, appropriately adjusted to deal with this task. Input is limited to '''signed''' 64 bit integers as Kotlin doesn't currently support unsigned types. |
||
< |
<syntaxhighlight lang="scala">// version 1.1.4-3 |
||
val names = mapOf( |
val names = mapOf( |
||
Line 2,292: | Line 2,292: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,318: | Line 2,318: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Four is magic, in Lua, 6/16/2020 db |
||
local oneslist = { [0]="", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" } |
local oneslist = { [0]="", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" } |
||
local teenlist = { [0]="ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" } |
local teenlist = { [0]="ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" } |
||
Line 2,361: | Line 2,361: | ||
for _, num in ipairs(numbers) do |
for _, num in ipairs(numbers) do |
||
print(num, fourismagic(num)) |
print(num, fourismagic(num)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>-21 Negative twenty-one is nineteen, nineteen is eight, eight is five, five is four, four is magic. |
<pre>-21 Negative twenty-one is nineteen, nineteen is eight, eight is five, five is four, four is magic. |
||
Line 2,389: | Line 2,389: | ||
Define a simple function which generates the output, using FixedPointList to iterate until a magic number is reached. |
Define a simple function which generates the output, using FixedPointList to iterate until a magic number is reached. |
||
< |
<syntaxhighlight lang="mathematica">magic[num_] := Capitalize[ StringRiffle[ Partition[ |
||
FixedPointList[IntegerName[StringLength[#], "Cardinal"] &, IntegerName[num, "Cardinal"]], |
FixedPointList[IntegerName[StringLength[#], "Cardinal"] &, IntegerName[num, "Cardinal"]], |
||
2, 1] /. {n_, n_} :> {n, "magic"}, ", ", " is "] <> "."]</ |
2, 1] /. {n_, n_} :> {n, "magic"}, ", ", " is "] <> "."]</syntaxhighlight> |
||
Call the function a few times to show the expected output: |
Call the function a few times to show the expected output: |
||
Line 2,414: | Line 2,414: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
const |
const |
||
Line 2,480: | Line 2,480: | ||
for n in [int64 0, 4, 6, 11, 13, 75, 100, 337, -164, int64.high]: |
for n in [int64 0, 4, 6, 11, 13, 75, 100, 337, -164, int64.high]: |
||
echo fourIsMagic(n)</ |
echo fourIsMagic(n)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,496: | Line 2,496: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use Lingua::EN::Numbers qw(num2en); |
||
sub cardinal { |
sub cardinal { |
||
Line 2,520: | Line 2,520: | ||
} |
} |
||
print magic($_) for 0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209;</ |
print magic($_) for 0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Zero is four, four is magic. |
<pre>Zero is four, four is magic. |
||
Line 2,534: | Line 2,534: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Note that on 32-bit Phix integers/atoms are only accurate to 9,007,199,254,740,992 (a hardware limit of 64-bit floating point registers) and on 64-bit the limit is 18,446,744,073,709,551,616 (ditto 80-bit floating points) so if you need more than that this will need to be reworked to use gmp or similar. |
Note that on 32-bit Phix integers/atoms are only accurate to 9,007,199,254,740,992 (a hardware limit of 64-bit floating point registers) and on 64-bit the limit is 18,446,744,073,709,551,616 (ditto 80-bit floating points) so if you need more than that this will need to be reworked to use gmp or similar. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #000080;font-style:italic;">--<adapted from demo\rosetta\number_names.exw, which alas outputs ",", "and", uses "minus" instead of "negative", etc...></span> |
<span style="color: #000080;font-style:italic;">--<adapted from demo\rosetta\number_names.exw, which alas outputs ",", "and", uses "minus" instead of "negative", etc...></span> |
||
Line 2,613: | Line 2,613: | ||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fourIsMagic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fourIsMagic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,635: | Line 2,635: | ||
Python 3 version. Should work for integers up to at least 10^3003. It can be extended easily to arbitrary integers by adding to the numbers dict. |
Python 3 version. Should work for integers up to at least 10^3003. It can be extended easily to arbitrary integers by adding to the numbers dict. |
||
< |
<syntaxhighlight lang="python">import random |
||
from collections import OrderedDict |
from collections import OrderedDict |
||
Line 2,798: | Line 2,798: | ||
for j in (random.randint(-10 ** 24, 10 ** 24) for i in range(2)): |
for j in (random.randint(-10 ** 24, 10 ** 24) for i in range(2)): |
||
print(j, ':\n', magic(j), '\n')</ |
print(j, ':\n', magic(j), '\n')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,822: | Line 2,822: | ||
Eight hundred seventy four sextillion one hundred forty three quintillion four hundred twenty five quadrillion eight hundred fifty five trillion seven hundred forty five billion seven hundred thirty three million eight hundred ninety six thousand thirty is two hundred fifty three, two hundred fifty three is twenty three, twenty three is twelve, twelve is six, six is three, three is five, five is four, four is magic. </pre> |
Eight hundred seventy four sextillion one hundred forty three quintillion four hundred twenty five quadrillion eight hundred fifty five trillion seven hundred forty five billion seven hundred thirty three million eight hundred ninety six thousand thirty is two hundred fifty three, two hundred fifty three is twenty three, twenty three is twelve, twelve is six, six is three, three is five, five is four, four is magic. </pre> |
||
=={{header|q}}== |
=={{header|q}}== |
||
< |
<syntaxhighlight lang="q">C:``one`two`three`four`five`six`seven`eight`nine`ten, |
||
`eleven`twelve`thirteen`fourteen`fifteen`sixteen`seventeen`eighteen`nineteen / cardinal numbers <20 |
`eleven`twelve`thirteen`fourteen`fifteen`sixteen`seventeen`eighteen`nineteen / cardinal numbers <20 |
||
Line 2,837: | Line 2,837: | ||
fim:{@[;0;upper],[;"four is magic.\n"] raze 1_{y," is ",x,", "}prior s each(count s@)\[x]} / four is magic |
fim:{@[;0;upper],[;"four is magic.\n"] raze 1_{y," is ",x,", "}prior s each(count s@)\[x]} / four is magic |
||
1 raze fim each 0 4 8 16 25 89 365 2586 25865 369854 40000000001; / tests</ |
1 raze fim each 0 4 8 16 25 89 365 2586 25865 369854 40000000001; / tests</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Zero is four, four is magic. |
<pre>Zero is four, four is magic. |
||
Line 2,853: | Line 2,853: | ||
In q the same syntax applies a function to an argument or a list to its indexes. A consequence is that, with the Converge iterator <code>\</code> the lengths alone form a finite-state machine which can generate the convergence. |
In q the same syntax applies a function to an argument or a list to its indexes. A consequence is that, with the Converge iterator <code>\</code> the lengths alone form a finite-state machine which can generate the convergence. |
||
< |
<syntaxhighlight lang="q">q)show sl:count each string C / string lengths |
||
0 3 3 5 4 4 3 5 5 4 3 6 6 8 8 7 7 9 8 8 |
0 3 3 5 4 4 3 5 5 4 3 6 6 8 8 7 7 9 8 8 |
||
q)sl\[18] |
q)sl\[18] |
||
18 8 5 4 |
18 8 5 4 |
||
q)sl\[19] |
q)sl\[19] |
||
19 8 5 4</ |
19 8 5 4</syntaxhighlight> |
||
* [https://code.kx.com/q/ref/ Language Reference] |
* [https://code.kx.com/q/ref/ Language Reference] |
||
* [https://code.kx.com/q/learn/pb/four-magic/ The Q Playbook: Four is magic – analysis] |
* [https://code.kx.com/q/learn/pb/four-magic/ The Q Playbook: Four is magic – analysis] |
||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
# provided by neonira |
# provided by neonira |
||
Line 2,927: | Line 2,927: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,943: | Line 2,943: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 2,999: | Line 2,999: | ||
(displayln (number-magic n)) |
(displayln (number-magic n)) |
||
(newline)) |
(newline)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,141: | Line 3,141: | ||
Lingua::EN::Numbers module available from the [https://modules.raku.org/search/?q=Lingua%3A%3AEN%3A%3ANumbers Raku ecosystem]. |
Lingua::EN::Numbers module available from the [https://modules.raku.org/search/?q=Lingua%3A%3AEN%3A%3ANumbers Raku ecosystem]. |
||
<lang |
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers; # Version 2.4.0 or higher |
||
sub card ($n) { cardinal($n).subst(/','/, '', :g) } |
sub card ($n) { cardinal($n).subst(/','/, '', :g) } |
||
Line 3,159: | Line 3,159: | ||
} |
} |
||
.&magic.say for 0, 4, 6, 11, 13, 75, 337, -164, 9876543209, 2**256;</ |
.&magic.say for 0, 4, 6, 11, 13, 75, 337, -164, 9876543209, 2**256;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="width:98%;overflow:wrap;">Zero is four, four is magic. |
<pre style="width:98%;overflow:wrap;">Zero is four, four is magic. |
||
Line 3,185: | Line 3,185: | ||
Numbers are limited to 3,003 decimal digits, the maximum number that the '''$SPELL#''' REXX program will handle. |
Numbers are limited to 3,003 decimal digits, the maximum number that the '''$SPELL#''' REXX program will handle. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm converts a # to English into the phrase: a is b, b is c, ... four is magic. */ |
||
numeric digits 3003 /*be able to handle gihugic numbers. */ |
numeric digits 3003 /*be able to handle gihugic numbers. */ |
||
parse arg x /*obtain optional numbers from the C.L.*/ |
parse arg x /*obtain optional numbers from the C.L.*/ |
||
Line 3,206: | Line 3,206: | ||
$= strip($ 'four is magic.') /*finish the sentence with the finale. */ |
$= strip($ 'four is magic.') /*finish the sentence with the finale. */ |
||
parse var $ first 2 other; upper first /*capitalize the first letter of output*/ |
parse var $ first 2 other; upper first /*capitalize the first letter of output*/ |
||
return first || other /*return the sentence to the invoker. */</ |
return first || other /*return the sentence to the invoker. */</syntaxhighlight> |
||
The '''$SPELL#.REX''' routine can be found here ───► [[$SPELL.REX|$SPELL#.REX]]. <br><br> |
The '''$SPELL#.REX''' routine can be found here ───► [[$SPELL.REX|$SPELL#.REX]]. <br><br> |
||
Line 3,234: | Line 3,234: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
/* Checking numbers from 0 to 10 */ |
/* Checking numbers from 0 to 10 */ |
||
for c = 0 to 10 |
for c = 0 to 10 |
||
Line 3,345: | Line 3,345: | ||
Return trim(Result) |
Return trim(Result) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,363: | Line 3,363: | ||
Using a 'refinement' to the Integer class, a way to a way to extend a class locally. |
Using a 'refinement' to the Integer class, a way to a way to extend a class locally. |
||
< |
<syntaxhighlight lang="ruby">module NumberToWord |
||
NUMBERS = { # taken from https://en.wikipedia.org/wiki/Names_of_large_numbers#cite_ref-a_14-3 |
NUMBERS = { # taken from https://en.wikipedia.org/wiki/Names_of_large_numbers#cite_ref-a_14-3 |
||
Line 3,439: | Line 3,439: | ||
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each{|n| puts magic4(n) } |
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each{|n| puts magic4(n) } |
||
</ |
</syntaxhighlight> |
||
{{out}}<pre>Zero is four, four is magic. |
{{out}}<pre>Zero is four, four is magic. |
||
Four is magic. |
Four is magic. |
||
Line 3,452: | Line 3,452: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
magic(4); |
magic(4); |
||
magic(2_340); |
magic(2_340); |
||
Line 3,529: | Line 3,529: | ||
} |
} |
||
return cur; |
return cur; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,550: | Line 3,550: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func cardinal(n) { |
||
static lingua_en = frequire("Lingua::EN::Numbers") |
static lingua_en = frequire("Lingua::EN::Numbers") |
||
lingua_en.num2en(n) - / and|,/g |
lingua_en.num2en(n) - / and|,/g |
||
Line 3,572: | Line 3,572: | ||
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each { |n| |
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each { |n| |
||
say four_is_magic(n) |
say four_is_magic(n) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,587: | Line 3,587: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func fourIsMagic(_ number: NSNumber) -> String { |
func fourIsMagic(_ number: NSNumber) -> String { |
||
Line 3,610: | Line 3,610: | ||
for testInput in [23, 1000000000, 20140, 100, 130, 151, -7] { |
for testInput in [23, 1000000000, 20140, 100, 130, 151, -7] { |
||
print(fourIsMagic(testInput as NSNumber)) |
print(fourIsMagic(testInput as NSNumber)) |
||
} </ |
} </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,624: | Line 3,624: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bash|4+}} |
{{works with|Bash|4+}} |
||
< |
<syntaxhighlight lang="sh">#!/usr/bin/env bash |
||
name_of() { |
name_of() { |
||
# return the English name for a numeric value |
# return the English name for a numeric value |
||
Line 3,704: | Line 3,704: | ||
four_is_magic "$len" "${prefix:+$prefix, }$name is $(name_of $len)" |
four_is_magic "$len" "${prefix:+$prefix, }$name is $(name_of $len)" |
||
fi |
fi |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,742: | Line 3,742: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">import math |
||
fn main() { |
fn main() { |
||
for n in [i64(0), 4, 6, 11, 13, 75, 100, 337, -164, |
for n in [i64(0), 4, 6, 11, 13, 75, 100, 337, -164, |
||
Line 3,817: | Line 3,817: | ||
} |
} |
||
return t |
return t |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,839: | Line 3,839: | ||
Note that it is not safe to use this script for numbers with an absolute magnitude >= 2^53 as integers cannot be expressed exactly by Wren's Num type beyond that limit. |
Note that it is not safe to use this script for numbers with an absolute magnitude >= 2^53 as integers cannot be expressed exactly by Wren's Num type beyond that limit. |
||
< |
<syntaxhighlight lang="ecmascript">import "/str" for Str |
||
var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", |
var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", |
||
Line 3,898: | Line 3,898: | ||
for (n in [0, 4, 6, 11, 13, 75, 100, 337, -164, 9007199254740991]) { |
for (n in [0, 4, 6, 11, 13, 75, 100, 337, -164, 9007199254740991]) { |
||
System.print(fourIsMagic.call(n)) |
System.print(fourIsMagic.call(n)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,920: | Line 3,920: | ||
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]] |
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]] |
||
< |
<syntaxhighlight lang="zkl">fcn fourIsMagic(int){ |
||
if(int==0) return("Zero is four, four is magic."); |
if(int==0) return("Zero is four, four is magic."); |
||
string:=""; |
string:=""; |
||
Line 3,933: | Line 3,933: | ||
} |
} |
||
string[0].toUpper() + string[1,*] |
string[0].toUpper() + string[1,*] |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach n in (T(0,4,6,11,13,75,337,-164,9876543209)){ |
||
println(fourIsMagic(n),"\n") |
println(fourIsMagic(n),"\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |