| Prime Factorization Algorithm |
Article Index for Prime |
Website Links For Prime |
Information AboutPrime Factorization Algorithm |
|
A SIMPLE FACTORIZATION ALGORITHM Description We can describe a Recursive algorithm to perform such factorizations: given a number ''n''
Note that we need to test only primes ''p''''i'' such that . Example :Suppose we wish to factorize the number 9438. :9438/2 = 4719 with a remainder of 0, so 2 is a factor of 9438. ''We repeat the algorithm with 4719.'' :4719/2 = 2359 with a remainder of 1, so 2 is NOT a factor of 4719. ''We try the next prime, 3.'' :4719/3 = 1573 with a remainder of 0, so 3 is a factor of 4719. ''We repeat the algorithm with 1573.'' :1573/3 = 524 with a remainder of 1, so 3 is NOT a factor of 1573. ''We try the next prime, 5.'' :1573/5 = 314 with a remainder of 3, so 5 is NOT a factor of 1573. ''We try the next prime, 7.'' :1573/7 = 224 with a remainder of 5, so 7 is NOT a factor of 1573. ''We try the next prime, 11.'' :1573/11 = 143 with a remainder of 0, so 11 is a factor of 1573. ''We repeat the algorithm with 143.'' :143/11 = 13 with a remainder of 0, so 11 is a factor of 143. ''We repeat the algorithm with 13.'' :13/11 = 1 with a remainder of 2, so 11 is NOT a factor of 13. ''We try the next prime, 13.'' :13/13 = 1 with a remainder of 0, so 13 is a factor of 13. ''We stop when we reached 1.'' Thus working from top to bottom, we have 9438 = 2 × 3 × 11 × 11 × 13. Code Here is some code in Python for finding the factors of numbers less than 2147483647:
output: python factorize.py 9438 Here is more complex code in Python for finding the factors of any arbitrarily large number:
for d in range(9,-1,-1):
if x <= currvalue: remainder= currvalue - x
return(results,remainder) else: (results,remainder)=intsqrt_core(digitpair//100,remainder,results) (results,remainder)=intsqrt_core(digitpair%100,remainder,results) return(results,remainder) (results,remainder)=intsqrt_core(n,0,0) return results def isPrime(n): """ Return True if n is a prime """ if DictPrime.has_key(n): return True high=intsqrt(n) for x in ListOfPrimes: if x <= high and n%x == 0: return False if x >= high: return True x=maxprimeinlist + 2 while x<=high: if n%x == 0: return False x += 2 return True def factorize(n): """ Factorize an integer number """ primes = {Link without Title} index=0 candidate = ListOfPrimes {Link without Title} while not primes and candidate <= n: if n%candidate == 0 and (index < maxindex or isPrime(candidate)): primes = primes + {Link without Title} + factorize(n//candidate) index += 1 if index < maxindex: candidate = ListOfPrimes {Link without Title} else: candidate += 2 return primes def condense(L): """ Condense result in list to prime^nth_power format """ prime,count,list=0,0, {Link without Title} for x in L: if x == prime: count += 1 else: if prime != 0: list = list + + '^' + str(count) prime,count=x,1 list = list + + '^' + str(count) return list if __name__ == '__main__': print condense(factorize(long(sys.argv {Link without Title} ))) # Sample output # # python factorize.py 173248246132375748867198458668657948626531982421875 # '5^14', '7^33', '13^1' Time complexity The algorithm described above works fine for small ''n'', but becomes impractical as ''n'' gets larger. For example, for an 18- Digit (or 60 Bit ) number, all primes below about 1,000,000,000 may need to be tested, which is taxing even for a computer. Adding two decimal digits to the original number will multiply the computation time by 10. The difficulty (large time complexity) of factorization makes it a suitable basis for modern Cryptography . SEE ALSO EXTERNAL LINKS
|
|
|