# Broadcasting \*= and the like

**URL:** <https://discourse.julialang.org/t/broadcasting-and-the-like/41702>\
**Category:** General Usage\
**Created:** [June 19, 2020, 12:00am UTC](https://discourse.julialang.org/t/broadcasting-and-the-like/41702 "2020-06-19T00:00:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![binarygcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@binarygcd](https://discourse.julialang.org/u/binarygcd)\
**Post date:** [June 19, 2020, 12:00am UTC](https://discourse.julialang.org/t/broadcasting-and-the-like/41702/1 "2020-06-19T00:00:59Z")

</div>

I tried using broadcast for computing the radicals of integers 1 to limit. But it was significantly slower than the for loop version. Am I doing something wrong? I tried map! but I’m not sure if I got it right so please include that as well (and any other not so complicated alternative).

```julia
function getradicals(limit)
    radicals = ones(Int, limit)

    for i ∈ Iterators.filter(x -> isone(radicals[x]), 2:length(radicals))
        radicals[i:i:end] .*= i
        #for j ∈ i:i:length(radicals)
        # radicals[j] *= i
        #end
    end

    radicals
end

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [June 19, 2020, 2:12am UTC](https://discourse.julialang.org/t/broadcasting-and-the-like/41702/2 "2020-06-19T02:12:26Z")

</div>

Broadcasting `*=` doesn’t seem to be a problem, but you’re allocating a temporary array at each iteration `i` when saying `radicals[i:i:end]`. You can avoid this by either using `@views` or the loop you’ve commented out.

```julia
julia> using BenchmarkTools

julia> function getradicals1(limit)
           radicals = ones(Int64, limit)

           for i ∈ Iterators.filter(x -> isone(radicals[x]), 2:length(radicals))
               radicals[i:i:end] .*= i
               #for j ∈ i:i:length(radicals)
               # radicals[j] *= i
               #end
           end

           radicals
       end
getradicals1 (generic function with 1 method)

julia> @btime getradicals1(100);
  1.560 μs (26 allocations: 4.33 KiB)

julia> function getradicals2(limit)
           radicals = ones(Int64, limit)

           for i ∈ Iterators.filter(x -> isone(radicals[x]), 2:length(radicals))
               @views radicals[i:i:end] .*= i
               #for j ∈ i:i:length(radicals)
               # radicals[j] *= i
               #end
           end

           radicals
       end
getradicals2 (generic function with 1 method)

julia> @btime getradicals2(100);
  824.390 ns (1 allocation: 896 bytes)

julia> function getradicals3(limit)
           radicals = ones(Int64, limit)

           for i ∈ Iterators.filter(x -> isone(radicals[x]), 2:length(radicals))
               #radicals[i:i:end] .*= i
               for j ∈ i:i:length(radicals)
                   radicals[j] *= i
               end
           end

           radicals
       end
getradicals3 (generic function with 1 method)

julia> @btime getradicals3(100);
  414.065 ns (1 allocation: 896 bytes)

```

---

<div class="post-metadata">

**Author:** ![binarygcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@binarygcd](https://discourse.julialang.org/u/binarygcd)\
**Post date:** [June 19, 2020, 5:05am UTC](https://discourse.julialang.org/t/broadcasting-and-the-like/41702/3 "2020-06-19T05:05:27Z")

</div>

I read somewhere that a += b is converted to a = a + b. I only realized it after using @views. Thanks!  
Additional but related question. How would I use broadcasting for something like

```julia
radicals[j] -= radicals[j] \div i

```

instead of

```julia
radicals[j] *= i

```

in the inner loop?

---

<div class="post-metadata">

**Author:** ![binarygcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@binarygcd](https://discourse.julialang.org/u/binarygcd)\
**Post date:** [June 19, 2020, 6:54am UTC](https://discourse.julialang.org/t/broadcasting-and-the-like/41702/4 "2020-06-19T06:54:07Z")

</div>

This seems to work

```julia
@. @views radicals[i:i:end] -= radicals[i:i:end] \div i

```
