Factors of an integer: Difference between revisions
Content added Content deleted
(→List comprehension: Factor, cofactor. Rearrange a list of tuples to a sorted list) |
(added Julia example) |
||
Line 1,160: | Line 1,160: | ||
factors(53); // [1,53] |
factors(53); // [1,53] |
||
factors(64); // [1,2,4,8,16,32,64]</lang> |
factors(64); // [1,2,4,8,16,32,64]</lang> |
||
=={{header|Julia}}== |
|||
<lang julia>function factors(p) |
|||
f = Array(typeof(p), 0) |
|||
n = one(p) |
|||
while n*n < p |
|||
if p % n == 0 |
|||
push!(f, n) |
|||
push!(f, div(p, n)) |
|||
end |
|||
n += one(p) |
|||
end |
|||
n*n == p && push!(f, n) |
|||
return sort!(f) |
|||
end</lang> |
|||
Example output: |
|||
<pre> |
|||
julia> factors(45) |
|||
6-element Array{Int64,1}: |
|||
1 |
|||
3 |
|||
5 |
|||
9 |
|||
15 |
|||
45 |
|||
</pre> |
|||
=={{header|K}}== |
=={{header|K}}== |