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

m
m (→‎{{header|Wren}}: Minor tidy)
 
(36 intermediate revisions by 13 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 43 ⟶ 44:
:* &nbsp; [[Self-referential sequence]]
:* &nbsp; [[Spelling of ordinal numbers]]
 
 
;Also see:
:* &nbsp; See the OEIS sequence [[oeis:A072425| A72425 "Four is the number of letters..."]].
:* &nbsp; See the OEIS sequence [[oeis:A072424| A72424 "Five's the number of letters..."]]
<br><br>
 
=={{header|C}}==
{{libheader|GLib}}
<syntaxhighlight lang="c">#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <glib.h>
 
typedef uint64_t integer;
 
typedef struct number_names_tag {
const char* cardinal;
const char* ordinal;
} number_names;
 
const number_names small[] = {
{ "zero", "zeroth" }, { "one", "first" }, { "two", "second" },
{ "three", "third" }, { "four", "fourth" }, { "five", "fifth" },
{ "six", "sixth" }, { "seven", "seventh" }, { "eight", "eighth" },
{ "nine", "ninth" }, { "ten", "tenth" }, { "eleven", "eleventh" },
{ "twelve", "twelfth" }, { "thirteen", "thirteenth" },
{ "fourteen", "fourteenth" }, { "fifteen", "fifteenth" },
{ "sixteen", "sixteenth" }, { "seventeen", "seventeenth" },
{ "eighteen", "eighteenth" }, { "nineteen", "nineteenth" }
};
 
const number_names tens[] = {
{ "twenty", "twentieth" }, { "thirty", "thirtieth" },
{ "forty", "fortieth" }, { "fifty", "fiftieth" },
{ "sixty", "sixtieth" }, { "seventy", "seventieth" },
{ "eighty", "eightieth" }, { "ninety", "ninetieth" }
};
 
typedef struct named_number_tag {
const char* cardinal;
const char* ordinal;
integer number;
} named_number;
 
const named_number named_numbers[] = {
{ "hundred", "hundredth", 100 },
{ "thousand", "thousandth", 1000 },
{ "million", "millionth", 1000000 },
{ "billion", "biliionth", 1000000000 },
{ "trillion", "trillionth", 1000000000000 },
{ "quadrillion", "quadrillionth", 1000000000000000ULL },
{ "quintillion", "quintillionth", 1000000000000000000ULL }
};
 
const char* get_small_name(const number_names* n, bool ordinal) {
return ordinal ? n->ordinal : n->cardinal;
}
 
const char* get_big_name(const named_number* n, bool ordinal) {
return ordinal ? n->ordinal : n->cardinal;
}
 
const named_number* get_named_number(integer n) {
const size_t names_len = sizeof(named_numbers)/sizeof(named_numbers[0]);
for (size_t i = 0; i + 1 < names_len; ++i) {
if (n < named_numbers[i + 1].number)
return &named_numbers[i];
}
return &named_numbers[names_len - 1];
}
 
typedef struct word_tag {
size_t offset;
size_t length;
} word_t;
 
typedef struct word_list_tag {
GArray* words;
GString* str;
} word_list;
 
void word_list_create(word_list* words) {
words->words = g_array_new(FALSE, FALSE, sizeof(word_t));
words->str = g_string_new(NULL);
}
 
void word_list_destroy(word_list* words) {
g_string_free(words->str, TRUE);
g_array_free(words->words, TRUE);
}
 
void word_list_clear(word_list* words) {
g_string_truncate(words->str, 0);
g_array_set_size(words->words, 0);
}
 
void word_list_append(word_list* words, const char* str) {
size_t offset = words->str->len;
size_t len = strlen(str);
g_string_append_len(words->str, str, len);
word_t word;
word.offset = offset;
word.length = len;
g_array_append_val(words->words, word);
}
 
word_t* word_list_get(word_list* words, size_t index) {
return &g_array_index(words->words, word_t, index);
}
 
void word_list_extend(word_list* words, const char* str) {
word_t* word = word_list_get(words, words->words->len - 1);
size_t len = strlen(str);
word->length += len;
g_string_append_len(words->str, str, len);
}
 
size_t append_number_name(word_list* words, integer n, bool ordinal) {
size_t count = 0;
if (n < 20) {
word_list_append(words, get_small_name(&small[n], ordinal));
count = 1;
} else if (n < 100) {
if (n % 10 == 0) {
word_list_append(words, get_small_name(&tens[n/10 - 2], ordinal));
} else {
word_list_append(words, get_small_name(&tens[n/10 - 2], false));
word_list_extend(words, "-");
word_list_extend(words, get_small_name(&small[n % 10], ordinal));
}
count = 1;
} else {
const named_number* num = get_named_number(n);
integer p = num->number;
count += append_number_name(words, n/p, false);
if (n % p == 0) {
word_list_append(words, get_big_name(num, ordinal));
++count;
} else {
word_list_append(words, get_big_name(num, false));
++count;
count += append_number_name(words, n % p, ordinal);
}
}
return count;
}
 
size_t count_letters(word_list* words, size_t index) {
const word_t* word = word_list_get(words, index);
size_t letters = 0;
const char* s = words->str->str + word->offset;
for (size_t i = 0, n = word->length; i < n; ++i) {
if (isalpha((unsigned char)s[i]))
++letters;
}
return letters;
}
 
void sentence(word_list* result, size_t count) {
static const char* words[] = {
"Four", "is", "the", "number", "of", "letters", "in", "the",
"first", "word", "of", "this", "sentence,"
};
word_list_clear(result);
size_t n = sizeof(words)/sizeof(words[0]);
for (size_t i = 0; i < n; ++i)
word_list_append(result, words[i]);
for (size_t i = 1; count > n; ++i) {
n += append_number_name(result, count_letters(result, i), false);
word_list_append(result, "in");
word_list_append(result, "the");
n += 2;
n += append_number_name(result, i + 1, true);
// Append a comma to the final word
word_list_extend(result, ",");
}
}
 
size_t sentence_length(const word_list* words) {
size_t n = words->words->len;
if (n == 0)
return 0;
return words->str->len + n - 1;
}
 
int main() {
setlocale(LC_ALL, "");
size_t n = 201;
word_list result = { 0 };
word_list_create(&result);
sentence(&result, n);
printf("Number of letters in first %'lu words in the sequence:\n", n);
for (size_t i = 0; i < n; ++i) {
if (i != 0)
printf("%c", i % 25 == 0 ? '\n' : ' ');
printf("%'2lu", count_letters(&result, i));
}
printf("\nSentence length: %'lu\n", sentence_length(&result));
for (n = 1000; n <= 10000000; n *= 10) {
sentence(&result, n);
const word_t* word = word_list_get(&result, n - 1);
const char* s = result.str->str + word->offset;
printf("The %'luth word is '%.*s' and has %lu letters. ", n,
(int)word->length, s, count_letters(&result, n - 1));
printf("Sentence length: %'lu\n" , sentence_length(&result));
}
word_list_destroy(&result);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Number of letters in first 201 words in the 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
Sentence length: 1,203
The 1,000th word is 'in' and has 2 letters. Sentence length: 6,279
The 10,000th word is 'in' and has 2 letters. Sentence length: 64,140
The 100,000th word is 'one' and has 3 letters. Sentence length: 659,474
The 1,000,000th word is 'the' and has 3 letters. Sentence length: 7,113,621
The 10,000,000th word is 'thousand' and has 8 letters. Sentence length: 70,995,756
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <cctype>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
 
struct number_names {
const char* cardinal;
const char* ordinal;
};
 
const number_names small[] = {
{ "zero", "zeroth" }, { "one", "first" }, { "two", "second" },
{ "three", "third" }, { "four", "fourth" }, { "five", "fifth" },
{ "six", "sixth" }, { "seven", "seventh" }, { "eight", "eighth" },
{ "nine", "ninth" }, { "ten", "tenth" }, { "eleven", "eleventh" },
{ "twelve", "twelfth" }, { "thirteen", "thirteenth" },
{ "fourteen", "fourteenth" }, { "fifteen", "fifteenth" },
{ "sixteen", "sixteenth" }, { "seventeen", "seventeenth" },
{ "eighteen", "eighteenth" }, { "nineteen", "nineteenth" }
};
 
const number_names tens[] = {
{ "twenty", "twentieth" }, { "thirty", "thirtieth" },
{ "forty", "fortieth" }, { "fifty", "fiftieth" },
{ "sixty", "sixtieth" }, { "seventy", "seventieth" },
{ "eighty", "eightieth" }, { "ninety", "ninetieth" }
};
 
struct named_number {
const char* cardinal;
const char* ordinal;
uint64_t number;
};
 
const named_number named_numbers[] = {
{ "hundred", "hundredth", 100 },
{ "thousand", "thousandth", 1000 },
{ "million", "millionth", 1000000 },
{ "billion", "biliionth", 1000000000 },
{ "trillion", "trillionth", 1000000000000 },
{ "quadrillion", "quadrillionth", 1000000000000000ULL },
{ "quintillion", "quintillionth", 1000000000000000000ULL }
};
 
const char* get_name(const number_names& n, bool ordinal) {
return ordinal ? n.ordinal : n.cardinal;
}
 
const char* get_name(const named_number& n, bool ordinal) {
return ordinal ? n.ordinal : n.cardinal;
}
 
const named_number& get_named_number(uint64_t n) {
constexpr size_t names_len = std::size(named_numbers);
for (size_t i = 0; i + 1 < names_len; ++i) {
if (n < named_numbers[i + 1].number)
return named_numbers[i];
}
return named_numbers[names_len - 1];
}
 
size_t append_number_name(std::vector<std::string>& result, uint64_t n, bool ordinal) {
size_t count = 0;
if (n < 20) {
result.push_back(get_name(small[n], ordinal));
count = 1;
}
else if (n < 100) {
if (n % 10 == 0) {
result.push_back(get_name(tens[n/10 - 2], ordinal));
} else {
std::string name(get_name(tens[n/10 - 2], false));
name += "-";
name += get_name(small[n % 10], ordinal);
result.push_back(name);
}
count = 1;
} else {
const named_number& num = get_named_number(n);
uint64_t p = num.number;
count += append_number_name(result, n/p, false);
if (n % p == 0) {
result.push_back(get_name(num, ordinal));
++count;
} else {
result.push_back(get_name(num, false));
++count;
count += append_number_name(result, n % p, ordinal);
}
}
return count;
}
 
size_t count_letters(const std::string& str) {
size_t letters = 0;
for (size_t i = 0, n = str.size(); i < n; ++i) {
if (isalpha(static_cast<unsigned char>(str[i])))
++letters;
}
return letters;
}
 
std::vector<std::string> sentence(size_t count) {
static const char* words[] = {
"Four", "is", "the", "number", "of", "letters", "in", "the",
"first", "word", "of", "this", "sentence,"
};
std::vector<std::string> result;
result.reserve(count + 10);
size_t n = std::size(words);
for (size_t i = 0; i < n && i < count; ++i) {
result.push_back(words[i]);
}
for (size_t i = 1; count > n; ++i) {
n += append_number_name(result, count_letters(result[i]), false);
result.push_back("in");
result.push_back("the");
n += 2;
n += append_number_name(result, i + 1, true);
result.back() += ',';
}
return result;
}
 
size_t sentence_length(const std::vector<std::string>& words) {
size_t n = words.size();
if (n == 0)
return 0;
size_t length = n - 1;
for (size_t i = 0; i < n; ++i)
length += words[i].size();
return length;
}
 
int main() {
std::cout.imbue(std::locale(""));
size_t n = 201;
auto result = sentence(n);
std::cout << "Number of letters in first " << n << " words in the sequence:\n";
for (size_t i = 0; i < n; ++i) {
if (i != 0)
std::cout << (i % 25 == 0 ? '\n' : ' ');
std::cout << std::setw(2) << count_letters(result[i]);
}
std::cout << '\n';
std::cout << "Sentence length: " << sentence_length(result) << '\n';
for (n = 1000; n <= 10000000; n *= 10) {
result = sentence(n);
const std::string& word = result[n - 1];
std::cout << "The " << n << "th word is '" << word << "' and has "
<< count_letters(word) << " letters. ";
std::cout << "Sentence length: " << sentence_length(result) << '\n';
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Number of letters in first 201 words in the 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
Sentence length: 1,203
The 1,000th word is 'in' and has 2 letters. Sentence length: 6,279
The 10,000th word is 'in' and has 2 letters. Sentence length: 64,140
The 100,000th word is 'one' and has 3 letters. Sentence length: 659,474
The 1,000,000th word is 'the' and has 3 letters. Sentence length: 7,113,621
The 10,000,000th word is 'thousand' and has 8 letters. Sentence length: 70,995,756
</pre>
 
=={{header|Go}}==
Line 53 ⟶ 464:
[[Spelling_of_ordinal_numbers#Go|Spelling of ordinal numbers]] task
(omitted from this listing).
<langsyntaxhighlight Golang="go">package main
 
import (
Line 146 ⟶ 557:
// omitted from this listing
// ...
</syntaxhighlight>
</lang>
{{out}}
<pre>The lengths of the first 201 words are:
Line 164 ⟶ 575:
Word 1000000 is "the", with 3 letters. Length of sentence so far: 7113621
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.
<syntaxhighlight lang="java">
import java.util.HashMap;
import java.util.Map;
 
public class FourIsTheNumberOfLetters {
 
public static void main(String[] args) {
String [] words = neverEndingSentence(201);
System.out.printf("Display the first 201 numbers in the sequence:%n%3d: ", 1);
for ( int i = 0 ; i < words.length ; i++ ) {
System.out.printf("%2d ", numberOfLetters(words[i]));
if ( (i+1) % 25 == 0 ) {
System.out.printf("%n%3d: ", i+2);
}
}
System.out.printf("%nTotal number of characters in the sentence is %d%n", characterCount(words));
for ( int i = 3 ; i <= 7 ; i++ ) {
int index = (int) Math.pow(10, i);
words = neverEndingSentence(index);
String last = words[words.length-1].replace(",", "");
System.out.printf("Number of letters of the %s word is %d. The word is \"%s\". The sentence length is %,d characters.%n", toOrdinal(index), numberOfLetters(last), last, characterCount(words));
}
}
@SuppressWarnings("unused")
private static void displaySentence(String[] words, int lineLength) {
int currentLength = 0;
for ( String word : words ) {
if ( word.length() + currentLength > lineLength ) {
String first = word.substring(0, lineLength-currentLength);
String second = word.substring(lineLength-currentLength);
System.out.println(first);
System.out.print(second);
currentLength = second.length();
}
else {
System.out.print(word);
currentLength += word.length();
}
if ( currentLength == lineLength ) {
System.out.println();
currentLength = 0;
}
System.out.print(" ");
currentLength++;
if ( currentLength == lineLength ) {
System.out.println();
currentLength = 0;
}
}
System.out.println();
}
private static int numberOfLetters(String word) {
return word.replace(",","").replace("-","").length();
}
private static long characterCount(String[] words) {
int characterCount = 0;
for ( int i = 0 ; i < words.length ; i++ ) {
characterCount += words[i].length() + 1;
}
// Extra space counted in last loop iteration
characterCount--;
return characterCount;
}
private static String[] startSentence = new String[] {"Four", "is", "the", "number", "of", "letters", "in", "the", "first", "word", "of", "this", "sentence,"};
private static String[] neverEndingSentence(int wordCount) {
String[] words = new String[wordCount];
int index;
for ( index = 0 ; index < startSentence.length && index < wordCount ; index++ ) {
words[index] = startSentence[index];
}
int sentencePosition = 1;
while ( index < wordCount ) {
// X in the Y
// X
sentencePosition++;
String word = words[sentencePosition-1];
for ( String wordLoop : numToString(numberOfLetters(word)).split(" ") ) {
words[index] = wordLoop;
index++;
if ( index == wordCount ) {
break;
}
}
// in
words[index] = "in";
index++;
if ( index == wordCount ) {
break;
}
// the
words[index] = "the";
index++;
if ( index == wordCount ) {
break;
}
// Y
for ( String wordLoop : (toOrdinal(sentencePosition) + ",").split(" ") ) {
words[index] = wordLoop;
index++;
if ( index == wordCount ) {
break;
}
}
}
return words;
}
private static final String[] nums = new String[] {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
private static final String[] tens = new String[] {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
 
private static final String numToString(long n) {
return numToStringHelper(n);
}
private static final String numToStringHelper(long n) {
if ( n < 0 ) {
return "negative " + numToStringHelper(-n);
}
int index = (int) n;
if ( n <= 19 ) {
return nums[index];
}
if ( n <= 99 ) {
return tens[index/10] + (n % 10 > 0 ? "-" + numToStringHelper(n % 10) : "");
}
String label = null;
long factor = 0;
if ( n <= 999 ) {
label = "hundred";
factor = 100;
}
else if ( n <= 999999) {
label = "thousand";
factor = 1000;
}
else if ( n <= 999999999) {
label = "million";
factor = 1000000;
}
else if ( n <= 999999999999L) {
label = "billion";
factor = 1000000000;
}
else if ( n <= 999999999999999L) {
label = "trillion";
factor = 1000000000000L;
}
else if ( n <= 999999999999999999L) {
label = "quadrillion";
factor = 1000000000000000L;
}
else {
label = "quintillion";
factor = 1000000000000000000L;
}
return numToStringHelper(n / factor) + " " + label + (n % factor > 0 ? " " + numToStringHelper(n % factor ) : "");
}
 
private static Map<String,String> ordinalMap = new HashMap<>();
static {
ordinalMap.put("one", "first");
ordinalMap.put("two", "second");
ordinalMap.put("three", "third");
ordinalMap.put("five", "fifth");
ordinalMap.put("eight", "eighth");
ordinalMap.put("nine", "ninth");
ordinalMap.put("twelve", "twelfth");
}
private static String toOrdinal(long n) {
String spelling = numToString(n);
String[] split = spelling.split(" ");
String last = split[split.length - 1];
String replace = "";
if ( last.contains("-") ) {
String[] lastSplit = last.split("-");
String lastWithDash = lastSplit[1];
String lastReplace = "";
if ( ordinalMap.containsKey(lastWithDash) ) {
lastReplace = ordinalMap.get(lastWithDash);
}
else if ( lastWithDash.endsWith("y") ) {
lastReplace = lastWithDash.substring(0, lastWithDash.length() - 1) + "ieth";
}
else {
lastReplace = lastWithDash + "th";
}
replace = lastSplit[0] + "-" + lastReplace;
}
else {
if ( ordinalMap.containsKey(last) ) {
replace = ordinalMap.get(last);
}
else if ( last.endsWith("y") ) {
replace = last.substring(0, last.length() - 1) + "ieth";
}
else {
replace = last + "th";
}
}
split[split.length - 1] = replace;
return String.join(" ", split);
}
}
</syntaxhighlight>
{{out}}
<pre>
Display the first 201 numbers in the sequence:
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
Total number of characters in the sentence is 1203
Number of letters of the one thousandth word is 2. The word is "in". The sentence length is 6,249 characters.
Number of letters of the ten thousandth word is 2. The word is "in". The sentence length is 64,097 characters.
Number of letters of the one hundred thousandth word is 3. The word is "one". The sentence length is 659,455 characters.
Number of letters of the one millionth word is 3. The word is "the". The sentence length is 7,113,560 characters.
Number of letters of the ten millionth word is 8. The word is "thousand". The sentence length is 70,995,729 characters.
</pre>
 
=={{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 210 ⟶ 890:
if itercount % 20 == 0
print("\n", lpad(itercount+1, 4), ":")
elseif itercount =>= n
break
end
Line 240 ⟶ 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 263 ⟶ 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 438 ⟶ 1,118:
}
while (n <= 10_000_000)
}</langsyntaxhighlight>
 
{{out}}
Line 471 ⟶ 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}}==
<syntaxhighlight lang="nim">import strutils, strformat, tables
 
####################################################################################################
# Cardinal and ordinal strings.
 
const
 
Small = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
 
Tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
 
Illions = ["", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion"]
 
IrregularOrdinals = {"one": "first", "two": "second", "three": "third", "five": "fifth",
"eight": "eighth", "nine": "ninth", "twelve": "twelfth"}.toTable()
 
#---------------------------------------------------------------------------------------------------
 
func spellCardinal(n: int64): string =
## Spell an integer as a cardinal.
 
var n = n
 
if n < 0:
result = "negative "
n = -n
 
if n < 20:
result &= Small[n]
 
elif n < 100:
result &= Tens[n div 10]
let m = n mod 10
if m != 0: result &= '-' & Small[m]
 
elif n < 1000:
result &= Small[n div 100] & " hundred"
let m = n mod 100
if m != 0: result &= ' ' & m.spellCardinal()
 
else:
# Work from right to left.
var sx = ""
var i = 0
while n > 0:
let m = n mod 1000
n = n div 1000
if m != 0:
var ix = m.spellCardinal() & Illions[i]
if sx.len > 0: ix &= " " & sx
sx = ix
inc i
result &= sx
 
#---------------------------------------------------------------------------------------------------
 
func spellOrdinal(n: int64): string =
## Spell an integer as an ordinal.
 
result = n.spellCardinal()
var parts = result.rsplit({' ', '-'}, maxsplit = 1)
let tail = parts[^1]
if tail in IrregularOrdinals:
result[^tail.len..^1] = IrregularOrdinals[tail]
elif tail.endsWith('y'):
result[^1..^1]= "ieth"
else:
result &= "th"
 
 
####################################################################################################
# Sentence building.
 
type Sentence = seq[string]
 
#---------------------------------------------------------------------------------------------------
 
iterator words(sentence: var Sentence): tuple[idx: int; word: string] =
## Yield the successive words of the sentence with their index.
 
yield (0, "Four")
var idx = 1
var last = 0
while true:
yield (idx, sentence[idx])
inc idx
if idx == sentence.len:
inc last
sentence.add([sentence[last].count(Letters).spellCardinal(), "in", "the"])
# For the position, we need to split the ordinal as it may contain spaces.
sentence.add(((last + 1).spellOrdinal() & ',').splitWhitespace())
 
#---------------------------------------------------------------------------------------------------
 
iterator letterCounts(sentence: var Sentence): tuple[idx: int; word: string; count: int] =
## Secondary iterator used to yield the number of letters in addition to the index and the word.
 
for i, word in sentence.words():
yield (i, word, word.count(Letters))
 
 
####################################################################################################
# Drivers.
 
# Constant to initialize the sentence.
const Init = "Four is the number of letters in the first word of this sentence,".splitWhitespace()
 
#---------------------------------------------------------------------------------------------------
 
proc displayLetterCounts(pos: Positive) =
## Display the number of letters of the word at position "pos".
 
var sentence = Init
echo fmt"Number of letters in first {pos} words in the sequence:"
var valcount = 0 # Number of values displayed in the current line.
var length = 0
 
for i, word, letterCount in sentence.letterCounts():
if i == pos:
# Terminated.
dec length # Adjust space count.
echo ""
break
 
if valcount == 0: stdout.write fmt"{i+1:>3}:"
stdout.write fmt"{letterCount:>3}"
inc valcount
inc length, word.len + 1 # +1 for space.
 
if valcount == 12:
# Terminate line.
echo ""
valcount = 0
 
echo fmt"Length of sentence: {length}"
 
#---------------------------------------------------------------------------------------------------
 
proc displayWord(pos: Positive) =
## Display the word at position "pos".
 
var sentence = Init
let idx = pos - 1
var length = 0
for i, word in sentence.words():
length += word.len + 1
if i == idx:
dec length # Adjust space count.
let w = word.strip(leading = false, chars = {','}) # Remove trailing ',' if needed.
echo fmt"Word {pos} is ""{w}"" with {w.count(Letters)} letters."
echo fmt"Length of sentence: {length}"
break
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
displayLetterCounts(201)
for n in [1_000, 10_000, 100_000, 1_000_000, 10_000_000]:
echo ""
displayWord(n)</syntaxhighlight>
 
{{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
Length of sentence: 1203
 
Word 1000 is "in" with 2 letters.
Length of sentence: 6249
 
Word 10000 is "in" with 2 letters.
Length of sentence: 64097
 
Word 100000 is "one" with 3 letters.
Length of sentence: 659455
 
Word 1000000 is "the" with 3 letters.
Length of sentence: 7113560
 
Word 10000000 is "thousand" with 8 letters.
Length of sentence: 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|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use feature 'state';
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
 
Line 506 ⟶ 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 533 ⟶ 1,517:
70995729 characters in the sentence, up to and including this word.</pre>
 
=={{header|Perl 6Phix}}==
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)-->
<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:</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #004080;">integer</span> <span style="color: #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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
<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>
<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>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[$..$]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ieth"</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">"th"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<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>
<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>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</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: #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>
<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>
<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>
<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>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<span style="color: #000080;font-style:italic;">--grr...</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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<span style="color: #000080;font-style:italic;">-- Returns the w'th word and its length (only counting letters).</span>
<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>
<span style="color: #000000;">fi</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<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>
<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>
<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>
<span style="color: #000080;font-style:italic;">-- append eg {"two","in","the","second,"}</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<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>
<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>
<span style="color: #008080;">function</span> <span style="color: #000000;">total_length</span><span style="color: #0000FF;">()</span>
<span style="color: #000080;font-style:italic;">-- Returns the total number of characters (including blanks,
-- commas, and punctuation) of the sentence so far constructed.</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</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: #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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
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>
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 so far:1204
Word 1000 is "in", with 2 letters. Length of sentence so far:6280
Word 10000 is "in", with 2 letters. Length of sentence so far:64692
Word 100000 is "one", with 3 letters. Length of sentence so far:671578
Word 1000000 is "the", with 3 letters. Length of sentence so far:7235383
Word 10000000 is "thousand,", with 8 letters. Length of sentence so far:72079160
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
# Python implementation of Rosetta Code Task
# http://rosettacode.org/wiki/Four_is_the_number_of_letters_in_the_...
# Uses inflect
# https://pypi.org/project/inflect/
 
import inflect
 
def count_letters(word):
"""
count letters ignore , or -, or space
"""
count = 0
for letter in word:
if letter != ',' and letter !='-' and letter !=' ':
count += 1
return count
def split_with_spaces(sentence):
"""
Takes string with partial sentence and returns
list of words with spaces included.
Leading space is attached to first word.
Later spaces attached to prior word.
"""
sentence_list = []
curr_word = ""
for c in sentence:
if c == " " and curr_word != "":
# append space to end of non-empty words
# assumed no more than 1 consecutive space.
sentence_list.append(curr_word+" ")
curr_word = ""
else:
curr_word += c
# add trailing word that does not end with a space
if len(curr_word) > 0:
sentence_list.append(curr_word)
return sentence_list
def my_num_to_words(p, my_number):
"""
Front end to inflect's number_to_words
Get's rid of ands and commas in large numbers.
"""
number_string_list = p.number_to_words(my_number, wantlist=True, andword='')
number_string = number_string_list[0]
for i in range(1,len(number_string_list)):
number_string += " " + number_string_list[i]
return number_string
def build_sentence(p, max_words):
"""
Builds at most max_words of the task following the pattern:
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,
"""
# start with first part of sentence up first comma as a list
sentence_list = split_with_spaces("Four is the number of letters in the first word of this sentence,")
num_words = 13
# which word number we are doing next
# two/second is first one in loop
word_number = 2
# loop until sentance is at least as long as needs be
while num_words < max_words:
# Build something like
# ,two in the second
# get second or whatever we are on
ordinal_string = my_num_to_words(p, p.ordinal(word_number))
# get two or whatever the length is of the word_number word
word_number_string = my_num_to_words(p, count_letters(sentence_list[word_number - 1]))
# sentence addition
new_string = " "+word_number_string+" in the "+ordinal_string+","
 
new_list = split_with_spaces(new_string)
sentence_list += new_list
 
# add new word count
num_words += len(new_list)
# increment word number
word_number += 1
return sentence_list, num_words
def word_and_counts(word_num):
"""
Print's lines like this:
Word 1000 is "in", with 2 letters. Length of sentence so far: 6279
 
"""
sentence_list, num_words = build_sentence(p, word_num)
word_str = sentence_list[word_num - 1].strip(' ,')
num_letters = len(word_str)
num_characters = 0
for word in sentence_list:
num_characters += len(word)
print('Word {0:8d} is "{1}", with {2} letters. Length of the sentence so far: {3} '.format(word_num,word_str,num_letters,num_characters))
p = inflect.engine()
 
sentence_list, num_words = build_sentence(p, 201)
 
print(" ")
print("The lengths of the first 201 words are:")
print(" ")
 
print('{0:3d}: '.format(1),end='')
 
total_characters = 0
 
for word_index in range(201):
 
word_length = count_letters(sentence_list[word_index])
total_characters += len(sentence_list[word_index])
print('{0:2d}'.format(word_length),end='')
if (word_index+1) % 20 == 0:
# newline every 20
print(" ")
print('{0:3d}: '.format(word_index + 2),end='')
else:
print(" ",end='')
print(" ")
print(" ")
print("Length of the sentence so far: "+str(total_characters))
print(" ")
 
"""
 
Expected output this part:
 
Word 1000 is "in", with 2 letters. Length of the sentence so far: 6279
Word 10000 is "in", with 2 letters. Length of the sentence so far: 64140
Word 100000 is "one", with 3 letters. Length of the sentence so far: 659474
Word 1000000 is "the", with 3 letters. Length of the sentence so far: 7113621
Word 10000000 is "thousand", with 8 letters. Length of the sentence so far: 70995756
 
"""
 
word_and_counts(1000)
word_and_counts(10000)
word_and_counts(100000)
word_and_counts(1000000)
word_and_counts(10000000)
</syntaxhighlight>
 
Output:
<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
21: 5 3 2 3 6 3 2 3 5 5 2 3 5 3 2 3 7 5 2 3
41: 6 4 2 3 5 4 2 3 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 9 3 2 3 11 4 2 3
81: 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
121: 12 4 2 3 11 4 2 3 9 3 2 3 11 5 2 3 12 4 2 3
141: 11 5 2 3 12 3 2 3 11 5 2 3 11 5 2 3 13 4 2 3
161: 12 3 2 3 11 5 2 3 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 10 3 2 3 12 5 2 3
201: 11
 
Length of the sentence so far: 1203
 
Word 1000 is "in", with 2 letters. Length of the sentence so far: 6279
Word 10000 is "in", with 2 letters. Length of the sentence so far: 64140
Word 100000 is "one", with 3 letters. Length of the sentence so far: 659474
Word 1000000 is "the", with 3 letters. Length of the sentence so far: 7113621
Word 10000000 is "thousand", with 8 letters. Length of the sentence so far: 70995756
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.09}}
Uses the Lingua::EN::Numbers::Cardinal 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::Cardinal;
no-commas(True);
 
my $index = 1;
my @sentence = flat 'Four is the number of letters in the first word of this sentence, '.words,
{ @sentence[$index++].&alpha.&cardinal, 'in', 'the', |($index.&ordinal.&no-commas ~ ',').words } ... * ;
 
sub alpha ( $str ) { $str.subst(/\W/, '', :g).chars }
sub no-commas ( $str ) { $str.subst(',', '', :g) }
sub count ( $index ) { @sentence[^$index].join(' ').chars ~ " characters in the sentence, up to and including this word.\n" }
 
Line 553 ⟶ 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 576 ⟶ 1,896:
 
Ten millionth word, 'thousand', has 8 characters. 70995729 characters in the sentence, up to and including this word.</pre>
 
=={{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.
<lang Phix>include demo\rosetta\number_names.exw
 
-- as per Spelling_of_ordinal_numbers#Phix:
constant {irregs,ordinals} = columnize({{"one","first"},
{"two","second"},
{"three","third"},
{"five","fifth"},
{"eight","eighth"},
{"nine","ninth"},
{"twelve","twelfth"}})
function ordinal(string s)
integer i
for i=length(s) to 1 by -1 do
integer ch = s[i]
if ch=' ' or ch='-' then exit end if
end for
integer k = find(s[i+1..$],irregs)
if k then
s = s[1..i]&ordinals[k]
elsif s[$]='y' then
s[$..$] = "ieth"
else
s &= "th"
end if
return s
end function
--/copy of Spelling_of_ordinal_numers#Phix
 
function countLetters(string s)
integer res = 0
for i=1 to length(s) do
integer ch = s[i]
if (ch>='A' and ch<='Z')
or (ch>='a' and ch<='z') then
res += 1
end if
end for
return res
end function
 
sequence words = split("Four is the number of letters in the first word of this sentence,")
integer fi = 1
 
function kill_and(sequence s)
--grr...
for i=length(s) to 1 by -1 do
if s[i] = "and" then
s[i..i] = {}
end if
end for
return s
end function
 
function WordLen(integer w)
-- Returns the w'th word and its length (only counting letters).
while length(words)<w do
fi += 1
integer n = countLetters(words[fi])
sequence ns = kill_and(split(spell(n)))
sequence os = kill_and(split(ordinal(spell(fi)) & ","))
-- append eg {"two","in","the","second,"}
words &= ns&{"in","the"}&os
end while
string word = words[w]
return {word, countLetters(word)}
end function
 
function TotalLength()
-- Returns the total number of characters (including blanks,
-- commas, and punctuation) of the sentence so far constructed.
integer res = 0
for i=1 to length(words) do
res += length(words[i])+1
end for
return res
end function
 
procedure main()
integer i,n
string w
printf(1,"The lengths of the first 201 words are:\n")
for i=1 to 201 do
if mod(i,25)==1 then
printf(1,"\n%3d: ", i)
end if
{?,n} = WordLen(i)
printf(1," %2d", n)
end for
printf(1,"\nLength of sentence so far:%d\n", TotalLength())
for p=3 to 7 do
i = power(10,p)
{w, n} = WordLen(i)
printf(1,"Word %8d is \"%s\", with %d letters.", {i, w, n})
printf(1," Length of sentence so far:%d\n", TotalLength())
end for
end procedure
main()</lang>
{{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 so far:1204
Word 1000 is "in", with 2 letters. Length of sentence so far:6280
Word 10000 is "in", with 2 letters. Length of sentence so far:64692
Word 100000 is "one", with 3 letters. Length of sentence so far:671578
Word 1000000 is "the", with 3 letters. Length of sentence so far:7235383
Word 10000000 is "thousand,", with 8 letters. Length of sentence so far:72079160
</pre>
 
=={{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 741 ⟶ 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 769 ⟶ 1,968:
3 is the length of word 1000000 [the]
length of sentence= 7113621
</pre>
 
=={{header|Rust}}==
{{trans|C}}
<syntaxhighlight lang="rust">struct NumberNames {
cardinal: &'static str,
ordinal: &'static str,
}
 
impl NumberNames {
fn get_name(&self, ordinal: bool) -> &'static str {
if ordinal {
return self.ordinal;
}
self.cardinal
}
}
 
const SMALL_NAMES: [NumberNames; 20] = [
NumberNames {
cardinal: "zero",
ordinal: "zeroth",
},
NumberNames {
cardinal: "one",
ordinal: "first",
},
NumberNames {
cardinal: "two",
ordinal: "second",
},
NumberNames {
cardinal: "three",
ordinal: "third",
},
NumberNames {
cardinal: "four",
ordinal: "fourth",
},
NumberNames {
cardinal: "five",
ordinal: "fifth",
},
NumberNames {
cardinal: "six",
ordinal: "sixth",
},
NumberNames {
cardinal: "seven",
ordinal: "seventh",
},
NumberNames {
cardinal: "eight",
ordinal: "eighth",
},
NumberNames {
cardinal: "nine",
ordinal: "ninth",
},
NumberNames {
cardinal: "ten",
ordinal: "tenth",
},
NumberNames {
cardinal: "eleven",
ordinal: "eleventh",
},
NumberNames {
cardinal: "twelve",
ordinal: "twelfth",
},
NumberNames {
cardinal: "thirteen",
ordinal: "thirteenth",
},
NumberNames {
cardinal: "fourteen",
ordinal: "fourteenth",
},
NumberNames {
cardinal: "fifteen",
ordinal: "fifteenth",
},
NumberNames {
cardinal: "sixteen",
ordinal: "sixteenth",
},
NumberNames {
cardinal: "seventeen",
ordinal: "seventeenth",
},
NumberNames {
cardinal: "eighteen",
ordinal: "eighteenth",
},
NumberNames {
cardinal: "nineteen",
ordinal: "nineteenth",
},
];
 
const TENS: [NumberNames; 8] = [
NumberNames {
cardinal: "twenty",
ordinal: "twentieth",
},
NumberNames {
cardinal: "thirty",
ordinal: "thirtieth",
},
NumberNames {
cardinal: "forty",
ordinal: "fortieth",
},
NumberNames {
cardinal: "fifty",
ordinal: "fiftieth",
},
NumberNames {
cardinal: "sixty",
ordinal: "sixtieth",
},
NumberNames {
cardinal: "seventy",
ordinal: "seventieth",
},
NumberNames {
cardinal: "eighty",
ordinal: "eightieth",
},
NumberNames {
cardinal: "ninety",
ordinal: "ninetieth",
},
];
 
struct NamedNumber {
cardinal: &'static str,
ordinal: &'static str,
number: usize,
}
 
impl NamedNumber {
fn get_name(&self, ordinal: bool) -> &'static str {
if ordinal {
return self.ordinal;
}
self.cardinal
}
}
 
const N: usize = 7;
const NAMED_NUMBERS: [NamedNumber; N] = [
NamedNumber {
cardinal: "hundred",
ordinal: "hundredth",
number: 100,
},
NamedNumber {
cardinal: "thousand",
ordinal: "thousandth",
number: 1000,
},
NamedNumber {
cardinal: "million",
ordinal: "millionth",
number: 1000000,
},
NamedNumber {
cardinal: "billion",
ordinal: "billionth",
number: 1000000000,
},
NamedNumber {
cardinal: "trillion",
ordinal: "trillionth",
number: 1000000000000,
},
NamedNumber {
cardinal: "quadrillion",
ordinal: "quadrillionth",
number: 1000000000000000,
},
NamedNumber {
cardinal: "quintillion",
ordinal: "quintillionth",
number: 1000000000000000000,
},
];
 
fn big_name(n: usize) -> &'static NamedNumber {
for i in 1..N {
if n < NAMED_NUMBERS[i].number {
return &NAMED_NUMBERS[i - 1];
}
}
&NAMED_NUMBERS[N - 1]
}
 
fn count_letters(s: &str) -> usize {
let mut count = 0;
for c in s.chars() {
if c.is_alphabetic() {
count += 1;
}
}
count
}
 
struct WordList {
words: Vec<(usize, usize)>,
string: String,
}
 
impl WordList {
fn new() -> WordList {
WordList {
words: Vec::new(),
string: String::new(),
}
}
fn append(&mut self, s: &str) {
let offset = self.string.len();
self.string.push_str(s);
self.words.push((offset, offset + s.len()));
}
fn extend(&mut self, s: &str) {
let len = self.words.len();
let mut w = &mut self.words[len - 1];
w.1 += s.len();
self.string.push_str(s);
}
fn len(&self) -> usize {
self.words.len()
}
fn sentence_length(&self) -> usize {
let n = self.words.len();
if n == 0 {
return 0;
}
self.string.len() + n - 1
}
fn get_word(&self, index: usize) -> &str {
let w = &self.words[index];
&self.string[w.0..w.1]
}
}
 
fn append_number_name(words: &mut WordList, n: usize, ordinal: bool) -> usize {
let mut count = 0;
if n < 20 {
words.append(SMALL_NAMES[n].get_name(ordinal));
count += 1;
} else if n < 100 {
if n % 10 == 0 {
words.append(TENS[n / 10 - 2].get_name(ordinal));
} else {
words.append(TENS[n / 10 - 2].get_name(false));
words.extend("-");
words.extend(SMALL_NAMES[n % 10].get_name(ordinal));
}
count += 1;
} else {
let big = big_name(n);
count += append_number_name(words, n / big.number, false);
if n % big.number == 0 {
words.append(big.get_name(ordinal));
count += 1;
} else {
words.append(big.get_name(false));
count += 1;
count += append_number_name(words, n % big.number, ordinal);
}
}
count
}
 
fn sentence(count: usize) -> WordList {
let mut result = WordList::new();
const WORDS: &'static [&'static str] = &[
"Four",
"is",
"the",
"number",
"of",
"letters",
"in",
"the",
"first",
"word",
"of",
"this",
"sentence,",
];
for s in WORDS {
result.append(s);
}
let mut n = result.len();
let mut i = 1;
while count > n {
let count = count_letters(result.get_word(i));
n += append_number_name(&mut result, count, false);
result.append("in");
result.append("the");
n += 2;
n += append_number_name(&mut result, i + 1, true);
result.extend(",");
i += 1;
}
result
}
 
fn main() {
let mut n = 201;
let s = sentence(n);
println!("Number of letters in first {} words in the sequence:", n);
for i in 0..n {
if i != 0 {
if i % 25 == 0 {
println!();
} else {
print!(" ");
}
}
print!("{:2}", count_letters(s.get_word(i)));
}
println!();
println!("Sentence length: {}", s.sentence_length());
n = 1000;
while n <= 10000000 {
let s = sentence(n);
let word = s.get_word(n - 1);
print!(
"The {}th word is '{}' and has {} letters. ",
n,
word,
count_letters(word)
);
println!("Sentence length: {}", s.sentence_length());
n *= 10;
}
}</syntaxhighlight>
 
{{out}}
<pre>
Number of letters in first 201 words in the 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
Sentence length: 1203
The 1000th word is 'in' and has 2 letters. Sentence length: 6279
The 10000th word is 'in' and has 2 letters. Sentence length: 64140
The 100000th word is 'one' and has 3 letters. Sentence length: 659474
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 800 ⟶ 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 808 ⟶ 2,585:
if(0 == n%25) print("\n%3d:".fmt(n+1));
}
println("\nLength of above sentence: ",total);</langsyntaxhighlight>
{{out}}
<pre>
Line 822 ⟶ 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 828 ⟶ 2,605:
.fmt(n,word,word.len(),total));
n*=10;
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits