# Finding a vector which has the sum of the indices which are prime numbers

**URL:** <https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492>\
**Category:** Optimization (Mathematical)\
**Tags:** question, indexing, matrices\
**Created:** [September 29, 2020, 4:15pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492 "2020-09-29T16:15:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![julianoob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julianoob/32/8590_2.png) [@julianoob](https://discourse.julialang.org/u/julianoob)\
**Post date:** [September 29, 2020, 4:15pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/1 "2020-09-29T16:15:35Z")

</div>

For example a=[1 2 3; 4 5 6; 7 8 9] indices are ((1,1) ,(1,2) , (1,3); (2,1),(2,2),(2,3); (3,1),(3,2),(3,3)) and the prime number of these sums are 2,3,5 which should be in the vector s. Is there any function in julia which returns indices besides findmin and findmax? it should also be solved without for and if loops.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 29, 2020, 4:20pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/2 "2020-09-29T16:20:39Z")

</div>

Just some hint:  
`findall` takes a function, which, if returns `true` for an element of an array, returns the indices of all these element.

```julia
findall(f::Function, A)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 29, 2020, 4:49pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/3 "2020-09-29T16:49:04Z")

</div>

One alternative is to look into `CartesianIndices` in combination with logical indexing.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 29, 2020, 6:00pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/4 "2020-09-29T18:00:07Z")

</div>

For me the problem is not well defined yet. The sum of the indices or of the values (if the indices, the values have any relevance here?), if the indices are Cartesian indexes as it seems to be the case, how do you sum them? The final result is a 2-tuple of summed values or a single value?

---

<div class="post-metadata">

**Author:** ![julianoob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julianoob/32/8590_2.png) [@julianoob](https://discourse.julialang.org/u/julianoob)\
**Post date:** [September 29, 2020, 6:55pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/5 "2020-09-29T18:55:08Z")

</div>

sum of indices, i thnik the value is important beacuse u have to return it as a vector

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 29, 2020, 7:04pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/6 "2020-09-29T19:04:43Z")

</div>

I’m pretty sure the problem is well-defined. But I don’t want to post a solution because it appears to be an exam question.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 30, 2020, 9:53am UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/7 "2020-09-30T09:53:34Z")

</div>

I understand the exam is over. The approach here is to use `CartesianIndices`, and sum them pairwise. In order to do that, you have to convert them to tuples. So

```julia
julia> using Primes

julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
 1 2 3
 4 5 6
 7 8 9

julia> CartesianIndices(A)
3×3 CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
 CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(1, 3)
 CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 3)
 CartesianIndex(3, 1) CartesianIndex(3, 2) CartesianIndex(3, 3)

julia> Tuple.(CartesianIndices(A))
3×3 Array{Tuple{Int64,Int64},2}:
 (1, 1) (1, 2) (1, 3)
 (2, 1) (2, 2) (2, 3)
 (3, 1) (3, 2) (3, 3)

julia> sum.(Tuple.(CartesianIndices(A)))
3×3 Array{Int64,2}:
 2 3 4
 3 4 5
 4 5 6

julia> isprime.(sum.(Tuple.(CartesianIndices(A))))
3×3 BitArray{2}:
 1 1 0
 1 0 1
 0 1 0

```

This last bitarray can be used for logical indexing. So we make a few alternatives:

```julia
foo(A) = A[isprime.(sum.(Tuple.(CartesianIndices(A))))]
bar(A) = A[findall(isprime∘sum∘Tuple, CartesianIndices(A))]
baz(A) = [A[i] for i in CartesianIndices(A) if isprime(sum(Tuple(i)))]

```

In my tests, `foo` is the fastest, followed closely by `baz`, while `bar` is _by far_ the slowest.

Some may find this more readable:

```julia
A[CartesianIndices(A) .|> Tuple .|> sum .|> isprime]

```

or

```julia
A |> CartesianIndices .|> Tuple .|> sum .|> isprime |> (x->A[x])

```

---

<div class="post-metadata">

**Author:** ![julianoob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julianoob/32/8590_2.png) [@julianoob](https://discourse.julialang.org/u/julianoob)\
**Post date:** [September 30, 2020, 6:28pm UTC](https://discourse.julialang.org/t/finding-a-vector-which-has-the-sum-of-the-indices-which-are-prime-numbers/47492/8 "2020-09-30T18:28:56Z")

</div>

thank you very much
