<p dir="auto">After I extracted the <code class="notranslate">child_loop()</code> inner function (aka "closure") from the first program (which had two pieces of duplicate code) it makes the code much slower: from under 3seconds to over a minute).  This code is a solution I wrote to one of the <a href="https://en.wikipedia.org/wiki/Project_Euler" rel="nofollow">Project Euler</a> problems and it is pure/core Julia. I am using x86-64 mageia 8 Linux, but the issue happens also on Fedora 32 x86-64.</p>
<p dir="auto">The code before the change (without the slowdown):</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
const CHECK = false
function brute_my_calc(start_, end_, x0)
    sum_ = 0
    count_ = 0
    for n in (start_:end_)
        count_ += 1
        prev = x0
        next_ = ((prev + cld(n, prev)) &gt;&gt; 1)
        while prev != next_
            sum_ += 1
            prev = next_
            next_ = ((prev + cld(n, prev)) &gt;&gt; 1)
        end
    end
    return sum_, count_
end

function check(start_, end_, x0, have_sum, have_count)
    if CHECK
        gs, gc = brute_my_calc(start_, end_, x0)
        @assert (have_sum, have_count) == (gs, gc)
    end
end

function my_calc(start_, end_, x0)
    @assert start_ &lt;= end_
    count_ = end_ - start_ + 1
    sum_ = Int64(0)
    sofar_ = Int64(0)

    x0inv = cld(start_, x0)
    next_x0inv = x0inv * x0
    identity_n_low = x0*x0 - x0 + 1
    identity_n = x0*x0 + x0
    twice_first_next = x0 + x0inv
    first_next = twice_first_next &gt;&gt; 1
    limit = max(start_-1, min(identity_n_low-1, end_))
    if (first_next == x0 &amp;&amp; end_ &lt; next_x0inv)
        sum_ = 0
        check(start_, end_, x0, sum_, count_)
        return sum_, count_
    end
    @assert identity_n_low &lt;= identity_n

    function calc_child_sum(n, end_next, first_next)
        @assert end_next &gt;= n
        subsum_, subcount_ = my_calc(n, end_next, first_next)
        @assert subcount_ == end_next-n+1
        @assert subsum_ &gt;= 0
        subsum_ += subcount_
        check(n, end_next, x0, subsum_, subcount_)
        return subsum_, subcount_
    end

    n = start_
    while n &lt;= limit
        if next_x0inv &lt; n
            next_x0inv += x0
            twice_first_next += 1
            first_next = twice_first_next &gt;&gt; 1
            @assert next_x0inv &gt;= n
            if twice_first_next &amp; 0b1 == 0
                twice_first_next += 1
                next_x0inv += x0
            end
        end

        end_next = next_x0inv
        if end_next &gt; limit
            end_next = limit
        end

        subsum_, subcount_ = calc_child_sum(n, end_next, first_next)
        sum_ += subsum_
        sofar_ += subcount_
        check(start_, end_next, x0, sum_, sofar_)
        n = end_next + 1
    end


    check(start_, limit, x0, sum_, sofar_)
    @assert n == limit + 1
    start2 = max(start_, min(end_, limit + 1))
    limit2 = max(start_, min(end_, identity_n))
    if limit2 &gt;= start2 &amp;&amp; start2 &gt;= n
        diff = limit2 - start2 + 1
        sofar_ += diff
        @assert sofar_ == limit2 - start_ + 1
        check(start_, limit2, x0, sum_, sofar_)
    end
    if limit2 != end_
        start3 = limit2 + 1
        while start3 &gt; next_x0inv
            next_x0inv += x0
            twice_first_next += 1
        end
        first_next = twice_first_next &gt;&gt; 1
        initsofar_ = sofar_

        n = start3
        while n &lt;= end_
            if next_x0inv &lt; n
                next_x0inv += x0
                twice_first_next += 1
                first_next = twice_first_next &gt;&gt; 1
                @assert next_x0inv &gt;= n
                if twice_first_next &amp; 0b1 == 0
                    twice_first_next += 1
                    next_x0inv += x0
                end
            end

            end_next = next_x0inv
            if end_next &gt; end_
                end_next = end_
            end

            subsum_, subcount_ = calc_child_sum(n, end_next, first_next)
            sum_ += subsum_
            sofar_ += subcount_
            check(start_, end_next, x0, sum_, sofar_)
            n = end_next + 1
        end
    end

    @assert sum_ &gt;= 0
    @assert count_ == sofar_
    check(start_, end_, x0, sum_, sofar_)
    return sum_, count_
end

function digit_wrapper(dd)
    d = Int64(0+dd)
    start_ = Int64(10^(d-1))
    end_ = Int64(10*start_-1)
    if d % 2 == 0
        x0 = 7 * (10^((d-2)&gt;&gt;1))
    else
        x0 = 2 * (10^((d-1)&gt;&gt;1))
    end
    x0 = Int64(x0)
    sum_, count_ = my_calc(start_, end_, x0)
    sum_+=count_
    @assert count_ == end_ - start_ + 1
    return sum_, count_
end

function digit_average(dd)
    sum_, count_ = digit_wrapper(dd)
    @show dd, sum_, count_
    return sum_ / count_
end
ret = digit_average(5)
@show ret
@assert abs(ret - 3.2102888889) &lt; 0.0000001
if true
    ret = digit_average(14)
    @show ret
end"><pre class="notranslate"><span class="pl-k">const</span> CHECK <span class="pl-k">=</span> <span class="pl-c1">false</span>
<span class="pl-k">function</span> <span class="pl-en">brute_my_calc</span>(start_, end_, x0)
    sum_ <span class="pl-k">=</span> <span class="pl-c1">0</span>
    count_ <span class="pl-k">=</span> <span class="pl-c1">0</span>
    <span class="pl-k">for</span> n <span class="pl-k">in</span> (start_<span class="pl-k">:</span>end_)
        count_ <span class="pl-k">+=</span> <span class="pl-c1">1</span>
        prev <span class="pl-k">=</span> x0
        next_ <span class="pl-k">=</span> ((prev <span class="pl-k">+</span> <span class="pl-c1">cld</span>(n, prev)) <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>)
        <span class="pl-k">while</span> prev <span class="pl-k">!=</span> next_
            sum_ <span class="pl-k">+=</span> <span class="pl-c1">1</span>
            prev <span class="pl-k">=</span> next_
            next_ <span class="pl-k">=</span> ((prev <span class="pl-k">+</span> <span class="pl-c1">cld</span>(n, prev)) <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>)
        <span class="pl-k">end</span>
    <span class="pl-k">end</span>
    <span class="pl-k">return</span> sum_, count_
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">check</span>(start_, end_, x0, have_sum, have_count)
    <span class="pl-k">if</span> CHECK
        gs, gc <span class="pl-k">=</span> <span class="pl-c1">brute_my_calc</span>(start_, end_, x0)
        <span class="pl-c1">@assert</span> (have_sum, have_count) <span class="pl-k">==</span> (gs, gc)
    <span class="pl-k">end</span>
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">my_calc</span>(start_, end_, x0)
    <span class="pl-c1">@assert</span> start_ <span class="pl-k">&lt;=</span> end_
    count_ <span class="pl-k">=</span> end_ <span class="pl-k">-</span> start_ <span class="pl-k">+</span> <span class="pl-c1">1</span>
    sum_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">0</span>)
    sofar_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">0</span>)

    x0inv <span class="pl-k">=</span> <span class="pl-c1">cld</span>(start_, x0)
    next_x0inv <span class="pl-k">=</span> x0inv <span class="pl-k">*</span> x0
    identity_n_low <span class="pl-k">=</span> x0<span class="pl-k">*</span>x0 <span class="pl-k">-</span> x0 <span class="pl-k">+</span> <span class="pl-c1">1</span>
    identity_n <span class="pl-k">=</span> x0<span class="pl-k">*</span>x0 <span class="pl-k">+</span> x0
    twice_first_next <span class="pl-k">=</span> x0 <span class="pl-k">+</span> x0inv
    first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
    limit <span class="pl-k">=</span> <span class="pl-c1">max</span>(start_<span class="pl-k">-</span><span class="pl-c1">1</span>, <span class="pl-c1">min</span>(identity_n_low<span class="pl-k">-</span><span class="pl-c1">1</span>, end_))
    <span class="pl-k">if</span> (first_next <span class="pl-k">==</span> x0 <span class="pl-k">&amp;&amp;</span> end_ <span class="pl-k">&lt;</span> next_x0inv)
        sum_ <span class="pl-k">=</span> <span class="pl-c1">0</span>
        <span class="pl-c1">check</span>(start_, end_, x0, sum_, count_)
        <span class="pl-k">return</span> sum_, count_
    <span class="pl-k">end</span>
    <span class="pl-c1">@assert</span> identity_n_low <span class="pl-k">&lt;=</span> identity_n

    <span class="pl-k">function</span> <span class="pl-en">calc_child_sum</span>(n, end_next, first_next)
        <span class="pl-c1">@assert</span> end_next <span class="pl-k">&gt;=</span> n
        subsum_, subcount_ <span class="pl-k">=</span> <span class="pl-c1">my_calc</span>(n, end_next, first_next)
        <span class="pl-c1">@assert</span> subcount_ <span class="pl-k">==</span> end_next<span class="pl-k">-</span>n<span class="pl-k">+</span><span class="pl-c1">1</span>
        <span class="pl-c1">@assert</span> subsum_ <span class="pl-k">&gt;=</span> <span class="pl-c1">0</span>
        subsum_ <span class="pl-k">+=</span> subcount_
        <span class="pl-c1">check</span>(n, end_next, x0, subsum_, subcount_)
        <span class="pl-k">return</span> subsum_, subcount_
    <span class="pl-k">end</span>

    n <span class="pl-k">=</span> start_
    <span class="pl-k">while</span> n <span class="pl-k">&lt;=</span> limit
        <span class="pl-k">if</span> next_x0inv <span class="pl-k">&lt;</span> n
            next_x0inv <span class="pl-k">+=</span> x0
            twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
            first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
            <span class="pl-c1">@assert</span> next_x0inv <span class="pl-k">&gt;=</span> n
            <span class="pl-k">if</span> twice_first_next <span class="pl-k">&amp;</span> <span class="pl-c1">0b1</span> <span class="pl-k">==</span> <span class="pl-c1">0</span>
                twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
                next_x0inv <span class="pl-k">+=</span> x0
            <span class="pl-k">end</span>
        <span class="pl-k">end</span>

        end_next <span class="pl-k">=</span> next_x0inv
        <span class="pl-k">if</span> end_next <span class="pl-k">&gt;</span> limit
            end_next <span class="pl-k">=</span> limit
        <span class="pl-k">end</span>

        subsum_, subcount_ <span class="pl-k">=</span> <span class="pl-c1">calc_child_sum</span>(n, end_next, first_next)
        sum_ <span class="pl-k">+=</span> subsum_
        sofar_ <span class="pl-k">+=</span> subcount_
        <span class="pl-c1">check</span>(start_, end_next, x0, sum_, sofar_)
        n <span class="pl-k">=</span> end_next <span class="pl-k">+</span> <span class="pl-c1">1</span>
    <span class="pl-k">end</span>


    <span class="pl-c1">check</span>(start_, limit, x0, sum_, sofar_)
    <span class="pl-c1">@assert</span> n <span class="pl-k">==</span> limit <span class="pl-k">+</span> <span class="pl-c1">1</span>
    start2 <span class="pl-k">=</span> <span class="pl-c1">max</span>(start_, <span class="pl-c1">min</span>(end_, limit <span class="pl-k">+</span> <span class="pl-c1">1</span>))
    limit2 <span class="pl-k">=</span> <span class="pl-c1">max</span>(start_, <span class="pl-c1">min</span>(end_, identity_n))
    <span class="pl-k">if</span> limit2 <span class="pl-k">&gt;=</span> start2 <span class="pl-k">&amp;&amp;</span> start2 <span class="pl-k">&gt;=</span> n
        diff <span class="pl-k">=</span> limit2 <span class="pl-k">-</span> start2 <span class="pl-k">+</span> <span class="pl-c1">1</span>
        sofar_ <span class="pl-k">+=</span> diff
        <span class="pl-c1">@assert</span> sofar_ <span class="pl-k">==</span> limit2 <span class="pl-k">-</span> start_ <span class="pl-k">+</span> <span class="pl-c1">1</span>
        <span class="pl-c1">check</span>(start_, limit2, x0, sum_, sofar_)
    <span class="pl-k">end</span>
    <span class="pl-k">if</span> limit2 <span class="pl-k">!=</span> end_
        start3 <span class="pl-k">=</span> limit2 <span class="pl-k">+</span> <span class="pl-c1">1</span>
        <span class="pl-k">while</span> start3 <span class="pl-k">&gt;</span> next_x0inv
            next_x0inv <span class="pl-k">+=</span> x0
            twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
        <span class="pl-k">end</span>
        first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
        initsofar_ <span class="pl-k">=</span> sofar_

        n <span class="pl-k">=</span> start3
        <span class="pl-k">while</span> n <span class="pl-k">&lt;=</span> end_
            <span class="pl-k">if</span> next_x0inv <span class="pl-k">&lt;</span> n
                next_x0inv <span class="pl-k">+=</span> x0
                twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
                first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
                <span class="pl-c1">@assert</span> next_x0inv <span class="pl-k">&gt;=</span> n
                <span class="pl-k">if</span> twice_first_next <span class="pl-k">&amp;</span> <span class="pl-c1">0b1</span> <span class="pl-k">==</span> <span class="pl-c1">0</span>
                    twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
                    next_x0inv <span class="pl-k">+=</span> x0
                <span class="pl-k">end</span>
            <span class="pl-k">end</span>

            end_next <span class="pl-k">=</span> next_x0inv
            <span class="pl-k">if</span> end_next <span class="pl-k">&gt;</span> end_
                end_next <span class="pl-k">=</span> end_
            <span class="pl-k">end</span>

            subsum_, subcount_ <span class="pl-k">=</span> <span class="pl-c1">calc_child_sum</span>(n, end_next, first_next)
            sum_ <span class="pl-k">+=</span> subsum_
            sofar_ <span class="pl-k">+=</span> subcount_
            <span class="pl-c1">check</span>(start_, end_next, x0, sum_, sofar_)
            n <span class="pl-k">=</span> end_next <span class="pl-k">+</span> <span class="pl-c1">1</span>
        <span class="pl-k">end</span>
    <span class="pl-k">end</span>

    <span class="pl-c1">@assert</span> sum_ <span class="pl-k">&gt;=</span> <span class="pl-c1">0</span>
    <span class="pl-c1">@assert</span> count_ <span class="pl-k">==</span> sofar_
    <span class="pl-c1">check</span>(start_, end_, x0, sum_, sofar_)
    <span class="pl-k">return</span> sum_, count_
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">digit_wrapper</span>(dd)
    d <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">0</span><span class="pl-k">+</span>dd)
    start_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">10</span><span class="pl-k">^</span>(d<span class="pl-k">-</span><span class="pl-c1">1</span>))
    end_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">10</span><span class="pl-k">*</span>start_<span class="pl-k">-</span><span class="pl-c1">1</span>)
    <span class="pl-k">if</span> d <span class="pl-k">%</span> <span class="pl-c1">2</span> <span class="pl-k">==</span> <span class="pl-c1">0</span>
        x0 <span class="pl-k">=</span> <span class="pl-c1">7</span> <span class="pl-k">*</span> (<span class="pl-c1">10</span><span class="pl-k">^</span>((d<span class="pl-k">-</span><span class="pl-c1">2</span>)<span class="pl-k">&gt;&gt;</span><span class="pl-c1">1</span>))
    <span class="pl-k">else</span>
        x0 <span class="pl-k">=</span> <span class="pl-c1">2</span> <span class="pl-k">*</span> (<span class="pl-c1">10</span><span class="pl-k">^</span>((d<span class="pl-k">-</span><span class="pl-c1">1</span>)<span class="pl-k">&gt;&gt;</span><span class="pl-c1">1</span>))
    <span class="pl-k">end</span>
    x0 <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(x0)
    sum_, count_ <span class="pl-k">=</span> <span class="pl-c1">my_calc</span>(start_, end_, x0)
    sum_<span class="pl-k">+=</span>count_
    <span class="pl-c1">@assert</span> count_ <span class="pl-k">==</span> end_ <span class="pl-k">-</span> start_ <span class="pl-k">+</span> <span class="pl-c1">1</span>
    <span class="pl-k">return</span> sum_, count_
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">digit_average</span>(dd)
    sum_, count_ <span class="pl-k">=</span> <span class="pl-c1">digit_wrapper</span>(dd)
    <span class="pl-c1">@show</span> dd, sum_, count_
    <span class="pl-k">return</span> sum_ <span class="pl-k">/</span> count_
<span class="pl-k">end</span>
ret <span class="pl-k">=</span> <span class="pl-c1">digit_average</span>(<span class="pl-c1">5</span>)
<span class="pl-c1">@show</span> ret
<span class="pl-c1">@assert</span> <span class="pl-c1">abs</span>(ret <span class="pl-k">-</span> <span class="pl-c1">3.2102888889</span>) <span class="pl-k">&lt;</span> <span class="pl-c1">0.0000001</span>
<span class="pl-k">if</span> <span class="pl-c1">true</span>
    ret <span class="pl-k">=</span> <span class="pl-c1">digit_average</span>(<span class="pl-c1">14</span>)
    <span class="pl-c1">@show</span> ret
<span class="pl-k">end</span></pre></div>
<p dir="auto">The program after the change (with the slowdown):</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const CHECK = false
function brute_my_calc(start_, end_, x0)
    sum_ = 0
    count_ = 0
    for n in (start_:end_)
        count_ += 1
        prev = x0
        next_ = ((prev + cld(n, prev)) &gt;&gt; 1)
        while prev != next_
            sum_ += 1
            prev = next_
            next_ = ((prev + cld(n, prev)) &gt;&gt; 1)
        end
    end
    return sum_, count_
end

function check(start_, end_, x0, have_sum, have_count)
    if CHECK
        gs, gc = brute_my_calc(start_, end_, x0)
        @assert (have_sum, have_count) == (gs, gc)
    end
