Four is magic: Difference between revisions
Content added Content deleted
No edit summary |
(Four is magic en FreeBASIC) |
||
Line 937: | Line 937: | ||
Negative six hundred twelve thousand three hundred twelve is fifty-seven, fifty-seven is eleven, eleven is six, six is three, three is five, five is four, four is magic. |
Negative six hundred twelve thousand three hundred twelve is fifty-seven, fifty-seven is eleven, eleven is six, six is three, three is five, five is four, four is magic. |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|Phix}} |
|||
<lang freebasic> |
|||
#define floor(x) ((x*2.0-0.5) Shr 1) |
|||
Dim Shared veintes(1 To 20) As String*9 => _ |
|||
{"zero", "one", "two", "three", "four", "five", "six", _ |
|||
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", _ |
|||
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"} |
|||
Dim Shared decenas(1 To 8) As String*7 => _ |
|||
{"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"} |
|||
Type myorder |
|||
var1 As Double |
|||
var2 As String*8 |
|||
End Type |
|||
Dim Shared orders(1 To 4) As myorder => _ |
|||
{(10^12,"trillion"), (10^9,"billion"), (10^6,"million"), (10^3,"thousand")} |
|||
Function centenas(n As Integer) As String |
|||
If n < 20 Then |
|||
Return veintes((n Mod 20)+1) |
|||
Elseif (n Mod 10) = 0 Then |
|||
Return decenas((floor(n/10) Mod 10)-1) |
|||
End If |
|||
Return decenas((floor(n/10) Mod 10)-1) & "-" & veintes((n Mod 10)+1) |
|||
End Function |
|||
Function miles(n As Integer) As String |
|||
If n < 100 Then |
|||
Return centenas(n) |
|||
Elseif (n Mod 100) = 0 Then |
|||
Return veintes((floor(n/100) Mod 20)+1) & " centenas" |
|||
End If |
|||
Return veintes((floor(n/100) Mod 20)+1) & " centenas " & centenas(n Mod 100) |
|||
End Function |
|||
Function triplet(n As Integer) As String |
|||
Dim As Integer order, high, low |
|||
Dim As String nombre, res = "" |
|||
For i As Integer = 1 To Ubound(orders) |
|||
order = orders(i).var1 |
|||
nombre = orders(i).var2 |
|||
high = floor(n/order) |
|||
low = (n Mod order) |
|||
If high <> 0 Then res &= miles(high) & " " & nombre |
|||
n = low |
|||
If low = 0 Then Exit For : End If |
|||
If Len(res) And high <> 0 Then res &= " " |
|||
Next i |
|||
If n <> 0 Or res="" Then |
|||
res &= miles(floor(n)) |
|||
End If |
|||
Return res |
|||
End Function |
|||
Function deletrear(n As Integer) As String |
|||
Dim As String res = "" |
|||
If n < 0 Then |
|||
res = "negative " |
|||
n = -n |
|||
End If |
|||
res &= triplet(n) |
|||
Return res |
|||
End Function |
|||
Function fourIsMagic(n As Integer) As String |
|||
Dim As String s = deletrear(n) |
|||
s = Mid(Ucase(s), 1, 1) & Mid(s, 2, Len(s)) |
|||
Dim As String t = s |
|||
While n <> 4 |
|||
n = Len(s) |
|||
s = deletrear(n) |
|||
t &= " is " & s & ", " & s |
|||
Wend |
|||
t &= " is magic." |
|||
Return t |
|||
End Function |
|||
Dim As Longint tests(1 To 21) = _ |
|||
{-21, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, _ |
|||
34, 123, 456, 1024, 1234, 12345, 123456, 1010101} |
|||
For i As Integer = 1 To Ubound(tests) |
|||
Print Using "#######: &"; tests(i); fourIsMagic(tests(i)) |
|||
Next i |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre> |
|||
-21: Negative twenty-one is nineteen, nineteen is eight, eight is five, five is four, four is magic. |
|||
-1: Negative one 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. |
|||
12: Twelve is six, six is three, three is five, five is four, four is magic. |
|||
34: Thirty-four is eleven, eleven is six, six is three, three is five, five is four, four is magic. |
|||
123: One centenas twenty-three is twenty-five, twenty-five is eleven, eleven is six, six is three, three is five, five is four, four is magic. |
|||
456: Four centenas fifty-six is twenty-three, twenty-three is twelve, twelve is six, six is three, three is five, five is four, four is magic. |
|||
1024: 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. |
|||
1234: One thousand two centenas thirty-four is thirty-seven, thirty-seven is twelve, twelve is six, six is three, three is five, five is four, four is magic. |
|||
12345: Twelve thousand three centenas forty-five is forty-one, forty-one is nine, nine is four, four is magic. |
|||
123456: One centenas twenty-three thousand four centenas fifty-six is fifty-eight, fifty-eight is eleven, eleven is six, six is three, three is five, five is four, four is magic. |
|||
1010101: One million ten thousand one centenas one is forty-one, forty-one is nine, nine is four, four is magic. |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |