# Finding all factors of an integer

**URL:** <https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841>\
**Category:** General Usage\
**Tags:** package\
**Created:** [October 22, 2020, 7:25pm UTC](https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841 "2020-10-22T19:25:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrXorile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drxorile/32/18881_2.png) [@DrXorile](https://discourse.julialang.org/u/DrXorile)\
**Post date:** [October 22, 2020, 7:25pm UTC](https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841/1 "2020-10-22T19:25:48Z")

</div>

I found [this question](https://discourse.julialang.org/t/iterator-over-variable-number-of-arguments/3558) but it didn’t quite answer mine.

I want to list all the factors of a number, n. I found Primes.factor(n) which returns all the primes with their powers. And I found Iterators.product which could, in principle, give all the factors.

In Python, there’s an \* operator that breaks out a list into separate inputs.

I created an array like this:

```julia
a = []
for p in Primes.factor(n)
    append!(a,[0:p[2])
end

```

Then I wanted to iterate over all combinations of these powers:

```julia
for x in Iterators.product(*a)
    #Do stuff with x being an array or tuple or list or whatever of powers
end

```

But I guess \* is a python thing! Anyway, general hints, tips, and good ideas for how to do this in Julia?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 22, 2020, 7:33pm UTC](https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841/2 "2020-10-22T19:33:49Z")

</div>

I’m not sure I understand your question correcty, but I _think_ you’re looking for [splatting](https://docs.julialang.org/en/v1/manual/faq/#The-two-uses-of-the-...-operator:-slurping-and-splatting):

```julia
Iterators.product(a...)

```

for example:

```julia
julia> a = [0:1, 0:2]
2-element Array{UnitRange{Int64},1}:
 0:1
 0:2

julia> collect(Iterators.product(a...))
2×3 Array{Tuple{Int64,Int64},2}:
 (0, 0) (0, 1) (0, 2)
 (1, 0) (1, 1) (1, 2)

```

---

<div class="post-metadata">

**Author:** ![DrXorile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drxorile/32/18881_2.png) [@DrXorile](https://discourse.julialang.org/u/DrXorile)\
**Post date:** [October 22, 2020, 8:08pm UTC](https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841/3 "2020-10-22T20:08:18Z")

</div>

I’m not sure why you’re using the collect? But the splat seems to do exactly what I was looking for. Thank you for your help.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 22, 2020, 8:30pm UTC](https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841/4 "2020-10-22T20:30:17Z")

</div>

Sorry the collect was just to show what’s in the iterator, clearly you don’t have to materialise for your use case!

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [October 23, 2020, 10:02am UTC](https://discourse.julialang.org/t/finding-all-factors-of-an-integer/48841/5 "2020-10-23T10:02:32Z")

</div>

you might also be interrested in these methods for `factor`:

```julia
julia> factor(Set, 12)
Set{Int64} with 2 elements:
  2
  3

julia> factor(Vector, 12)
3-element Array{Int64,1}:
 2
 2
 3

julia> factor(Dict, 12)
Dict{Int64,Int64} with 2 entries:
  2 => 2
  3 => 1

```
