Four is the number of letters in the ...: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 9 users not shown)
Line 14:
:*   '''twenty─three'''   is considered one word   (which is hyphenated).
:* &nbsp; no &nbsp; <big>''' ''and'' '''</big> &nbsp; words are to be used when spelling a (English) word for a number.
:* &nbsp; The Americanshort versionscale ofnumbering numberssystem will(i.e. be2,000,000,000 usedis heretwo inbillion) thiswill taskbe &nbsp;used (ashere. opposed[[wp:Long toand theshort British version).scales]]
 
'''2,000,000,000''' &nbsp; is two billion, &nbsp; ''not'' &nbsp; two milliard.
 
 
;Task:
:* &nbsp; Write a driver (invoking routine) and a function (subroutine/routine···) that returns the sequence (for any positive integer) of the number of letters in the first &nbsp; '''N''' &nbsp; words in the never─ending sentence. &nbsp; For instance, the portion of the never─ending sentence shown above (2<sup>nd</sup> sentence of this task's preamble), &nbsp; the sequence would be:
'''4 2 3 6 2 7'''
:* &nbsp; Only construct as much as is needed for the never─ending sentence.
Line 52 ⟶ 53:
=={{header|C}}==
{{libheader|GLib}}
<langsyntaxhighlight lang="c">#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
Line 254 ⟶ 255:
word_list_destroy(&result);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 277 ⟶ 278:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cctype>
#include <cstdint>
#include <iomanip>
Line 433 ⟶ 434:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 463 ⟶ 464:
[[Spelling_of_ordinal_numbers#Go|Spelling of ordinal numbers]] task
(omitted from this listing).
<langsyntaxhighlight Golang="go">package main
 
import (
Line 556 ⟶ 557:
// omitted from this listing
// ...
</syntaxhighlight>
</lang>
{{out}}
<pre>The lengths of the first 201 words are:
Line 575 ⟶ 576:
Word 10000000 is "thousand", with 8 letters. Length of sentence so far: 70995756
</pre>
 
=={{header|Haskell}}==
 
Uses the solution [[Spelling_of_ordinal_numbers#Haskell]] and tying-the-knot technique to create the infinite sentence.
 
<syntaxhighlight lang="haskell">import Data.Char
 
sentence = start ++ foldMap add (zip [2..] $ tail $ words sentence)
where
start = "Four is the number of letters in the first word of this sentence, "
add (i, w) = unwords [spellInteger (alphaLength w), "in the", spellOrdinal i ++ ", "]
 
alphaLength w = fromIntegral $ length $ filter isAlpha w
 
main = mapM_ (putStrLn . say) [1000,10000,100000,1000000]
where
ws = words sentence
say n =
let (a, w:_) = splitAt (n-1) ws
in "The " ++ spellOrdinal n ++ " word is \"" ++ w ++ "\" which has " ++
spellInteger (alphaLength w) ++ " letters. The sentence length is " ++
show (length $ unwords a) ++ " chars."</syntaxhighlight>
 
<pre>λ> take 200 sentence
"Four is the number of letters in the first word of this sentence, two in the second, three in the third, six in the fourth, two in the fifth, seven in the sixth, two in the seventh, three in the eight"
 
λ> main
The one thousandth word is "in" which has two letters. The sentence length is 6349 chars.
The ten thousandth word is "in" which has two letters. The sentence length is 66051 chars.
The one hundred thousandth word is "one" which has three letters. The sentence length is 683690 chars.
The one millionth word is "the" which has three letters. The sentence length is 7349567 chars.</pre>
 
=={{header|Java}}==
Take care with the requirements. As noted, numberToString(23) = twenty-three. Therefore numberToString(723423) = seven hundred twenty-three thousand four hundred twenty-three (note the two "-").
<br/>The discussion was helpful by providing the first 2202 words of the sentence.
<langsyntaxhighlight lang="java">
import java.util.HashMap;
import java.util.Map;
Line 793 ⟶ 825:
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 815 ⟶ 847:
 
=={{header|Julia}}==
The functions num2text and numtext2ordinal are from the "Spelling of ordinal numbers" and "Number names" tasks, updated for Julia 1.0 and to remove the "and" words.<langsyntaxhighlight lang="julia">using DataStructures # for deque
 
const seed = "Four is the number of letters in the first word of this sentence, "
Line 888 ⟶ 920:
println("$n words -> $itercount iterations, $totalletters letters total, ",
"last word \"$lastword\" with $(length(lastword)) letters.")
end</langsyntaxhighlight> {{output}} <pre>
It is interesting how identical lengths align with 20 columns.
1: 4 2 3 6 2 7 2 3 5 4 2 4 8 3 2 3 6 5 2 3
Line 911 ⟶ 943:
=={{header|Kotlin}}==
This pulls in (slightly adjusted) code from related tasks to convert numbers to text or ordinals.
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
val names = mapOf(
Line 1,086 ⟶ 1,118:
}
while (n <= 10_000_000)
}</langsyntaxhighlight>
 
{{out}}
Line 1,119 ⟶ 1,151:
Length of sentence = 70995756
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">(*==Number names==*)
 
(*Mathematica has a built-in function for getting the name of an integer. It's a semantically rich function (dealing with many languages and grammatical variants), and consequently it would slow down our algorithm significantly. So, I've used the built-in function to seed/memoize special-purpose functions. Furthermore, the problem is suited to using a representation of the sentence that is an array of strings rather than a single monolithic string, and so these integer name functions will return arrays of strings.*)
 
(*We'll define the function for small integers to use the built-in function and to trigger memoization on the first invocation. After that, the basic strategy is to chunk up an integer into groups of three digits, apply the name to what those digits usually represent and then add the 'scaling' term (e.g. 'thousand', 'million'). Just for laziness, I'll skip trying to handle the 'thousand and zero' case and just fall back to the built-in function--it shouldn't be called often enough to matter. Since this problem won't need number names exceeding the 'million' scale, I won't optimize beyond that.*)
IntNameWords[n_]:=(IntNameWords[n]=StringSplit[IntegerName[n,"Words"]])/;n<1000;
IntNameWords[n_]:=StringSplit[IntegerName[n,"Words"]]/;Divisible[n,1000];
IntNameWords[n_]:=Flatten[Riffle[IntNameWords/@QuotientRemainder[n,1000],"thousand"]]/;n<1000000;
IntNameWords[n_]:=Flatten[Riffle[IntNameWords/@QuotientRemainder[n,1000000],"million"]]/;n<1000000000;
IntNameWords[n_]:=StringSplit[IntegerName[n,"Words"]];
(*I'm using Scan to trigger the memoization.*)
Scan[IntNameWords,Range[999]];
 
(*The strategy is similar for ordinals. Note that I'm tacking on a comma to the ordinals. This makes this function quite specialized to this specific problem.*)
OrdNameWords[n_]:=(OrdNameWords[n]=StringSplit[IntegerName[n,"Ordinal"]<>","])/;n<1000;
OrdNameWords[n_]:=StringSplit[IntegerName[n,"Ordinal"]<>","]/;Divisible[n,1000];
OrdNameWords[n_]:=Flatten[Riffle[Construct@@@Thread[{{IntNameWords,OrdNameWords},QuotientRemainder[n,1000]}],"thousand"]]/;n<1000000;
OrdNameWords[n_]:=Flatten[Riffle[Construct@@@Thread[{{IntNameWords,OrdNameWords},QuotientRemainder[n,1000000]}],"million"]]/;n<1000000000;
OrdNameWords[n_]:=StringSplit[IntegerName[n,"Ordinal"]<>","];
(*Triggering memoization again.*)
Scan[OrdNameWords,Range[999]];
 
 
(*==Helper/driver functions==*)
 
(*This could be generalized, but for this problem, the '-' and ',' are the only non-letter characters we need to worry about.*)
LetterCount[str_]:=StringLength[StringDelete[str,"-"|","]];
 
(*The seed/initial part of the sentence.*)
SentenceHeadWords=StringSplit["Four is the number of letters in the first word of this sentence,"];
 
(*Output formatters*)
DisplayWordLengthSequence[wordSeq_]:=StringRiffle[{"First "<>StringRiffle[IntNameWords[Length@wordSeq]]<>" numbers in sequence:",LetterCount/@wordSeq},"\n"];
DisplayCharacterCount[wordSeq_]:=StringRiffle[{"String length of sentence with "<>StringRiffle[IntNameWords[Length@wordSeq]]<>" words:",SentenceCharacterCount[wordSeq]}];
DisplayWordInfo[wordSeq_,wordIdx_]:=StringForm["The `` word is '``' consisting of `` letters.",StringRiffle[OrdNameWords[Length@wordSeq]],wordSeq[[wordIdx]],StringRiffle[IntNameWords[StringLength@wordSeq[[wordIdx]]]]];
 
(*There is a space between each 'word', so we can just add 1 less than the number of 'words' to get total characters in the full string representation of the sentence (if we were to create it). I could also subract another 1 for the trailing comma, but the requirements weren't precise in this regard.*)
SentenceCharacterCount[chunks:{__String}]:=Total[StringLength[chunks]]+Length[chunks]-1;
 
(*==Implementation A==*)
 
(*A simple functional implementation that continues to extend the 'sentence' one fragment at a time until the number of words exceeds the requested number. This implementation takes several seconds to complete the 100,000 word case.*)
ExtendCharChunks[{0,0,{}}]={1,Length[SentenceHeadWords],SentenceHeadWords};
ExtendCharChunks[{fragCt_,wordCt_,chunks_}]:=
With[
{nextFrag=Flatten[{IntNameWords[LetterCount[chunks[[1+fragCt]]]],"in","the",OrdNameWords[1+fragCt]}]},
{1+fragCt,wordCt+Length[nextFrag],Flatten[{chunks,nextFrag}]}
];
SentenceChunksFun[chunkCt_]:=Take[Last[NestWhile[ExtendCharChunks,ExtendCharChunks[{0,0,{}}],#[[2]]<chunkCt&]],chunkCt];
 
 
(*==Implementation B==*)
 
(*This implementation uses a pre-allocated array, an iterative strategy, and inlining of the fragment construction. It performs much better than the previous implementation but still takes about 20 seconds for the 10 million word case. One could try compiling the function for greater performance.*)
SentenceChunksArray[targetCount_]:=
Block[
{
chunks=ConstantArray["",targetCount],
wordIdx=0,
fragmentIdx=0
},
Scan[(chunks[[++wordIdx]]=#)&,SentenceHeadWords];
++fragmentIdx;
While[
(*Since each new fragment is longer than one word, it is likely that we will try to insert more words into the array than it has been allocated to hold. This generates and error message, but does not otherwise interfere with processing (the insertion simply fails). I could include more checks, but it didn't seem necessary for this task.*)
wordIdx<targetCount,
Scan[(chunks[[++wordIdx]]=#)&,{Splice[IntNameWords[LetterCount[chunks[[++fragmentIdx]]]]],"in","the",Splice[OrdNameWords[fragmentIdx]]}]
];
chunks
];
 
 
(*==Output==*)
 
StringRiffle[
{
DisplayWordLengthSequence[SentenceChunksArray[201]],
DisplayCharacterCount[SentenceChunksArray[201]],
DisplayWordInfo[SentenceChunksArray[1000],1000],
DisplayWordInfo[SentenceChunksArray[10000],10000],
DisplayWordInfo[SentenceChunksArray[100000],100000],
DisplayWordInfo[SentenceChunksArray[1000000],1000000],
DisplayWordInfo[SentenceChunksArray[10000000],10000000]
},
"\n\n"
]</syntaxhighlight>
 
{{out}}
<pre>First two hundred one numbers in sequence:
{4, 2, 3, 6, 2, 7, 2, 3, 5, 4, 2, 4, 8, 3, 2, 3, 6, 5, 2, 3, 5, 3, 2, 3, 6, 3, 2, 3, 5, 5, 2, 3, 5, 3, 2, 3, 7, 5, 2, 3, 6, 4, 2, 3, 5, 4, 2, 3, 5, 3, 2, 3, 8, 4, 2, 3, 7, 5, 2, 3, 10, 5, 2, 3, 10, 3, 2, 3, 9, 5, 2, 3, 9, 3, 2, 3, 11, 4, 2, 3, 10, 3, 2, 3, 10, 5, 2, 3, 9, 4, 2, 3, 11, 5, 2, 3, 12, 3, 2, 3, 11, 5, 2, 3, 12, 3, 2, 3, 11, 5, 2, 3, 11, 3, 2, 3, 13, 5, 2, 3, 12, 4, 2, 3, 11, 4, 2, 3, 9, 3, 2, 3, 11, 5, 2, 3, 12, 4, 2, 3, 11, 5, 2, 3, 12, 3, 2, 3, 11, 5, 2, 3, 11, 5, 2, 3, 13, 4, 2, 3, 12, 3, 2, 3, 11, 5, 2, 3, 8, 3, 2, 3, 10, 4, 2, 3, 11, 3, 2, 3, 10, 5, 2, 3, 11, 4, 2, 3, 10, 4, 2, 3, 10, 3, 2, 3, 12, 5, 2, 3, 11}
 
String length of sentence with two hundred one words: 1203
 
The one thousandth, word is 'in' consisting of two letters.
 
The ten thousandth, word is 'in' consisting of two letters.
 
The one hundred thousandth, word is 'one' consisting of three letters.
 
The one millionth, word is 'the' consisting of three letters.
 
The ten millionth, word is 'thousand' consisting of eight letters.</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, strformat, tables
 
####################################################################################################
Line 1,248 ⟶ 1,384:
break
 
if valcount == 0: stdout.write ($letterCount).align(fmt"{i+1:>3)}:"
stdout.write fmt"{letterCount:>3}"
inc valcount
inc length, word.len + 1 # +1 for space.
Line 1,257 ⟶ 1,394:
valcount = 0
 
echo fmt"SentenceLength lengthof sentence: {length}"
 
#---------------------------------------------------------------------------------------------------
Line 1,271 ⟶ 1,408:
if i == idx:
dec length # Adjust space count.
let w = word.strip(leading = false, chars = {','}) # Remove trailing ',' if needed.
echo fmt"Word {pos} is ""{word}"" with {word.count(Letters)} letters."
echo fmt"LengthWord of{pos} sequence:is ""{lengthw}"" with {w.count(Letters)} letters."
echo fmt"Length of sentence: {length}"
break
 
Line 1,280 ⟶ 1,418:
for n in [1_000, 10_000, 100_000, 1_000_000, 10_000_000]:
echo ""
displayWord(n)</langsyntaxhighlight>
 
{{out}}
<pre>Number of letters in first 201 words in the sequence:
1: 4 2 3 6 2 7 2 3 5 4 2 4
13: 8 3 2 3 6 5 2 3 5 3 2 3
25: 6 3 2 3 5 5 2 3 5 3 2 3
37: 7 5 2 3 6 4 2 3 5 4 2 3
49: 5 3 2 3 8 4 2 3 7 5 2 3
61: 10 5 2 3 10 3 2 3 9 5 2 3
73: 9 3 2 3 11 4 2 3 10 3 2 3
85: 10 5 2 3 9 4 2 3 11 5 2 3
97: 12 3 2 3 11 5 2 3 12 3 2 3
109: 11 5 2 3 11 3 2 3 13 5 2 3
121: 12 4 2 3 11 4 2 3 9 3 2 3
133: 11 5 2 3 12 4 2 3 11 5 2 3
145: 12 3 2 3 11 5 2 3 11 5 2 3
157: 13 4 2 3 12 3 2 3 11 5 2 3
169: 8 3 2 3 10 4 2 3 11 3 2 3
181: 10 5 2 3 11 4 2 3 10 4 2 3
193: 10 3 2 3 12 5 2 3 11
SentenceLength lengthof sentence: 1203
 
Word 1000 is "in" with 2 letters.
Length of sequencesentence: 6249
 
Word 10000 is "in" with 2 letters.
Length of sequencesentence: 64097
 
Word 100000 is "one" with 3 letters.
Length of sequencesentence: 659455
 
Word 1000000 is "the" with 3 letters.
Length of sequencesentence: 7113560
 
Word 10000000 is "thousand" with 8 letters.
Length of sequencesentence: 70995729</pre>
 
=={{header|Perl}}==
Uses <code>Lingua::EN::Numbers</code> module to generate number names. State variable in <tt>extend_to</tt> routine keeps track of last word tallied.
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use feature 'state';
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
 
Line 1,352 ⟶ 1,490:
ucfirst(num2en_ordinal($_)) . " word, '$sentence[$_-1]' has " . alpha($sentence[$_-1]) . " characters. \n" .
count($_) . "\n";
}</langsyntaxhighlight>
{{out}}
<pre>First 201 word lengths in the sequence:
Line 1,381 ⟶ 1,519:
=={{header|Phix}}==
Note that my version of [[Number_names]] includes "and" (and ","), that others do not, hence the kill_and()/grr below and the minor mismatch of sentence lengths.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include demo\rosetta\number_names.exw
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">demo</span><span style="color: #0000FF;">\</span><span style="color: #000000;">rosetta</span><span style="color: #0000FF;">\</span><span style="color: #000000;">number_names</span><span style="color: #0000FF;">.</span><span style="color: #000000;">exw</span> <span style="color: #000080;font-style:italic;">-- see note
-- as per Spelling_of_ordinal_numbers#Phix:
constant {irregs,ordinals} = columnize({{"one","first"},
-- as per Spelling_of_ordinal_numbers#Phix:</span>
{"two","second"},
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">irregs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ordinals</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: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"first"</span><span style="color: #0000FF;">},</span>
{"three","third"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"second"</span><span style="color: #0000FF;">},</span>
{"five","fifth"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"three"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"third"</span><span style="color: #0000FF;">},</span>
{"eight","eighth"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"five"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fifth"</span><span style="color: #0000FF;">},</span>
{"nine","ninth"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"eight"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eighth"</span><span style="color: #0000FF;">},</span>
{"twelve","twelfth"}})
<span style="color: #0000FF;">{</span><span style="color: #008000;">"nine"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ninth"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"twelve"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"twelfth"</span><span style="color: #0000FF;">}})</span>
function ordinal(string s)
integer i
<span style="color: #008080;">function</span> <span style="color: #000000;">ordinl</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=length(s) to 1 by -1 do
<span style="color: #004080;">integer</span> ch<span style="color: s[#000000;">i]</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
if ch=' ' or ch='-' then exit end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">or</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer k = find(s[i+1..$],irregs)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if k then
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$],</span><span style="color: #000000;">irregs</span><span style="color: #0000FF;">)</span>
s = s[1..i]&ordinals[k]
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
elsif s[$]='y' then
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">ordinals</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
s[$..$] = "ieth"
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[$]=</span><span style="color: #008000;">'y'</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">s</span><span style="color: #0000FF;">[$..$]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ieth"</span>
s &= "th"
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">"th"</span>
return s
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
--/copy of Spelling_of_ordinal_numers#Phix
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
<span style="color: #000080;font-style:italic;">--/copy of Spelling_of_ordinal_numers#Phix</span>
function countLetters(string s)
integer res = 0
<span style="color: #008080;">function</span> <span style="color: #000000;">count_letters</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
integer ch = s[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if (ch>='A' and ch<='Z')
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
or (ch>='a' and ch<='z') then
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'A'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'Z'</span><span style="color: #0000FF;">)</span>
res += 1
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'a'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'z'</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence words = split("Four is the number of letters in the first word of this sentence,")
integer fi = 1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Four is the number of letters in the first word of this sentence,"</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">fi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
function kill_and(sequence s)
--grr...
<span style="color: #008080;">function</span> <span style="color: #000000;">kill_and</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=length(s) to 1 by -1 do
<span style="color: #000080;font-style:italic;">--grr...</span>
if s[i] = "and" then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
s[i..i] = {}
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"and"</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return s
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function WordLen(integer w)
-- Returns the w'th word and its length (only counting letters).
<span style="color: #008080;">function</span> <span style="color: #000000;">word_len</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">)</span>
while length(words)<w do
<span style="color: #000080;font-style:italic;">-- Returns the w'th word and its length (only counting letters).</span>
fi += 1
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">w</span> <span style="color: #008080;">do</span>
integer n = countLetters(words[fi])
<span style="color: #000000;">fi</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
sequence ns = kill_and(split(spell(n)))
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">count_letters</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">])</span>
sequence os = kill_and(split(ordinal(spell(fi)) & ","))
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">kill_and</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span>
-- append eg {"two","in","the","second,"}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">os</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">kill_and</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ordinl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">","</span><span style="color: #0000FF;">))</span>
words &= ns&{"in","the"}&os
<span style="color: #000080;font-style:italic;">-- append eg {"two","in","the","second,"}</span>
end while
<span style="color: #000000;">words</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ns</span><span style="color: #0000FF;">&{</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"the"</span><span style="color: #0000FF;">}&</span><span style="color: #000000;">os</span>
string word = words[w]
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return {word, countLetters(word)}
<span style="color: #004080;">string</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">]</span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count_letters</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function TotalLength()
-- Returns the total number of characters (including blanks,
<span style="color: #008080;">function</span> <span style="color: #000000;">total_length</span><span style="color: #0000FF;">()</span>
-- commas, and punctuation) of the sentence so far constructed.
<span style="color: #000080;font-style:italic;">-- Returns the total number of characters (including blanks,
integer res = 0
-- commas, and punctuation) of the sentence so far constructed.</span>
for i=1 to length(words) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
res += length(words[i])+1
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])+</span><span style="color: #000000;">1</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure main()
integer i,n
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
string w
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The lengths of the first 201 words are:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">201</span> <span style="color: #008080;">do</span>
for i=1 to 201 do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if mod(i,25)==1 then
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n%3d: "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"\n%3d: ", i)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %2d"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">word_len</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
{?,n} = WordLen(i)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1," %2d", n)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nLength of sentence so far:%d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">total_length</span><span style="color: #0000FF;">())</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">5</span><span style="color: #0000FF;">:</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"\nLength of sentence so far:%d\n", TotalLength())
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
for p=3 to 7 do
<span style="color: #0000FF;">{</span><span style="color: #004080;">string</span> <span style="color: #000000;">w</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: #0000FF;">=</span> <span style="color: #000000;">word_len</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
i = power(10,p)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Word %8d is \"%s\", with %d letters."</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
{w, n} = WordLen(i)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Length of sentence so far:%d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">total_length</span><span style="color: #0000FF;">())</span>
printf(1,"Word %8d is \"%s\", with %d letters.", {i, w, n})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1," Length of sentence so far:%d\n", TotalLength())
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
end for
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
end procedure
<!--</syntaxhighlight>-->
main()</lang>
Note you will have to comment out main()..EOF in the number_names.exw file for pwa/p2js to accept it/not complain about multiple main() or #ilASM (which I may yet remove). Also as indicated 100K words is simply too much for your typical browser, let alone a million, so we limit the last part to 10K under pwa/p2js.
{{out}}
<pre>
Line 1,501 ⟶ 1,640:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
# Python implementation of Rosetta Code Task
# http://rosettacode.org/wiki/Four_is_the_number_of_letters_in_the_...
Line 1,686 ⟶ 1,825:
word_and_counts(1000000)
word_and_counts(10000000)
</syntaxhighlight>
</lang>
 
Output:
Line 1,718 ⟶ 1,857:
Uses the Lingua::EN::Numbers module to generate both cardinal and ordinal numbers. This module places commas in number words between 3-orders-of-magnitude clusters. E.G. <code>12345678.&ordinal</code> becomes: twelve million, three hundred forty-five thousand, six hundred seventy-eighth. Uses a custom 'no-commas' routine to filter them out for accurate character counts. Generates the 'sentence' lazily so only the words needed are ever calculated and reified.
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
no-commas(True);
 
Line 1,734 ⟶ 1,873:
for 1e3, 1e4, 1e5, 1e6, 1e7 {
say "{.&ordinal.tc} word, '{@sentence[$_ - 1]}', has {@sentence[$_ - 1].&alpha} characters. ", .&count
}</langsyntaxhighlight>
{{out}}
<pre>First 201 word lengths in the sequence:
Line 1,759 ⟶ 1,898:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds/shows the number of letters in the Nth word in a constructed sentence*/
@= 'Four is the number of letters in the first word of this sentence,' /*···*/
/* [↑] the start of a long sentence. */
Line 1,801 ⟶ 1,940:
 
if $\=='' & z>0 then say right(idx, w)'►'$ /*display if there are residual numbers*/
return</langsyntaxhighlight>
The &nbsp; '''$SPELL#.REX''' &nbsp; routine can be found here &nbsp; ───► &nbsp; [[$SPELL.REX|$SPELL#.REX]]. <br><br>
 
Line 1,833 ⟶ 1,972:
=={{header|Rust}}==
{{trans|C}}
<langsyntaxhighlight lang="rust">struct NumberNames {
cardinal: &'static str,
ordinal: &'static str,
Line 2,170 ⟶ 2,309:
n *= 10;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,190 ⟶ 2,329:
The 1000000th word is 'the' and has 3 letters. Sentence length: 7113621
The 10000000th word is 'thousand' and has 8 letters. Sentence length: 70995756
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var names = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
10: "ten",
11: "eleven",
12: "twelve",
13: "thirteen",
14: "fourteen",
15: "fifteen",
16: "sixteen",
17: "seventeen",
18: "eighteen",
19: "nineteen",
20: "twenty",
30: "thirty",
40: "forty",
50: "fifty",
60: "sixty",
70: "seventy",
80: "eighty",
90: "ninety"
}
 
var bigNames = {
1e3 : "thousand",
1e6 : "million",
1e9 : "billion",
1e12: "trillion",
1e15: "quadrillion"
}
 
var irregOrdinals = {
"one" : "first",
"two" : "second",
"three" : "third",
"five" : "fifth",
"eight" : "eighth",
"nine" : "ninth",
"twelve": "twelfth"
}
 
var strToOrd = Fn.new { |s|
if (s == "zero") return "zeroth" // or alternatively 'zeroeth'
var splits = s.replace("-", " ").split(" ")
var last = splits[-1]
return irregOrdinals.containsKey(last) ? s[0...-last.count] + irregOrdinals[last] :
last.endsWith("y") ? s[0...-1] + "ieth" : s + "th"
}
 
var numToText = Fn.new { |n, uk|
if (n == 0) return "zero"
var neg = n < 0
var nn = neg ? - n : n
var digits3 = List.filled(6, 0)
for (i in 0..5) { // split number into groups of 3 digits from the right
digits3[i] = nn % 1000
nn = (nn / 1000).truncate
}
 
var threeDigitsToText = Fn.new { |number|
var sb = ""
if (number == 0) return ""
var hundreds = (number / 100).truncate
var remainder = number % 100
if (hundreds > 0) {
sb = sb + names[hundreds] + " hundred"
if (remainder > 0) sb = sb + (uk ? " and " : " ")
}
if (remainder > 0) {
var tens = (remainder / 10).truncate
var units = remainder % 10
if (tens > 1) {
sb = sb + names[tens * 10]
if (units > 0) sb = sb + "-" + names[units]
} else {
sb = sb + names[remainder]
}
}
return sb
}
 
var strings = List.filled(6, 0)
for (i in 0..5) strings[i] = threeDigitsToText.call(digits3[i])
var text = strings[0]
var andNeeded = uk && 1 <= digits3[0] && digits3[0] <= 99
var big = 1000
for (i in 1..5) {
if (digits3[i] > 0) {
var text2 = strings[i] + " " + bigNames[big]
if (!text.isEmpty) {
text2 = text2 + (andNeeded ? " and " : " ") // no commas inserted in this version
andNeeded = false
} else {
andNeeded = uk && 1 <= digits3[i] && digits3[i] <= 99
}
text = text2 + text
}
big = big * 1000
}
if (neg) text = "minus " + text
return text
}
 
var opening = "Four is the number of letters in the first word of this sentence,".split(" ")
 
var adjustedLength = Fn.new { |s| s.replace(",", "").replace("-", "").count } // no ',' or '-'
 
var getWords = Fn.new { |n|
var words = []
words.addAll(opening)
if (n > opening.count) {
var k = 2
while (true) {
var len = adjustedLength.call(words[k - 1])
var text = numToText.call(len, false)
var splits = text.split(" ")
words.addAll(splits)
words.add("in")
words.add("the")
var text2 = strToOrd.call(numToText.call(k, false)) + "," // add trailing comma
var splits2 = text2.split(" ")
words.addAll(splits2)
if (words.count >= n) break
k = k + 1
}
}
return words
}
 
var getLengths = Fn.new { |n|
var words = getWords.call(n)
var lengths = words.take(n).map { |w| adjustedLength.call(w) }.toList
// includes hyphens, commas & spaces
var sentenceLength = words.reduce(0) { |acc, w| acc + w.count } + words.count - 1
return [lengths, sentenceLength]
}
 
var getLastWord = Fn.new { |n|
var words = getWords.call(n)
var nthWord = words[n - 1]
var nthWordLength = adjustedLength.call(nthWord)
// includes hyphens, commas & spaces
var sentenceLength = words.reduce(0) { |acc, w| acc + w.count } + words.count - 1
return [nthWord, nthWordLength, sentenceLength]
}
 
var n = 201
System.print("The lengths of the first %(n) words are:\n")
var res = getLengths.call(n)
var list = res[0]
var sentenceLength = res[1]
for (i in 0...n) {
if (i % 25 == 0) {
if (i > 0) System.print()
Fmt.write("$3d: ", i + 1)
}
Fmt.write("$3d", list[i])
}
Fmt.print("\n\nLength of sentence = $,d\n", sentenceLength)
 
n = 1000
while (true) {
var res = getLastWord.call(n)
var word = res[0]
var wLen = res[1]
var sLen = res[2]
if (word.endsWith(",")) word = word[0...-1] // strip off any trailing comma
Fmt.print("The length of word $,d [$s] is $d", n, word, wLen)
Fmt.print("Length of sentence = $,d\n", sLen)
n = n * 10
if (n > 1e7) break
}</syntaxhighlight>
 
{{out}}
<pre>
The lengths of the first 201 words are:
 
1: 4 2 3 6 2 7 2 3 5 4 2 4 8 3 2 3 6 5 2 3 5 3 2 3 6
26: 3 2 3 5 5 2 3 5 3 2 3 7 5 2 3 6 4 2 3 5 4 2 3 5 3
51: 2 3 8 4 2 3 7 5 2 3 10 5 2 3 10 3 2 3 9 5 2 3 9 3 2
76: 3 11 4 2 3 10 3 2 3 10 5 2 3 9 4 2 3 11 5 2 3 12 3 2 3
101: 11 5 2 3 12 3 2 3 11 5 2 3 11 3 2 3 13 5 2 3 12 4 2 3 11
126: 4 2 3 9 3 2 3 11 5 2 3 12 4 2 3 11 5 2 3 12 3 2 3 11 5
151: 2 3 11 5 2 3 13 4 2 3 12 3 2 3 11 5 2 3 8 3 2 3 10 4 2
176: 3 11 3 2 3 10 5 2 3 11 4 2 3 10 4 2 3 10 3 2 3 12 5 2 3
201: 11
 
Length of sentence = 1,203
 
The length of word 1,000 [in] is 2
Length of sentence = 6,279
 
The length of word 10,000 [in] is 2
Length of sentence = 64,140
 
The length of word 100,000 [one] is 3
Length of sentence = 659,474
 
The length of word 1,000,000 [the] is 3
Length of sentence = 7,113,621
 
The length of word 10,000,000 [thousand] is 8
Length of sentence = 70,995,756
</pre>
 
=={{header|zkl}}==
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]]
<langsyntaxhighlight lang="zkl"> // Built the sentence in little chucks but only save the last one
// Save the word counts
fcn fourIsThe(text,numWords){
Line 2,221 ⟶ 2,577:
return(lastWords.strip(),szs,total);
}
fcn lastWord(sentence){ sentence[sentence.rfind(" ")+1,*] }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">var seed="Four is the number of letters in the first word of this sentence, ";
sentence,szs,total := fourIsThe(seed,201);
print(" 1:");
Line 2,229 ⟶ 2,585:
if(0 == n%25) print("\n%3d:".fmt(n+1));
}
println("\nLength of above sentence: ",total);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,243 ⟶ 2,599:
Length of above sentence: 1203
</pre>
<langsyntaxhighlight lang="zkl">n:=1000; do(5){
sentence,x,total := fourIsThe(seed,n);
word:=lastWord(sentence);
Line 2,249 ⟶ 2,605:
.fmt(n,word,word.len(),total));
n*=10;
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits