Re: prime factorization


[Prev][Next][Index][Thread]

Re: prime factorization



Excerpts from netnews.bit.listserv.calc-ti: 1-Oct-97 prime factorization by
> Anyone have any code for prime factorization of an integer?

Here's my algorithm.  Works great for reasonably sized numbers (4 or 5
digits). It's for a ti85 so you may have to modify it slightly. The
output looks like this  {2 2 3 5}  for the number 60.


Input "number ",A
0->diml C
while 0==fpart (A/2       //this loop picks out all of the 2's
A/2->A
2->C(diml C+1
end
3->B
repeat B^2>A              //this loop checks all other odd numbers
if fpart (A/B:then
B+2->B
else
A/B->A
B->C(diml C+1
end:end
if A-1:A->C(diml C+1    //trap largest prime factor if it appears once
C


"C" must be the last line of the program to get nice output. If you
don't see any output from this program, use "disp C" or "pause C"
instead.

Note that if size is a concern, you can omit the while loop if you
change the start value of B to 2, and change the increment of B from 2
to 1. I just liked the idea of skipping even numbers....

hope this helps

-Jonathan




+---------------------------------------------+
| Jonathan Samuel     jsamuel+@andrew.cmu.edu |
| Electrical and Computer Engineering Student |
|     at Carnegie Mellon University           |
|                                             |
| QuickTime movie archive:                    |
|  http://jsamuel.res.cmu.edu/~jsamuel        |
| Web Page:                                   |
|  http://www.contrib.andrew.cmu.edu/~jsamuel |
+---------------------------------------------+


References: