# integer factorization subroutine

Hello all,

I would like to write a subroutine to factor an integer into it's
prime factors. I think the best interface to have is something
like the following:

subroutine factor(n, factors, multiplicities)
integer(kind=i4b), intent(in) :: n
integer(kind=i4b), dimension(:), intent(out) :: factors
integer(kind=i4b), dimension(:), intent(out) :: multiplicities

There are some little problems however:

1) I do not know in advance how many prime factors I will have for a certain n.
And since i want to keep my code F-compliant, I cannot use allocatable
dummy arguments. So I will probably also need a function nb_prime_factors(n)
that returns me the number of prime factors for a certain n. I can then
allocate the factor and multiplicity arrays with the correct size and pass
them to my factor subroutine. What do you guys think of this solution?

2) Seems like prime factoring is a whole science in itself. What i want for
now is a *readable* and *quite simple* method. I can (try to) optimize
the algorithm later. Has anybody done this in Fortran before? What