# Fastest approach to returning a vector of elements choose 2

**URL:** <https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 21, 2024, 3:31am UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494 "2024-02-21T03:31:07Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2024, 2:05pm UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494/5 "2024-02-21T14:05:45Z")

</div>

a slightly different wording used in the [same post](https://discourse.julialang.org/t/iterating-over-elements-of-upper-triangular-matrix-but-cartesian-indices-are-needed/65498) @Dan is probably referring to

```julia
v=1:10
n=size(v)[1]
function n_over_2(v,idx)
    j = Int(ceil(sqrt(2 * idx + 0.25) - 0.5))+1
    i = Int(idx - (j-2) * (j-1) / 2)
    (v[i],v[j])
end

n_over_2.([v],1:n*(n-1)/2)

```

but perhaps it is less suitable for this case

---

_[View the full topic](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494)._
