Factors of an integer: Difference between revisions

Content added Content deleted
Line 2,357: Line 2,357:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
{{incorrect|Julia|factor not defined}}

<lang julia>function factors(n)
function factors(n)
f = [one(n)]
f = [one(n)]
for (p,e) in factor(n)
for (p,e) in factor(n)
f = reduce(vcat, f, [f*p^j for j in 1:e])
f = reduce(vcat, [f*p^j for j in 1:e], init=f)
end
end
return length(f) == 1 ? [one(n), n] : sort!(f)
return length(f) == 1 ? [one(n), n] : sort!(f)
end</lang>
end

const examples = [28, 45, 53, 64, 6435789435768]


for n in examples
@time println("The factors of $n are: $(factors(n))")
end
</lang>
{{out}}
{{out}}
<pre>
<pre>
The factors of 28 are: [1, 2, 4, 7, 14, 28]
julia> factors(45)
0.330684 seconds (784.75 k allocations: 39.104 MiB, 3.17% gc time)
6-element Array{Int64,1}:
The factors of 45 are: [1, 3, 5, 9, 15, 45]
1
0.000117 seconds (56 allocations: 2.672 KiB)
3
The factors of 53 are: [1, 53]
5
0.000102 seconds (35 allocations: 1.516 KiB)
9
The factors of 64 are: [1, 2, 4, 8, 16, 32, 64]
15
0.000093 seconds (56 allocations: 3.172 KiB)
45
The factors of 6435789435768 are: [1, 2, 3, 4, 6, 7, 8, 11, 12, 14, 21, 22, 24, 28,
33, 42, 44, 56, 66, 77, 84, 88, 132, 154, 168, 191, 231, 264, 308, 382, 462, 573,
616, 764, 924, 1146, 1337, 1528, 1848, 2101, 2292, 2674, 4011, 4202, 4584, 5348,
6303, 8022, 8404, 10696, 12606, 14707, 16044, 16808, 25212, 29414, 32088, 44121,
50424, 58828, 88242, 117656, 176484, 352968, 18233351, 36466702, 54700053, 72933404,
109400106, 127633457, 145866808, 200566861, 218800212, 255266914, 382900371,
401133722, 437600424, 510533828, 601700583, 765800742, 802267444, 1021067656,
1203401166, 1403968027, 1531601484, 1604534888, 2406802332, 2807936054, 3063202968,
3482570041, 4211904081, 4813604664, 5615872108, 6965140082, 8423808162, 10447710123,
11231744216, 13930280164, 16847616324, 20895420246, 24377990287, 27860560328,
33695232648, 38308270451, 41790840492, 48755980574, 73133970861, 76616540902,
83581680984, 97511961148, 114924811353, 146267941722, 153233081804, 195023922296,
229849622706, 268157893157, 292535883444, 306466163608, 459699245412, 536315786314,
585071766888, 804473679471, 919398490824, 1072631572628, 1608947358942, 2145263145256,
3217894717884, 6435789435768]
0.000249 seconds (451 allocations: 24.813 KiB)
</pre>
</pre>