Jump to content

Humble numbers: Difference between revisions

→‎{{header|jq}}: Fast and economical
m (added whitespace.)
(→‎{{header|jq}}: Fast and economical)
Line 3,058:
=={{header|jq}}==
 
===Brute force ...===
First, brute force (because we can) ...
<lang>
# Input: a positive integer
Line 3,101 ⟶ 3,102:
5: 356
6: 579</pre>
===Fast and economical===
Having already shown one way to display the first few humble numbers, this subsection will focus on the more difficult problem.
<lang jq>
# A generator
def humbles($digits):
def update:
.[0] as $h
| ([$h * 3, $h * 5, $h * 7] | map(select(tostring|length <= $digits))) as $next
| (.[1:] + [$h * 2] + $next) | sort;
def trim: if length <= 1 then . elif .[0]==.[1] then .[1:]|trim else . end;
{ h: 1, queue: [2,3,5,7]}
| while( (.h|tostring|length) <= $digits;
.queue |= trim
| .h = .queue[0]
| (.queue |= update)
)
| .h;
 
def distribution(stream):
reduce stream as $i ([]; .[$i|tostring|length-1]+=1);
 
def task($digits):
"Distribution of the number of decimal digits up to \($digits) digits:",
(distribution(humbles($digits)) | range(0;length) as $i | " \($i+1): \(.[$i])") ;
 
task(11)</lang>
{{out}}
 
<pre>
Distribution of the number of decimal digits up to 11 digits:
1: 9
2: 36
3: 95
4: 197
5: 356
6: 579
7: 882
8: 1272
9: 1767
10: 2381
11: 3113</pre>
 
=={{header|Julia}}==
2,472

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.