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

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (syntax highlighting fixup automation)
Line 52: Line 52:
=={{header|C}}==
=={{header|C}}==
{{libheader|GLib}}
{{libheader|GLib}}
<lang c>#include <ctype.h>
<syntaxhighlight lang="c">#include <ctype.h>
#include <locale.h>
#include <locale.h>
#include <stdbool.h>
#include <stdbool.h>
Line 254: Line 254:
word_list_destroy(&result);
word_list_destroy(&result);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 277: Line 277:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <cctype>
<syntaxhighlight lang="cpp">#include <cctype>
#include <cstdint>
#include <cstdint>
#include <iomanip>
#include <iomanip>
Line 433: Line 433:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 463: Line 463:
[[Spelling_of_ordinal_numbers#Go|Spelling of ordinal numbers]] task
[[Spelling_of_ordinal_numbers#Go|Spelling of ordinal numbers]] task
(omitted from this listing).
(omitted from this listing).
<lang Go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 556: Line 556:
// omitted from this listing
// omitted from this listing
// ...
// ...
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>The lengths of the first 201 words are:
<pre>The lengths of the first 201 words are:
Line 580: Line 580:
Uses the solution [[Spelling_of_ordinal_numbers#Haskell]] and tying-the-knot technique to create the infinite sentence.
Uses the solution [[Spelling_of_ordinal_numbers#Haskell]] and tying-the-knot technique to create the infinite sentence.


<lang haskell>import Data.Char
<syntaxhighlight lang="haskell">import Data.Char


sentence = start ++ foldMap add (zip [2..] $ tail $ words sentence)
sentence = start ++ foldMap add (zip [2..] $ tail $ words sentence)
Line 596: Line 596:
in "The " ++ spellOrdinal n ++ " word is \"" ++ w ++ "\" which has " ++
in "The " ++ spellOrdinal n ++ " word is \"" ++ w ++ "\" which has " ++
spellInteger (alphaLength w) ++ " letters. The sentence length is " ++
spellInteger (alphaLength w) ++ " letters. The sentence length is " ++
show (length $ unwords a) ++ " chars."</lang>
show (length $ unwords a) ++ " chars."</syntaxhighlight>


<pre>λ> take 200 sentence
<pre>λ> take 200 sentence
Line 610: Line 610:
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 "-").
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.
<br/>The discussion was helpful by providing the first 2202 words of the sentence.
<lang java>
<syntaxhighlight lang="java">
import java.util.HashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map;
Line 824: Line 824:
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 846: Line 846:


=={{header|Julia}}==
=={{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.<lang julia>using DataStructures # for deque
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.<syntaxhighlight lang="julia">using DataStructures # for deque


const seed = "Four is the number of letters in the first word of this sentence, "
const seed = "Four is the number of letters in the first word of this sentence, "
Line 919: Line 919:
println("$n words -> $itercount iterations, $totalletters letters total, ",
println("$n words -> $itercount iterations, $totalletters letters total, ",
"last word \"$lastword\" with $(length(lastword)) letters.")
"last word \"$lastword\" with $(length(lastword)) letters.")
end</lang> {{output}} <pre>
end</syntaxhighlight> {{output}} <pre>
It is interesting how identical lengths align with 20 columns.
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
1: 4 2 3 6 2 7 2 3 5 4 2 4 8 3 2 3 6 5 2 3
Line 942: Line 942:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
This pulls in (slightly adjusted) code from related tasks to convert numbers to text or ordinals.
This pulls in (slightly adjusted) code from related tasks to convert numbers to text or ordinals.
<lang scala>// version 1.1.4-3
<syntaxhighlight lang="scala">// version 1.1.4-3


val names = mapOf(
val names = mapOf(
Line 1,117: Line 1,117:
}
}
while (n <= 10_000_000)
while (n <= 10_000_000)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,152: Line 1,152:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>(*==Number names==*)
<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.*)
(*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.*)
Line 1,237: Line 1,237:
},
},
"\n\n"
"\n\n"
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 1,256: Line 1,256:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils, strformat, tables
<syntaxhighlight lang="nim">import strutils, strformat, tables


####################################################################################################
####################################################################################################
Line 1,417: Line 1,417:
for n in [1_000, 10_000, 100_000, 1_000_000, 10_000_000]:
for n in [1_000, 10_000, 100_000, 1_000_000, 10_000_000]:
echo ""
echo ""
displayWord(n)</lang>
displayWord(n)</syntaxhighlight>


{{out}}
{{out}}
Line 1,458: Line 1,458:
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.
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}}
{{trans|Raku}}
<lang perl>use feature 'state';
<syntaxhighlight lang="perl">use feature 'state';
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
use Lingua::EN::Numbers qw(num2en num2en_ordinal);


Line 1,489: Line 1,489:
ucfirst(num2en_ordinal($_)) . " word, '$sentence[$_-1]' has " . alpha($sentence[$_-1]) . " characters. \n" .
ucfirst(num2en_ordinal($_)) . " word, '$sentence[$_-1]' has " . alpha($sentence[$_-1]) . " characters. \n" .
count($_) . "\n";
count($_) . "\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 201 word lengths in the sequence:
<pre>First 201 word lengths in the sequence:
Line 1,518: Line 1,518:
=={{header|Phix}}==
=={{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.
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>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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
<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
Line 1,615: Line 1,615:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</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.
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}}
{{out}}
Line 1,639: Line 1,639:


=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
# Python implementation of Rosetta Code Task
# Python implementation of Rosetta Code Task
# http://rosettacode.org/wiki/Four_is_the_number_of_letters_in_the_...
# http://rosettacode.org/wiki/Four_is_the_number_of_letters_in_the_...
Line 1,824: Line 1,824:
word_and_counts(1000000)
word_and_counts(1000000)
word_and_counts(10000000)
word_and_counts(10000000)
</syntaxhighlight>
</lang>


Output:
Output:
Line 1,856: Line 1,856:
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.
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.


<lang perl6>use Lingua::EN::Numbers;
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;
no-commas(True);
no-commas(True);


Line 1,872: Line 1,872:
for 1e3, 1e4, 1e5, 1e6, 1e7 {
for 1e3, 1e4, 1e5, 1e6, 1e7 {
say "{.&ordinal.tc} word, '{@sentence[$_ - 1]}', has {@sentence[$_ - 1].&alpha} characters. ", .&count
say "{.&ordinal.tc} word, '{@sentence[$_ - 1]}', has {@sentence[$_ - 1].&alpha} characters. ", .&count
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 201 word lengths in the sequence:
<pre>First 201 word lengths in the sequence:
Line 1,897: Line 1,897:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds/shows the number of letters in the Nth word in a constructed sentence*/
<syntaxhighlight 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,' /*···*/
@= 'Four is the number of letters in the first word of this sentence,' /*···*/
/* [↑] the start of a long sentence. */
/* [↑] the start of a long sentence. */
Line 1,939: Line 1,939:


if $\=='' & z>0 then say right(idx, w)'►'$ /*display if there are residual numbers*/
if $\=='' & z>0 then say right(idx, w)'►'$ /*display if there are residual numbers*/
return</lang>
return</syntaxhighlight>
The &nbsp; '''$SPELL#.REX''' &nbsp; routine can be found here &nbsp; ───► &nbsp; [[$SPELL.REX|$SPELL#.REX]]. <br><br>
The &nbsp; '''$SPELL#.REX''' &nbsp; routine can be found here &nbsp; ───► &nbsp; [[$SPELL.REX|$SPELL#.REX]]. <br><br>


Line 1,971: Line 1,971:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|C}}
{{trans|C}}
<lang rust>struct NumberNames {
<syntaxhighlight lang="rust">struct NumberNames {
cardinal: &'static str,
cardinal: &'static str,
ordinal: &'static str,
ordinal: &'static str,
Line 2,308: Line 2,308:
n *= 10;
n *= 10;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,333: Line 2,333:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var names = {
var names = {
Line 2,513: Line 2,513:
n = n * 10
n = n * 10
if (n > 1e7) break
if (n > 1e7) break
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,549: Line 2,549:
=={{header|zkl}}==
=={{header|zkl}}==
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]]
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]]
<lang zkl> // Built the sentence in little chucks but only save the last one
<syntaxhighlight lang="zkl"> // Built the sentence in little chucks but only save the last one
// Save the word counts
// Save the word counts
fcn fourIsThe(text,numWords){
fcn fourIsThe(text,numWords){
Line 2,576: Line 2,576:
return(lastWords.strip(),szs,total);
return(lastWords.strip(),szs,total);
}
}
fcn lastWord(sentence){ sentence[sentence.rfind(" ")+1,*] }</lang>
fcn lastWord(sentence){ sentence[sentence.rfind(" ")+1,*] }</syntaxhighlight>
<lang zkl>var seed="Four is the number of letters in the first word of this sentence, ";
<syntaxhighlight lang="zkl">var seed="Four is the number of letters in the first word of this sentence, ";
sentence,szs,total := fourIsThe(seed,201);
sentence,szs,total := fourIsThe(seed,201);
print(" 1:");
print(" 1:");
Line 2,584: Line 2,584:
if(0 == n%25) print("\n%3d:".fmt(n+1));
if(0 == n%25) print("\n%3d:".fmt(n+1));
}
}
println("\nLength of above sentence: ",total);</lang>
println("\nLength of above sentence: ",total);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,598: Line 2,598:
Length of above sentence: 1203
Length of above sentence: 1203
</pre>
</pre>
<lang zkl>n:=1000; do(5){
<syntaxhighlight lang="zkl">n:=1000; do(5){
sentence,x,total := fourIsThe(seed,n);
sentence,x,total := fourIsThe(seed,n);
word:=lastWord(sentence);
word:=lastWord(sentence);
Line 2,604: Line 2,604:
.fmt(n,word,word.len(),total));
.fmt(n,word,word.len(),total));
n*=10;
n*=10;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>