#!/usr/bin/python

# factor.py
#       --copyright--                   Copyright 2007 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       July 2, 2004            bar
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       --eodstamps--
##      \file
#
#
#       Factor a number
#
#

import  math


__all__ = [
            "factor",
          ]


def afactor(n, r) :
    """
        Factor a number. Return an array of factors.
    """

    if  n <= 1 :    return

    if  long(n / 2) * 2 == n :
        r.append(2)
        afactor(long(n / 2), r)
        return

    for d in range(3, math.sqrt(n) + 1, 2) :
        if  long(n / d) * d == n :
            r.append(d)
            afactor(long(n / d), r)
            return
        pass

    r.append(n)

    return



def factor(n) :
    """
        Factor a number. Return an array of factors.
    """

    n = long(n)

    if  n < 0 :     n = abs(n)
    if  n == 0 :    return(None)

    r = []
    afactor(n, r)

    return(r)



#
#
#   Test code.
#
#
if __name__ == '__main__' :
    import  sys

    for i in range(1, len(sys.argv)) :
        print sys.argv[i], factor(sys.argv[i])

    pass

#
#
#
# eof

