# Find the factors

I am writing a script that will allow me to find the factors of any number.
What I have come up with so far is as follows:

``````set a to "1356301320354343" as number

set b to "2" as number

set c to a mod b

if c â‰  0 then
repeat until b = a
set b to b + 1
set c to a mod b
if c = 0 then
log b
end if
end repeat
end if

``````

The problem that I’m having is when I use a number which has 17 or more digits. Can you help me tweak my code so that it will allow larger numbers to be used. Thanks in advance.
Josh

AppleScript can’t deal with very large integers; not the tool of choice.

I might add that there’s no reason to test any factor larger than half the original number, and there’s no reason to test any even number. Since the factors of an odd number must all be prime numbers, you really only need to test the primes that are less than half the original number to be factored.

You might see: Integer Factorization

To get the primes to try, you might read this Code Exchange article on the Sieve of Erastothenes

EDIT: You might look at the man page for the shell program bc which is an arbitrary precision calculator if you must handle very large numbers (>17 digits).