end

function my_calc(start_, end_, x0)
    @assert start_ &lt;= end_
    count_ = end_ - start_ + 1
    sum_ = Int64(0)
    sofar_ = Int64(0)

    x0inv = cld(start_, x0)
    next_x0inv = x0inv * x0
    identity_n_low = x0*x0 - x0 + 1
    identity_n = x0*x0 + x0
    twice_first_next = x0 + x0inv
    first_next = twice_first_next &gt;&gt; 1
    limit = max(start_-1, min(identity_n_low-1, end_))
    if (first_next == x0 &amp;&amp; end_ &lt; next_x0inv)
        sum_ = 0
        check(start_, end_, x0, sum_, count_)
        return sum_, count_
    end
    @assert identity_n_low &lt;= identity_n

    function calc_child_sum(n, end_next, first_next)
        @assert end_next &gt;= n
        subsum_, subcount_ = my_calc(n, end_next, first_next)
        @assert subcount_ == end_next-n+1
        @assert subsum_ &gt;= 0
        subsum_ += subcount_
        check(n, end_next, x0, subsum_, subcount_)
        return subsum_, subcount_
    end

    n = start_
    function child_loop(start3, limit3)
        # @show start3, limit3
        n = start3
        while n &lt;= limit3
            if next_x0inv &lt; n
                next_x0inv += x0
                twice_first_next += 1
                first_next = twice_first_next &gt;&gt; 1
                @assert next_x0inv &gt;= n
                if twice_first_next &amp; 0b1 == 0
                    twice_first_next += 1
                    next_x0inv += x0
                end
            end

            end_next = next_x0inv
            if end_next &gt; limit3
                end_next = limit3
            end

            subsum_, subcount_ = calc_child_sum(n, end_next, first_next)
            sum_ += subsum_
            sofar_ += subcount_
            check(start_, end_next, x0, sum_, sofar_)
            n = end_next + 1
        end
    end

    child_loop(start_, limit)

    check(start_, limit, x0, sum_, sofar_)
    start2 = max(start_, min(end_, limit + 1))
    limit2 = max(start_, min(end_, identity_n))
    if limit2 &gt;= start2 &amp;&amp; start2 &gt;= limit + 1
        diff = limit2 - start2 + 1
        sofar_ += diff
        @assert sofar_ == limit2 - start_ + 1
        check(start_, limit2, x0, sum_, sofar_)
    end
    if limit2 != end_
        start3 = limit2 + 1
        while start3 &gt; next_x0inv
            next_x0inv += x0
            twice_first_next += 1
        end
        first_next = twice_first_next &gt;&gt; 1
        initsofar_ = sofar_


        child_loop(start3, end_)
    end

    @assert sum_ &gt;= 0
    @assert count_ == sofar_
    check(start_, end_, x0, sum_, sofar_)
    return sum_, count_
end

function digit_wrapper(dd)
    d = Int64(0+dd)
    start_ = Int64(10^(d-1))
    end_ = Int64(10*start_-1)
    if d % 2 == 0
        x0 = 7 * (10^((d-2)&gt;&gt;1))
    else
        x0 = 2 * (10^((d-1)&gt;&gt;1))
    end
    x0 = Int64(x0)
    sum_, count_ = my_calc(start_, end_, x0)
    sum_+=count_
    @assert count_ == end_ - start_ + 1
    return sum_, count_
end

function digit_average(dd)
    sum_, count_ = digit_wrapper(dd)
    @show dd, sum_, count_
    return sum_ / count_
end
ret = digit_average(5)
@show ret
@assert abs(ret - 3.2102888889) &lt; 0.0000001
if true
    ret = digit_average(14)
    @show ret
end"><pre class="notranslate"><span class="pl-k">const</span> CHECK <span class="pl-k">=</span> <span class="pl-c1">false</span>
<span class="pl-k">function</span> <span class="pl-en">brute_my_calc</span>(start_, end_, x0)
    sum_ <span class="pl-k">=</span> <span class="pl-c1">0</span>
    count_ <span class="pl-k">=</span> <span class="pl-c1">0</span>
    <span class="pl-k">for</span> n <span class="pl-k">in</span> (start_<span class="pl-k">:</span>end_)
        count_ <span class="pl-k">+=</span> <span class="pl-c1">1</span>
        prev <span class="pl-k">=</span> x0
        next_ <span class="pl-k">=</span> ((prev <span class="pl-k">+</span> <span class="pl-c1">cld</span>(n, prev)) <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>)
        <span class="pl-k">while</span> prev <span class="pl-k">!=</span> next_
            sum_ <span class="pl-k">+=</span> <span class="pl-c1">1</span>
            prev <span class="pl-k">=</span> next_
            next_ <span class="pl-k">=</span> ((prev <span class="pl-k">+</span> <span class="pl-c1">cld</span>(n, prev)) <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>)
        <span class="pl-k">end</span>
    <span class="pl-k">end</span>
    <span class="pl-k">return</span> sum_, count_
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">check</span>(start_, end_, x0, have_sum, have_count)
    <span class="pl-k">if</span> CHECK
        gs, gc <span class="pl-k">=</span> <span class="pl-c1">brute_my_calc</span>(start_, end_, x0)
        <span class="pl-c1">@assert</span> (have_sum, have_count) <span class="pl-k">==</span> (gs, gc)
    <span class="pl-k">end</span>
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">my_calc</span>(start_, end_, x0)
    <span class="pl-c1">@assert</span> start_ <span class="pl-k">&lt;=</span> end_
    count_ <span class="pl-k">=</span> end_ <span class="pl-k">-</span> start_ <span class="pl-k">+</span> <span class="pl-c1">1</span>
    sum_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">0</span>)
    sofar_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">0</span>)

    x0inv <span class="pl-k">=</span> <span class="pl-c1">cld</span>(start_, x0)
    next_x0inv <span class="pl-k">=</span> x0inv <span class="pl-k">*</span> x0
    identity_n_low <span class="pl-k">=</span> x0<span class="pl-k">*</span>x0 <span class="pl-k">-</span> x0 <span class="pl-k">+</span> <span class="pl-c1">1</span>
    identity_n <span class="pl-k">=</span> x0<span class="pl-k">*</span>x0 <span class="pl-k">+</span> x0
    twice_first_next <span class="pl-k">=</span> x0 <span class="pl-k">+</span> x0inv
    first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
    limit <span class="pl-k">=</span> <span class="pl-c1">max</span>(start_<span class="pl-k">-</span><span class="pl-c1">1</span>, <span class="pl-c1">min</span>(identity_n_low<span class="pl-k">-</span><span class="pl-c1">1</span>, end_))
    <span class="pl-k">if</span> (first_next <span class="pl-k">==</span> x0 <span class="pl-k">&amp;&amp;</span> end_ <span class="pl-k">&lt;</span> next_x0inv)
        sum_ <span class="pl-k">=</span> <span class="pl-c1">0</span>
        <span class="pl-c1">check</span>(start_, end_, x0, sum_, count_)
        <span class="pl-k">return</span> sum_, count_
    <span class="pl-k">end</span>
    <span class="pl-c1">@assert</span> identity_n_low <span class="pl-k">&lt;=</span> identity_n

    <span class="pl-k">function</span> <span class="pl-en">calc_child_sum</span>(n, end_next, first_next)
        <span class="pl-c1">@assert</span> end_next <span class="pl-k">&gt;=</span> n
        subsum_, subcount_ <span class="pl-k">=</span> <span class="pl-c1">my_calc</span>(n, end_next, first_next)
        <span class="pl-c1">@assert</span> subcount_ <span class="pl-k">==</span> end_next<span class="pl-k">-</span>n<span class="pl-k">+</span><span class="pl-c1">1</span>
        <span class="pl-c1">@assert</span> subsum_ <span class="pl-k">&gt;=</span> <span class="pl-c1">0</span>
        subsum_ <span class="pl-k">+=</span> subcount_
        <span class="pl-c1">check</span>(n, end_next, x0, subsum_, subcount_)
        <span class="pl-k">return</span> subsum_, subcount_
    <span class="pl-k">end</span>

    n <span class="pl-k">=</span> start_
    <span class="pl-k">function</span> <span class="pl-en">child_loop</span>(start3, limit3)
        <span class="pl-c"><span class="pl-c">#</span> @show start3, limit3</span>
        n <span class="pl-k">=</span> start3
        <span class="pl-k">while</span> n <span class="pl-k">&lt;=</span> limit3
            <span class="pl-k">if</span> next_x0inv <span class="pl-k">&lt;</span> n
                next_x0inv <span class="pl-k">+=</span> x0
                twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
                first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
                <span class="pl-c1">@assert</span> next_x0inv <span class="pl-k">&gt;=</span> n
                <span class="pl-k">if</span> twice_first_next <span class="pl-k">&amp;</span> <span class="pl-c1">0b1</span> <span class="pl-k">==</span> <span class="pl-c1">0</span>
                    twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
                    next_x0inv <span class="pl-k">+=</span> x0
                <span class="pl-k">end</span>
            <span class="pl-k">end</span>

            end_next <span class="pl-k">=</span> next_x0inv
            <span class="pl-k">if</span> end_next <span class="pl-k">&gt;</span> limit3
                end_next <span class="pl-k">=</span> limit3
            <span class="pl-k">end</span>

            subsum_, subcount_ <span class="pl-k">=</span> <span class="pl-c1">calc_child_sum</span>(n, end_next, first_next)
            sum_ <span class="pl-k">+=</span> subsum_
            sofar_ <span class="pl-k">+=</span> subcount_
            <span class="pl-c1">check</span>(start_, end_next, x0, sum_, sofar_)
            n <span class="pl-k">=</span> end_next <span class="pl-k">+</span> <span class="pl-c1">1</span>
        <span class="pl-k">end</span>
    <span class="pl-k">end</span>

    <span class="pl-c1">child_loop</span>(start_, limit)

    <span class="pl-c1">check</span>(start_, limit, x0, sum_, sofar_)
    start2 <span class="pl-k">=</span> <span class="pl-c1">max</span>(start_, <span class="pl-c1">min</span>(end_, limit <span class="pl-k">+</span> <span class="pl-c1">1</span>))
    limit2 <span class="pl-k">=</span> <span class="pl-c1">max</span>(start_, <span class="pl-c1">min</span>(end_, identity_n))
    <span class="pl-k">if</span> limit2 <span class="pl-k">&gt;=</span> start2 <span class="pl-k">&amp;&amp;</span> start2 <span class="pl-k">&gt;=</span> limit <span class="pl-k">+</span> <span class="pl-c1">1</span>
        diff <span class="pl-k">=</span> limit2 <span class="pl-k">-</span> start2 <span class="pl-k">+</span> <span class="pl-c1">1</span>
        sofar_ <span class="pl-k">+=</span> diff
        <span class="pl-c1">@assert</span> sofar_ <span class="pl-k">==</span> limit2 <span class="pl-k">-</span> start_ <span class="pl-k">+</span> <span class="pl-c1">1</span>
        <span class="pl-c1">check</span>(start_, limit2, x0, sum_, sofar_)
    <span class="pl-k">end</span>
    <span class="pl-k">if</span> limit2 <span class="pl-k">!=</span> end_
        start3 <span class="pl-k">=</span> limit2 <span class="pl-k">+</span> <span class="pl-c1">1</span>
        <span class="pl-k">while</span> start3 <span class="pl-k">&gt;</span> next_x0inv
            next_x0inv <span class="pl-k">+=</span> x0
            twice_first_next <span class="pl-k">+=</span> <span class="pl-c1">1</span>
        <span class="pl-k">end</span>
        first_next <span class="pl-k">=</span> twice_first_next <span class="pl-k">&gt;&gt;</span> <span class="pl-c1">1</span>
        initsofar_ <span class="pl-k">=</span> sofar_


        <span class="pl-c1">child_loop</span>(start3, end_)
    <span class="pl-k">end</span>

    <span class="pl-c1">@assert</span> sum_ <span class="pl-k">&gt;=</span> <span class="pl-c1">0</span>
    <span class="pl-c1">@assert</span> count_ <span class="pl-k">==</span> sofar_
    <span class="pl-c1">check</span>(start_, end_, x0, sum_, sofar_)
    <span class="pl-k">return</span> sum_, count_
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">digit_wrapper</span>(dd)
    d <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">0</span><span class="pl-k">+</span>dd)
    start_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">10</span><span class="pl-k">^</span>(d<span class="pl-k">-</span><span class="pl-c1">1</span>))
    end_ <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(<span class="pl-c1">10</span><span class="pl-k">*</span>start_<span class="pl-k">-</span><span class="pl-c1">1</span>)
    <span class="pl-k">if</span> d <span class="pl-k">%</span> <span class="pl-c1">2</span> <span class="pl-k">==</span> <span class="pl-c1">0</span>
        x0 <span class="pl-k">=</span> <span class="pl-c1">7</span> <span class="pl-k">*</span> (<span class="pl-c1">10</span><span class="pl-k">^</span>((d<span class="pl-k">-</span><span class="pl-c1">2</span>)<span class="pl-k">&gt;&gt;</span><span class="pl-c1">1</span>))
    <span class="pl-k">else</span>
        x0 <span class="pl-k">=</span> <span class="pl-c1">2</span> <span class="pl-k">*</span> (<span class="pl-c1">10</span><span class="pl-k">^</span>((d<span class="pl-k">-</span><span class="pl-c1">1</span>)<span class="pl-k">&gt;&gt;</span><span class="pl-c1">1</span>))
    <span class="pl-k">end</span>
    x0 <span class="pl-k">=</span> <span class="pl-c1">Int64</span>(x0)
    sum_, count_ <span class="pl-k">=</span> <span class="pl-c1">my_calc</span>(start_, end_, x0)
    sum_<span class="pl-k">+=</span>count_
    <span class="pl-c1">@assert</span> count_ <span class="pl-k">==</span> end_ <span class="pl-k">-</span> start_ <span class="pl-k">+</span> <span class="pl-c1">1</span>
    <span class="pl-k">return</span> sum_, count_
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">digit_average</span>(dd)
    sum_, count_ <span class="pl-k">=</span> <span class="pl-c1">digit_wrapper</span>(dd)
    <span class="pl-c1">@show</span> dd, sum_, count_
    <span class="pl-k">return</span> sum_ <span class="pl-k">/</span> count_
<span class="pl-k">end</span>
ret <span class="pl-k">=</span> <span class="pl-c1">digit_average</span>(<span class="pl-c1">5</span>)
<span class="pl-c1">@show</span> ret
<span class="pl-c1">@assert</span> <span class="pl-c1">abs</span>(ret <span class="pl-k">-</span> <span class="pl-c1">3.2102888889</span>) <span class="pl-k">&lt;</span> <span class="pl-c1">0.0000001</span>
<span class="pl-k">if</span> <span class="pl-c1">true</span>
    ret <span class="pl-k">=</span> <span class="pl-c1">digit_average</span>(<span class="pl-c1">14</span>)
    <span class="pl-c1">@show</span> ret
<span class="pl-k">end</span></pre></div>
<p dir="auto">I tried running both codes several times in succession (and got similar results).</p>
<p dir="auto">Thanks for any help or insights and let me know if I can prove of any further help.<br>
Julia seems useful and nice so far, so thanks for your past and future work.</p>
<p dir="auto">In Julia 0.6 I get:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; digits(0,2,0)
0-element Array{Int64,1}

julia&gt; digits(0,3,0)
1-element Array{Int64,1}:
 0"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">digits</span>(<span class="pl-c1">0</span>,<span class="pl-c1">2</span>,<span class="pl-c1">0</span>)
<span class="pl-c1">0</span><span class="pl-k">-</span>element Array{Int64,<span class="pl-c1">1</span>}

julia<span class="pl-k">&gt;</span> <span class="pl-c1">digits</span>(<span class="pl-c1">0</span>,<span class="pl-c1">3</span>,<span class="pl-c1">0</span>)
<span class="pl-c1">1</span><span class="pl-k">-</span>element Array{Int64,<span class="pl-c1">1</span>}<span class="pl-k">:</span>
 <span class="pl-c1">0</span></pre></div>
<p dir="auto">I think both should return a vector of length 0. This is the behavior in Julia 0.5 for all bases and in Julia 0.6 for the special cases where the base is 2, 8, 10, and 16. The source code for the digits function did not change between Julia 0.5 and 0.6, but the code for the ndigits0z function did change.</p>