N'th: Difference between revisions
m
syntax highlighting fixup automation
Puppydrum64 (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20:
{{trans|Python}}
<
F nth(n)
Line 26:
L(j) (0..1000).step(250)
print_elements(Array(j.+25).map(i -> nth(i)))</
{{out}}
Line 39:
=={{header|68000 Assembly}}==
The function itself:
<
MOVEQ #1,D1
.loop:
Line 97:
rd:
dc.b "rd",255
even</
And the test cases (each was executed in separate builds of the Sega Genesis cartridge to have enough room to see them all at once)
<
MOVE.W #25,D7
JSR Nth
Line 112:
JSR Nth
jmp * ;stop the cpu - we're done.</
{{out}}
Line 120:
=={{header|8080 Assembly}}==
<
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 196:
dcr b ; Decrement counter
jnz printnums ; If not zero, print next number
ret</
{{out}}
Line 212:
{{trans|8080 Assembly}}
<
cpu 8086
segment .text
Line 269:
dec bl ; Are we done yet?
jnz printn
ret</
{{out}}
Line 283:
=={{header|Action!}}==
<
CHAR ARRAY sfx
BYTE d
Line 321:
PutE() PutE()
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/N'th.png Screenshot from Atari 8-bit computer]
Line 334:
=={{header|Ada}}==
<
procedure Nth is
Line 359:
Print_Images( 250, 265);
Print_Images(1000, 1025);
end Nth;</
{{Out}}
Line 368:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.6.win32}}
<
PROC nth = ( INT number )STRING:
BEGIN
Line 416:
test nth( 1000, 1025 )
)</
{{out}}
<pre>
Line 437:
{{works with|Dyalog APL}}
<
sfx←4 2⍴'stndrdth'
tens←(10<100|⍵)∧20>100|⍵
(⍕⍵),sfx[(4×tens)⌈(⍳3)⍳10|⍵;]
}</
{{out}}
Line 459:
=={{header|AppleScript}}==
<
-- ordinalString :: Int -> String
Line 540:
end |λ|
end script
end uncurry</
{{Out}}
<pre>{{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th",
Line 553:
=={{header|Applesoft BASIC}}==
<
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT
Line 571:
260 IF NOT OP THEN NTH$ = "'" + NTH$
270 NTH$ = STR$(N) + NTH$
280 RETURN</
{{Out}}
<pre>0'TH 1'ST 2'ND 3'RD 4'TH 5'TH 6'TH 7'TH 8'TH 9'TH 10'TH 11'TH 12'TH 13'TH 14'TH 15'TH 16'TH 17'TH 18'TH 19'TH 20'TH 21'ST 22'ND 23'RD 24'TH 25'TH 250'TH 251'ST 252'ND 253'RD 254'TH 255'TH 256'TH 257'TH 258'TH 259'TH 260'TH 261'ST 262'ND 263'RD 264'TH 265'TH 1000'TH 1001'ST 1002'ND 1003'RD 1004'TH 1005'TH 1006'TH 1007'TH 1008'TH 1009'TH 1010'TH 1011'TH 1012'TH 1013'TH 1014'TH 1015'TH 1016'TH 1017'TH 1018'TH 1019'TH 1020'TH 1021'ST 1022'ND 1023'RD 1024'TH 1025'TH</pre>
=={{header|Arturo}}==
<
nth: function [n][
Line 589:
prints (nth i)++" "
print ""
]</
{{out}}
Line 601:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
while v[1] <= v[2] {
Out .= Ordinal(v[1]) " "
Line 616:
s1 := Mod(n, 10)
return n (s1 = 1 ? "st" : s1 = 2 ? "nd" : s1 = 3 ? "rd" : "th")
}</
{{Out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 623:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NTH.AWK
BEGIN {
Line 656:
return(nthday)
}
</syntaxhighlight>
{{out}}
<pre>
Line 666:
=={{header|Babel}}==
<
(main
Line 693:
. . }
{ -> %d "'th" . }
ifte }))</
{{out}}
Line 707:
=={{header|BaCon}}==
<
FUNCTION nth$(NUMBER n) TYPE STRING
LOCAL suffix
Line 734:
NEXT
NEXT
DATA 0, 25, 250, 265, 1000, 1025, -20, -11</
{{out}}
Line 750:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
function ordinal(n)
ns$ = string(n)
Line 779:
call imprimeOrdinal (1000, 1025)
end
</syntaxhighlight>
==={{header|Commodore BASIC}}===
{{trans|Applesoft BASIC}}
{{works with|Commodore BASIC|2.0}}
<
0 REM ROSETTACODE.ORG
1 REM N'TH
Line 813:
270 NTH$ = STR$(N) + NTH$
280 RETURN
</syntaxhighlight>
{{out}}
Commodore 64 (40 column text)
Line 837:
{{trans|Ada}}
{{works with|Nascom ROM BASIC|4.7}}
<
10 REM N'th
20 LOLIM=0
Line 865:
1540 SUF$="th"
1550 RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 874:
==={{header|QBasic}}===
<
DECLARE FUNCTION sufijo$ (n%)
DECLARE SUB imprimeOrdinal (loLim%, hiLim%)
Line 911:
END IF
END FUNCTION
</syntaxhighlight>
=={{header|Batch File}}==
<
::Main thing...
call :Nth 0 25
Line 934:
)
echo."!range_output:~1!"
goto :EOF</
{{Out}}
<pre>"0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11st 12nd 13rd 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th"
Line 943:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
PROCNth( 250, 265)
PROCNth(1000,1025)
Line 958:
NEXT
PRINT
ENDPROC</
{{out}}
Line 967:
=={{header|BCPL}}==
<
// Generate ASCII string of number n with ordinal suffix
Line 1,006:
for i = 250 to 265 do writef("%S*N", nth(i, buf))
for i = 1000 to 1025 do writef("%S*N", nth(i, buf))
$)</
{{out}}
<pre style="height:50ex">0th
Line 1,079:
The bottom section of code contains the "subroutine" that calculates the ordinal of a given number; the top section generates the list of values to test.
<
#v$#$<^:\+*8"}"_
>35*:>1-\:0\`!v
Line 1,090:
htdd >$>:#,_$:vg
v"d"\*!`3:%+55<9
>%55+/1-!!*:8g,^</
{{out}}
Line 1,096:
=={{header|BQN}}==
<
sfx ← ⟨"th","st","nd","rd"⟩
(•Fmt 𝕩)∾sfx⊑˜ (1≠10|⌊𝕩÷10)×(3≥10|𝕩)×10|𝕩
}
∘‿4⥊ Nth¨ (↕26) ∾ (250+↕16) ∾ (1000+↕26)</
{{out}}
<pre>┌─
Line 1,123:
┘</pre>
=={{header|C}}==
<
char* addSuffix(int num, char* buf, size_t len)
Line 1,174:
return 0;
}</
Another method with dynamic memory allocation
<
#include <stdio.h>
Line 1,208:
print_range(250, 265);
print_range(1000, 1025);
}</
{{out}}
<pre>Set [0,25] :
Line 1,219:
=={{header|C sharp|C#}}==
{{trans|Ruby}}
<
using System.Linq;
Line 1,250:
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize)));
}
}</
Dotnet 5 version without LINQ
<
static string Ordinalize(int i)
Line 1,273:
PrintRange(0, 25);
PrintRange(250, 265);
PrintRange(1000, 1025);</
{{Out}}
Line 1,281:
=={{header|C++}}==
<
#include <iostream>
Line 1,321:
return 0;
}</
{{out}}
<pre>Set [0,25] :
Line 1,331:
=={{header|Clojure}}==
<
(defn n-th [n]
(str n
Line 1,346:
(apply str (interpose " " (map n-th (range 250 266))))
(apply str (interpose " " (map n-th (range 1000 1026))))
</syntaxhighlight>
{{out}}
<pre>
Line 1,357:
Alternatively, if you want to print the full ordinal English, it becomes trivial with pprint:
<
(apply str (interpose " " (map #(clojure.pprint/cl-format nil "~:R" %) (range 0 26))))
</syntaxhighlight>
{{out}}
<pre>
Line 1,366:
=={{header|CLU}}==
<
num: string := int$unparse(n)
sfx: array[string] := array[string]$[0: "th", "st", "nd", "rd"]
Line 1,396:
do_range(250,265)
do_range(1000,1025)
end start_up</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th
Line 1,411:
=={{header|COBOL}}==
COBOL stores numbers in decimal form, so there is no need to use a modulo function: the last digit or the last two digits can be extracted directly.
<
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
Line 1,441:
IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.</
Output:
<pre> 0TH 1ST 2ND 3RD 4TH 5TH 6TH 7TH 8TH 9TH 10TH 11TH 12TH 13TH 14TH 15TH 16TH 17TH 18TH 19TH 20TH 21ST 22ND 23RD 24TH 25TH 250TH 251ST 252ND 253RD 254TH 255TH 256TH 257TH 258TH 259TH 260TH 261ST 262ND 263RD 264TH 265TH 1000TH 1001ST 1002ND 1003RD 1004TH 1005TH 1006TH 1007TH 1008TH 1009TH 1010TH 1011TH 1012TH 1013TH 1014TH 1015TH 1016TH 1017TH 1018TH 1019TH 1020TH 1021ST 1022ND 1023RD 1024TH 1025TH</pre>
=={{header|Common Lisp}}==
<
(let* ((suffixes #10("th" "st" "nd" "rd" "th"))
(last2 (mod number 100))
Line 1,454:
(svref suffixes last-digit))))
(format nil "~a~a" number suffix)))
</syntaxhighlight>
A more concise, albeit less readable version:
<
(format nil "~d'~:[~[th~;st~;nd~;rd~:;th~]~;th~]" n (< (mod (- n 10) 100) 10) (mod n 10)))
</syntaxhighlight>
Display the results:
<
do (progn
(format t "~a to ~a: " low high)
(loop for n from low to high
do (format t "~a " (add-suffix n))
finally (terpri))))</
{{Out}}
Line 1,477:
=={{header|Cowgol}}==
<
include "strings.coh";
Line 1,516:
test(0, 25);
test(250,265);
test(1000,1025);</
{{out}}
Line 1,531:
=={{header|Crystal}}==
{{trans|Ruby}}
<
def ordinalize
num = self.abs
Line 1,549:
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map{ |n| n.ordinalize }.join(", "); puts}
</syntaxhighlight>
{{out}}
<pre>
Line 1,561:
=={{header|D}}==
{{trans|Python}}
<
string nth(in uint n) pure {
Line 1,572:
foreach (r; [iota(26), iota(250, 266), iota(1000, 1026)])
writefln("%-(%s %)", r.map!nth);
}</
{{out}}
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th
Line 1,582:
=={{header|Draco}}==
<
channel output text ch;
open(ch, buf);
Line 1,613:
print_range(250, 265);
print_range(1000, 1025)
corp</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th
Line 1,627:
{{trans|C#}}
ELENA 5.0 :
<
import system'math;
import system'routines;
Line 1,655:
console.printLine(new Range(250,26).selectBy(mssgconst ordinalize<op>[1]));
console.printLine(new Range(1000,26).selectBy(mssgconst ordinalize<op>[1]))
}</
{{out}}
<pre>
Line 1,664:
=={{header|Elixir}}==
<
def ordinalize(n) do
num = abs(n)
Line 1,683:
Enum.each([0..25, 250..265, 1000..1025], fn range ->
Enum.map(range, fn n -> RC.ordinalize(n) end) |> Enum.join(" ") |> IO.puts
end)</
{{out}}
Line 1,693:
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM NTH_SOLVE
Line 1,717:
NTH(1000,1025)
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Line 1,730:
=={{header|F_Sharp|F#}}==
<
let ordinalsuffix n =
Line 1,745:
[1000..1025] |> show
0
</syntaxhighlight>
{{out}}
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 1,752:
=={{header|Factor}}==
<
sequences ;
IN: rosetta-code.nth
Line 1,765:
[ [ n'th write bl ] each nl ] tri@ ;
MAIN: n'th-demo</
{{out}}
<pre>
Line 1,774:
=={{header|Forth}}==
<
: .nth ( n -- )
dup 10 20 within if 0 .r ." th " exit then
Line 1,783:
26 0 test 266 250 test 1026 1000 test ;
tests</
{{out}}
Line 1,814:
Please find the compilation instructions and examples in comments at the start of the source.
<
!Compilation started at Fri Jun 6 15:40:18
!
Line 1,904:
end function ordinate
end program nth</
=={{header|FreeBASIC}}==
<
' Apostrophes NOT used as incorrect English
Line 1,945:
Print "Press any key to quit"
Sleep</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 1,956:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=6d60749ae886a37f128e75cffc6c7118 Click this link to run this code]'''
<
Dim siNums As Short[] = [0, 25, 250, 265, 1000, 1025]
Dim siCount, siNumbers As Short
Line 1,972:
Next
End </
Output:
<pre>
Line 1,984:
=={{header|Go}}==
{{trans|Raku}}
<
import "fmt"
Line 2,020:
}
fmt.Println()
}</
{{out}}
<pre>
Line 2,031:
{{trans|Ada}}
{{works with|PC-BASIC|any}}
<
10 ' N'th
20 LET LOLIM% = 0
Line 2,060:
1800 LET SUF$ = "th"
2000 RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 2,069:
=={{header|Haskell}}==
<
ordSuffs :: Array Integer String
Line 2,087:
printOrdSuffs [ 0.. 25]
printOrdSuffs [ 250.. 265]
printOrdSuffs [1000..1025]</
{{out}}
<pre>
Line 2,098:
The following works in both languages.
<
every writes(" ",nth(0 to 25) | "\n")
every writes(" ",nth(250 to 265) | "\n")
Line 2,108:
(n%10 = 2, n%100 ~= 12, "nd") |
(n%10 = 3, n%100 ~= 13, "rd") | "th")
end</
{{out}}
Line 2,123:
Implementation:
<
nth=: [: ;:inv (": , suf)each</
Task:
<
nth 0 thru 25
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 2,134:
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
nth 1000 thru 1025
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</
=={{header|Java}}==
<
public static String ordinalAbbrev(int n){
String ans = "th"; //most of the time it should be "th"
Line 2,162:
}
}
}</
{{works with|Java|8+}}
<
import java.util.stream.IntStream;
Line 2,211:
;
}
}</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 2,221:
===ES5===
<
var lstSuffix = 'th st nd rd th th th th th th'.split(' '),
Line 2,244:
}).join('\n\n');
}());</
{{Out}}
<
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</
===ES6===
<
'use strict'
Line 2,283:
);
})([[0, 25], [250, 265], [1000, 1025]]);</
Line 2,298:
=={{header|jq}}==
<syntaxhighlight lang="jq">
# ordinalize an integer input, positive or negative
def ordinalize:
Line 2,316:
([range(-5; -1)], [range(0;26)], [range(250;266)], [range(1000;1026)])
| map(ordinalize)
</syntaxhighlight>
{{out}}
<pre>
Line 2,326:
=={{header|Julia}}==
<syntaxhighlight lang
'''Function''':
<
n < 0 && throw(DomainError())
suffixes = ("st", "nd", "rd")
Line 2,339:
end
return string(n, suf)
end</
'''Main''':
<
for (i, n) in enumerate(0:25)
(i - 1) % 10 == 0 && println()
Line 2,358:
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end</
'''Main2''':
<
NthA(x::Integer) = "$(x)'$(Nth(x)) "
[0:25..., 250:265..., 1000:1025...] .|> NthA .|> print;</
{{out}}
Line 2,379:
=={{header|Kotlin}}==
<
if (this % 100 / 10 == 1) "th"
else when (this % 10) { 1 -> "st" 2 -> "nd" 3 -> "rd" else -> "th" }
Line 2,387:
fun main(args: Array<String>) {
listOf((0..25), (250..265), (1000..1025)).forEach { println(it.ordinalAbbrev()) }
}</
=={{header|Lambdatalk}}==
Translation from the javascript entry
<
{def fnOrdinalForm
{lambda {:n}
Line 2,411:
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th
1021st 1022nd 1023rd 1024th 1025th
</syntaxhighlight>
=={{header|Liberty BASIC}}==
{{trans|Ada}}
{{works with|Just BASIC|any}}
<syntaxhighlight lang="lb">
call printImages 0, 25
call printImages 250, 265
Line 2,449:
end if
end function
</syntaxhighlight>
{{out}}
<pre>
Line 2,459:
=={{header|Lua}}==
The apostrophe just looks weird if you ask me. No one did, obviously.
<
local lastTwo, lastOne = n % 100, n % 10
if lastTwo > 3 and lastTwo < 21 then return "th" end
Line 2,470:
function Nth (n) return n .. "'" .. getSuffix(n) end
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) end</
{{out}}
Line 2,503:
=={{header|Maple}}==
<
if 1 <= n and n <= 10 then
if n >= 4 then
Line 2,527:
end do;
printf("\n\n");
end do;</
{{out}}
<pre>
Line 2,539:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
I borrowed the logic from the Python code.
<
addsuffix[n_] := Module[{suffix},
suffix = Which[
Line 2,550:
addsuffix[#] & /@ Range[0, 25] (* test 1 *)
addsuffix[#] & /@ Range[250, 265] (* test 2 *)
addsuffix[#] & /@ Range[1000, 1025] (* test 3 *)</
{{out}}
<pre>{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"}
Line 2,559:
=={{header|MATLAB}}==
<
tens = mod(n, 100);
if tens > 9 && tens < 20
Line 2,576:
end
s = sprintf('%d%s', n, suf);
end</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 2,585:
=={{header|Microsoft Small Basic}}==
{{trans|Ada}}
<
loLim = 0
hiLim = 25
Line 2,618:
EndIf
EndSub
</syntaxhighlight>
=={{header|MiniScript}}==
To get the output all on one line, we append it to a list as we go, and then print the list all at once at the end.
<
if n > 3 and n < 20 then return n + "th"
if n % 10 == 1 then return n + "st"
Line 2,641:
test 250, 265
test 1000, 1025
print out.join</
{{out}}
Line 2,649:
{{trans|Ada}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<
MODULE Nth;
Line 2,695:
PrintImages(1000, 1025);
END Nth.
</syntaxhighlight>
=={{header|Nanoquery}}==
{{trans|Java}}
<
if int(n % 100 / 10) = 1
return "th"
Line 2,728:
print (i + "'" + ordinalAbbrev(i) + " ")
end
println</
{{out}}
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th
Line 2,736:
=={{header|Nim}}==
{{trans|Python}}
<
proc nth(n: Natural): string =
Line 2,744:
for i in j..j+24:
stdout.write nth(i), " "
echo ""</
{{out}}
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th
Line 2,754:
=={{header|Objeck}}==
{{trans|Java}}
<
function : OrdinalAbbrev(n : Int ) ~ String {
ans := "th"; # most of the time it should be "th"
Line 2,788:
};
}
}</
{{output}}
Line 2,799:
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">
let show_nth n =
if (n mod 10 = 1) && (n mod 100 <> 11) then "st"
Line 2,815:
List.iter show_ordinals [ (0,25); (250,265); (1000,1025) ]
</syntaxhighlight>
{{out}}
Line 2,826:
=={{header|Oforth}}==
<
| r |
n "th" over 10 mod ->r
Line 2,832:
r 2 == ifTrue: [ n 100 mod 12 == ifFalse: [ drop "nd" ] ]
r 3 == ifTrue: [ n 100 mod 13 == ifFalse: [ drop "rd" ] ]
+ ;</
{{out}}
Line 2,853:
(Spurious apostrophes intentionally omitted, following Raku.)
<
apply(ordinal, [0..25])
apply(ordinal, [250..265])
apply(ordinal, [1000..1025])
apply(ordinal, [111, 1012])</
{{out}}
<pre>%1 = ["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"]
Line 2,866:
=={{header|Pascal}}==
nearly copy of [[N'th#Ada|Ada]]
<
function Suffix(N: NativeInt):AnsiString;
Line 2,898:
Print_Images( 250, 265);
Print_Images(1000, 1025);
end.</
{{Out}} shortened
<pre>
Line 2,909:
{{Trans|Raku}}
Requires Perl 5.10 or newer for the Defined OR operator (//).
<
my %irregulars = ( 1 => 'st',
2 => 'nd',
Line 2,924:
sub range { join ' ', map { nth($_) } @{$_[0]} }
print range($_), "\n" for ([0..25], [250..265], [1000..1025]);</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 2,932:
Alternatively, can use a library.
{{libheader|Lingua::EN::Numbers::Ordinate}}
<
foreach my $i (0..25, 250..265, 1000..1025) {
print ordinate($i),"\n";
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">ordinals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"th"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"st"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rd"</span><span style="color: #0000FF;">}</span>
Line 2,955:
<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: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 2,970:
</pre>
Alternatively you can use the builtin ord(), and a more functional style, as follows (same output)
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">do_one</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d%s%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
Line 2,979:
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">re</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">265</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1025</span><span style="color: #0000FF;">}})</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">do_set</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">re</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">}),{</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">}})</span>
<!--</
=={{header|PHP}}==
<
$os = "th";
if ($num % 100 <= 10 or $num % 100 > 20) {
Line 3,006:
}
echo "\n";
}</
{{out}}
<pre>
Line 3,018:
===Prolog style===
{{trans|Prolog}}
<
( tween(N) -> Th = "th"
; 1 = N mod 10 -> Th = "st"
Line 3,024:
; 3 = N mod 10 -> Th = "rd"
; Th = "th" ).
tween(N) => Tween = N mod 100, between(11, 13, Tween).</
===Function with explicit conditions===
<
nth3(N) = cc(N,"st"), N mod 10 = 1 => true.
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true.
Line 3,033:
nth3(N) = cc(N,"th") => true.
% helper function
cc(N,Th) = N.to_string() ++ Th.</
===List of suffixes===
{{trans|Python}}
<
Suffix = ["th","st","nd","rd","th","th","th","th","th","th"],
Nth = N.to_string() ++ cond((N mod 100 <= 10; N mod 100 > 20), Suffix[1 + N mod 10], "th").</
===Test===
<
Ranges = [ 0..25, 250..265, 1000..1025],
foreach(Range in Ranges) println([nth2(I) : I in Range])
end,
nl.</
{{out}}
Line 3,054:
=={{header|PicoLisp}}==
<
(mapcar
'((I)
Line 3,071:
(prinl (glue " " (rangeth 1000 1025)))
(bye)</
{{out}}
<pre>
Line 3,080:
=={{header|PL/I}}==
<syntaxhighlight lang="text">Nth: procedure options (main); /* 1 June 2014 */
declare i fixed (10);
Line 3,105:
end enth;
end Nth;</
{{out}}
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th
Line 3,118:
=={{header|PowerShell}}==
<
$suffix = "th"
Line 3,131:
0..25 | %{Write-host -nonewline (nth "$_")};""
250..265 | %{Write-host -nonewline (nth "$_")};""
1000..1025 | %{Write-host -nonewline (nth "$_")};""</
{{Out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11st 12nd 13rd 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 3,139:
===An Alternate Version===
This is, I think, is a more "PowerShelly" way:
<syntaxhighlight lang="powershell">
function Get-Nth ([int]$Number)
{
Line 3,157:
251..265 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
1001..1025 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
</syntaxhighlight>
{{Out}}
<pre>
Line 3,184:
Following Icon:
<
( tween(N) -> Th = "th"
; 1 is N mod 10 -> Th = "st"
Line 3,200:
nl, nl,
forall( between(1000,1025,N), (nth(N, N_Th), format('~w, ', N_Th)) ).
</syntaxhighlight>
{{out}} of `test/0`:
Line 3,214:
=={{header|PureBasic}}==
<
Select n%10
Case 1 : If n%100<>11 : ProcedureReturn "st" : EndIf
Line 3,232:
put(250,265)
put(1000,1025)
Input()</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 3,239:
=={{header|Python}}==
<
def nth(n):
Line 3,246:
if __name__ == '__main__':
for j in range(0,1001, 250):
print(' '.join(nth(i) for i in list(range(j, j+25))))</
{{out}}
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th
Line 3,254:
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th</pre>
'''Alternate version'''
<
def ord(n):
Line 3,268:
print(*(ord(n) for n in range(26)))
print(*(ord(n) for n in range(250,266)))
print(*(ord(n) for n in range(1000,1026)))</
{{out}}
<pre>
Line 3,282:
=={{header|Quackery}}==
<
$ "th st nd rd th th th th th th"
Line 3,305:
250 265 test
cr
1000 1025 test</
{{out}}
Line 3,324:
{{trans|Python}}
Note that R vectors are 1-indexed.
<
{
if (length(n) > 1) return(sapply(n, nth))
Line 3,341:
range <- list(0:25, 250:275, 500:525, 750:775, 1000:1025)
sapply(range, nth)</
{{out}}
<pre> [,1] [,2] [,3] [,4] [,5]
Line 3,374:
=={{header|Racket}}==
<
(define (teen? n) (<= 11 (modulo n 100) 19))
(define (Nth n)
Line 3,383:
(for ((range (list (in-range 26) (in-range 250 266) (in-range 1000 1026))))
(displayln (string-join (for/list ((nth (sequence-map Nth range))) nth) " ")))</
{{out}}
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th
Line 3,392:
(formerly Perl 6)
(Spurious apostrophes intentionally omitted.)
<syntaxhighlight lang="raku"
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) }
say .list».&nth for [^26], [250..265], [1000..1025];</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
Line 3,402:
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre>
If you want to get Unicodally fancy, use this version instead:
<syntaxhighlight lang="raku"
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ᵗʰ' ) }
say .list».&nth for [^26], [250..265], [1000..1025];</
{{out}}
<blockquote>0ᵗʰ 1ˢᵗ 2ⁿᵈ 3ʳᵈ 4ᵗʰ 5ᵗʰ 6ᵗʰ 7ᵗʰ 8ᵗʰ 9ᵗʰ 10ᵗʰ 11ᵗʰ 12ᵗʰ 13ᵗʰ 14ᵗʰ 15ᵗʰ 16ᵗʰ 17ᵗʰ 18ᵗʰ 19ᵗʰ 20ᵗʰ 21ˢᵗ 22ⁿᵈ 23ʳᵈ 24ᵗʰ 25ᵗʰ<br>
Line 3,413:
=={{header|Red}}==
<
nth: function [n][
Line 3,430:
test 0 25
test 250 265
test 1000 1025</
{{out}}
<pre>
Line 3,442:
Negative numbers and fractions are also handled.
<
call tell 0, 25 /*display the 1st range of numbers. */
call tell 250, 265 /* " " 2nd " " " */
Line 3,454:
return /*return to invoker. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: parse arg z; x=abs(z); return z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</
'''output''' using the default inputs:
<pre>
Line 3,470:
=={{header|Ring}}==
<
for nr = 0 to 25
see Nth(nr) + Nth(nr + 250) + Nth(nr + 1000) + nl
Line 3,486:
func Nth n
return "" + n + "'" + getSuffix(n) + " "
</syntaxhighlight>
=={{header|Ruby}}==
Code (slightly adapted) and methodname taken from ActiveSupport (Ruby on Rails).
<
def ordinalize
num = self.abs
Line 3,508:
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map(&:ordinalize).join(", "); puts}
</syntaxhighlight>
{{out}}
<pre>
Line 3,519:
=={{header|Rust}}==
<
format!("{}{}", num, match (num % 10, num % 100) {
(1, 11) | (2, 12) | (3, 13) => "th",
Line 3,538:
println!();
}
}</
{{out}}
Line 3,550:
=={{header|Scala}}==
{{libheader|Scala}}<
def abbrevNumber(i: Int) = print(s"$i${ordinalAbbrev(i)} ")
Line 3,569:
println();
(1000 to 1025).foreach(abbrevNumber)
}</
=={{header|Seed7}}==
<
const func string: suffix (in integer: num) is func
Line 3,600:
printImages( 250, 265);
printImages(1000, 1025);
end func;</
{{out}}
Line 3,611:
=={{header|Set lang}}==
Due to the language's specification, the input can only contain one character. Therefore, the following code only works with 0-9.
<
set t 50
set h 51
Line 3,631:
set ! R
set ! D
> EOF</
Input: I, Output: O
<pre>I: 1, O: 1'ST
Line 3,642:
=={{header|Sidef}}==
{{trans|Raku}}
<
static irregulars = Hash(<1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ 11 ᵗʰ 12 ᵗʰ 13 ᵗʰ>...)
n.to_s + (irregulars{n % 100} \\ irregulars{n % 10} \\ 'ᵗʰ')
Line 3,649:
for r in [0..25, 250..265, 1000..1025] {
say r.map {|n| nth(n) }.join(" ")
}</
{{out}}
<pre>
Line 3,659:
=={{header|Sinclair ZX81 BASIC}}==
Works flawlessly with 2k or more of RAM. With 1k, the subroutine itself works but you can't quite print all the tests: the program crashes with an 'out of memory' error code after 1017th. (A slightly less useful and readable version gets as far as 1023rd; 1025th is probably attainable, but might involve obfuscating the program more than is appropriate for this site.)
<
20 GOSUB 160
30 PRINT N$;" ";
Line 3,682:
220 IF N$(LEN N$)="3" THEN LET S$="RD"
230 LET N$=N$+S$
240 RETURN</
{{out}}
<pre>
Line 3,691:
=={{header|SQL}}==
Oracle
<syntaxhighlight lang="sql">
select level card,
to_char(to_date(level,'j'),'fmjth') ord
Line 3,699:
select to_char(to_date(5373485,'j'),'fmjth')
from dual;
</syntaxhighlight>
<pre>
CARD ORD
Line 3,728:
=={{header|Standard ML}}==
<
val v = Vector.tabulate (10, fn 1 => "st" | 2 => "nd" | 3 => "rd" | _ => "th")
fun getSuffix x =
Line 3,739:
(* some test ouput *)
val () = (print o concat o List.tabulate)
(26, fn i => String.concatWith "\t" (map nth [i, i + 250, i + 1000]) ^ "\n")</
{{out}}
<pre>0th 250th 1000th
Line 3,771:
We reuse here the '''maps''' function defined in the task [[Apply a callback to an array]].
<
function maps(f,a) {
nr = rows(a)
Line 3,788:
maps(&nth(),((0::25),(250::275),(1000::1025)))
end</
'''Output:'''
Line 3,823:
=={{header|Swift}}==
<
if n % 100 / 10 == 1 {
return "th"
Line 3,851:
print("\(i)\(addSuffix(i)) ")
}
println()</
{{out}}
<pre>
Line 3,860:
=={{header|Tcl}}==
<
if {$n%100<10 || $n%100>20} {
set suff [lindex {th st nd rd th th th th th th} [expr {$n % 10}]]
Line 3,874:
}
puts $l
}</
{{out}}
<pre>
Line 3,884:
=={{header|True BASIC}}==
<
SUB sufijo (n)
LET n = INT(n)
Line 3,920:
CALL imprimeOrdinal (1000, 1025)
END
</syntaxhighlight>
== {{header|TypeScript}} ==
{{trans|Ada}}
<
// N'th
function suffix(n: number): string {
Line 3,948:
printImages( 250, 265);
printImages(1000, 1025);
</syntaxhighlight>
{{out}}
<pre>
Line 3,957:
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">For x = 0 to 25 ' Test range 0..25
Push x : GoSub _PrintOrdinal
Next x : Print
Line 3,991:
110 Print "st"; : Return
120 Print "nd"; : Return
130 Print "rd"; : Return</
{{out}}
<pre>
Line 4,001:
=={{header|UNIX Shell}}==
{{works with|Bourne Again Shell}}
<
local ordinals=(th st nd rd)
local -i n=$1 i
Line 4,016:
for n in {0..25} {250..265} {1000..1025}; do
nth $n
done | column</
{{Out}}
Line 4,028:
=={{header|VBA}}==
{{trans|Phix}}<
ordinals = [{"th","st","nd","rd"}]
End Function
Line 4,047:
Debug.Print
Next i
End Sub</
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th
Line 4,061:
=={{header|Vlang}}==
{{trans|go}}
<
mut s := "th"
c := n % 10
Line 4,097:
}
println('')
}</
{{out}}
Line 4,108:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var ranges = [ 0..25, 250..265, 1000..1025 ]
Line 4,114:
r.each { |i| System.write("%(Conv.ord(i)) ") }
System.print("\n")
}</
{{out}}
Line 4,128:
{{trans|Ada}}
{{works with|Windows XBasic}}
<
PROGRAM "nth"
VERSION "0.0002"
Line 4,164:
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Line 4,173:
=={{header|XLISP}}==
<
(COND
((AND (> (MOD N 100) 3) (< (MOD N 100) 21)) `(,N TH))
Line 4,192:
(DISPLAY (MAPCAR NTH (RANGE 1000 1025))))
(TEST-NTH)</
{{out}}
<pre>((1 ST) (2 ND) (3 RD) (4 TH) (5 TH) (6 TH) (7 TH) (8 TH) (9 TH) (10 TH) (11 TH) (12 TH) (13 TH) (14 TH) (15 TH) (16 TH) (17 TH) (18 TH) (19 TH) (20 TH) (21 ST) (22 ND) (23 RD) (24 TH) (25 TH))
Line 4,201:
{{trans|Ada}}
{{works with|EXPL-32}}
<
\N'th
code Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12;
Line 4,238:
PrintImages(1000, 1025)
end
</syntaxhighlight>
{{out}}
<pre>
Line 4,248:
=={{header|Yabasic}}==
{{trans|Liberty BASIC}}
<
sub ordinal$ (n)
NMod10 = mod(n, 10)
Line 4,278:
imprimeOrdinal (1000, 1025)
end
</syntaxhighlight>
=={{header|zkl}}==
Two versions, your choice
<
fcn addSuffix(n){
z:=n.abs()%100;
Line 4,296:
String(n,(z<=10 or z>20) and suffixes[z%10] or "th");
}
#endif</
<
[250..265] .apply(addSuffix).concat(",").println();
[1000..1025].apply(addSuffix).concat(",").println();</
{{out}}
<pre>
Line 4,308:
=={{header|ZX Spectrum Basic}}==
<
20 GO SUB 140
30 PRINT n$;" ";
Line 4,329:
200 IF n$(LEN n$)="3" THEN LET s$="rd"
210 LET n$=n$+s$
220 RETURN</
{{out}}
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre>
|