#!/usr/local/bin/ruby
def div_while_possible(result, n, p)
quot,rem = n.divmod(p)
while rem==0 do
result.push p
n = quot
quot,rem = n.divmod(p)
end
n
end
n = ARGV.shift.to_i
while n<=0 do
STDERR.print "Type a POSITIVE integer:"
n = STDIN.gets.to_i
end
print "factorization of #{n}\n"
limit = Math::sqrt(n).to_i
result = []
n = div_while_possible(result, n, 2)
n = div_while_possible(result, n, 3)
p=5; q=7
while n!=1 do
if p>limit then
result.push n
break
end
n = div_while_possible(result, n, p)
n = div_while_possible(result, n, q)
p+=6; q+=6
end
p result
# Copyright (C) 2002 Yuuki
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA