#!/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
#       February 7, 2012        bar     get rid of a warning
#       May 27, 2012            bar     doxygen namespace
#       February 4, 2015        bar     finally, after all these years, replace the old Basic program to list primes
#       --eodstamps--
##      \file
#       \namespace              tzpython.factor
#
#
#       Factor a number
#
#       What is with this thing? Odd code.
#
#

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, long(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

    if  (len(sys.argv) == 3) and (long(sys.argv[2]) - long(sys.argv[1]) <= 10000) :
        frn = long(sys.argv[1])
        ton = long(sys.argv[2])
        print "Primes between", frn, "and", ton
        for i in xrange(frn, ton + 1) :
            r   = factor(i)
            if  (r in None) or (len(r) == 1) :
                print " ", i
            pass
        pass
    else :
        for i in range(1, len(sys.argv)) :
            print sys.argv[i], factor(sys.argv[i])
        pass
    pass

#
#
#
# eof
