How to resolve the algorithm N'th step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm N'th step by step in the Prolog programming language
Table of Contents
Problem Statement
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.
Returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th
Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs: 0..25, 250..265, 1000..1025
Note: apostrophes are now optional to allow correct apostrophe-less English.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm N'th step by step in the Prolog programming language
Source code in the prolog programming language
nth(N, N_Th) :-
( tween(N) -> Th = "th"
; 1 is N mod 10 -> Th = "st"
; 2 is N mod 10 -> Th = "nd"
; 3 is N mod 10 -> Th = "rd"
; Th = "th" ),
string_concat(N, Th, N_Th).
tween(N) :- Tween is N mod 100, between(11, 13, Tween).
test :-
forall( between(0,25, N), (nth(N, N_Th), format('~w, ', N_Th)) ),
nl, nl,
forall( between(250,265,N), (nth(N, N_Th), format('~w, ', N_Th)) ),
nl, nl,
forall( between(1000,1025,N), (nth(N, N_Th), format('~w, ', N_Th)) ).
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Oforth programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Batch File programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Gamma function step by step in the Phix programming language