Humble numbers: Difference between revisions

Content added Content deleted
(→‎Fast and economical: improve space management)
Line 3,111: Line 3,111:
def update:
def update:
.[0] as $h
.[0] as $h
| ([$h * 3, $h * 5, $h * 7] | map(select(tostring|length <= $digits))) as $next
| ([$h * 2, $h * 3, $h * 5, $h * 7] | map(select(tostring|length <= $digits))) as $next
| (.[1:] + [$h * 2] + $next) | sort;
| if $next == [] then .[1:]
else (.[1:] + $next) | sort
end;
def trim: if length <= 1 then . elif .[0]==.[1] then .[1:]|trim else . end;
def trim: if length <= 1 then . elif .[0]==.[1] then .[1:]|trim else . end;
{ h: 1, queue: [2,3,5,7]}
{ queue: [1]}
| while( (.h|tostring|length) <= $digits;
| recurse( select( .queue != [] )
.queue |= trim
| .h = .queue[0]
| queue |= (update|trim) )
| .h = .queue[0]
| (.queue |= update)
| .h ;
)
| .h;


def distribution(stream):
def distribution(stream):
Line 3,149: Line 3,149:
15: 7545
15: 7545
16: 9081</pre>
16: 9081</pre>

The queue in this case grows to a maximum length of about 18K.


=={{header|Julia}}==
=={{header|Julia}}==