Four is the number of letters in the ...
The Four is ... sequence is based on the counting of the number of letters in the words of the (never─ending) 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, ···
Letters are defined as the upper- and lowercase letters in the Latin alphabet (A──►Z and a──►z).
Commas are not counted, nor are hyphens (dashes or minus signs) for such numbers as twenty─three (which has 11 letters).
Twenty─three is considered one word (which is hyphenated).
Furthermore, the American version of numbers will be used here in this task (as opposed to the British).
2,000,000,000 is two billion, not two milliard.
- Task
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 N words in the never─ending sentence.
For instance, the portion of the never─ending sentence shown above (2nd sentence of this task), the sequence would be:
4 2 3 6 2 7
Only construct as much as is needed for the never─ending sentence.
After the sequence is displayed, also show the total number of characters (including blanks, commas, and
punctuation) of the sentence that was constructed.
Also, write a driver (invoking routine) to show the number of letters in the Nth word, as
well as the Nth word itself.
Show all output here.
- Test cases
Display the first 201 numbers in the sequence (and the total number of characters in the sentence). Display the number of letters (and the word itself) of the 1,000th word. Display the number of letters (and the word itself) of the 10,000th word. Display the number of letters (and the word itself) of the 100,000th word. Display the number of letters (and the word itself) of the 1,000,000th word. Display the number of letters (and the word itself) of the 10,000,000th word (optional).
- Related task
- [[Look-and-say sequence]
- Number names
- Self-describing numbers
- Self-referential sequence
- Spelling of ordinal numbers
<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. */
parse arg N M /*obtain optional argument from the CL.*/ if N= | N="," then N= 201 /*Not specified? Then use the default.*/ if M= | M="," then M=1000 10000 100000 1000000 /* " " " " " " */ @abcU= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*define the uppercase Latin alphabet. */ !.=.; #.=.; q=1; w=length(N) /* [↓] define some helpful low values.*/ call tell N if N<0 then say y ' is the length of word ' a " ["word(@, a)"]" say /* [↑] N negative? Just show 1 number*/ say 'length of sentence= ' length(@) /*display the length of the @ sentence.*/
if M\== then do k=1 for words(M) while M\=0 /*maybe handle counts (if specified). */
x=word(M, k) /*obtain the Kth word of the M list. */ call tell -x /*invoke subroutine (with negative arg)*/ say say y ' is the length of word ' x " ["word(@, x)"]" say 'length of sentence= ' length(@) /*display length of @ sentence.*/ end /*k*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ wordLen: arg ?; return length(?) - length( space( translate(?, , @abcU), 0) ) /*──────────────────────────────────────────────────────────────────────────────────────*/ tell: parse arg z,,$; idx=1; a=abs(z); group=25 /*show 25 numbers per line.*/
/*Q is the last number spelt by $SPELL#*/ do j=1 for a /*traipse through all the numbers to N.*/ do 2 /*perform loop twice (well ··· maybe).*/ y=wordLen( word(@, j) ) /*get the Jth word from the sentence.*/ if y\==0 then leave /*Is the word spelt? Then we're done.*/ q=q + 1 /*bump the on─going (moving) # counter.*/ if #.q==. then #.q=$spell#(q 'Q ORD') /*need to spell A as an ordinal number?*/ _=wordLen( word(@, q) ) /*use the length of the ordinal number.*/ if !._==. then !._=$spell#(_ 'Q') /*Not spelled? Then go and spell it. */ @=@ !._ 'in the' #.q"," /*append words to never─ending sentence*/ end /*2*/ /* [↑] Q ≡ Quiet ORD ≡ ORDinal */
$=$ || right(y, 3) /* [↓] append a justified # to a line.*/ if j//group==0 & z>0 then do; say right(idx, w)'►'$; idx=idx+group; $=; end end /*j*/ /* [↑] show line if there's enough #s.*/
if $\== & z>0 then say right(idx, w)'►'$ /*display if there are residual numbers*/ return</lang>
- output:
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= 1203 2 is the length of word 1000 [in] length of sentence= 6279 2 is the length of word 10000 [in] length of sentence= 64140 3 is the length of word 100000 [one] length of sentence= 659474 3 is the length of word 1000000 [the] length of sentence= 7113621