Code covered by the BSD License  

Highlights from
myfactor

3.0

3.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 2.24 KB File ID: #20059

myfactor

by John T. McCarthy

 

26 May 2008 (Updated 29 May 2008)

This function finds the factors of very large numbers (up to 10^14)

| Watch this File

File Information
Description

To find the prime factors of any number up to 2^32 (about 4.3*10^9), you can use MATLAB's built-in function factor.m

For integers greater than 2^32, try this program, which guarantees a result up to 10^14, thus extending the domain of inputs 23,000-fold.

(This file is an adaptation of MATLAB's function primes.m)

Example:

myfactor(2^32+1)
ans = [1 641 6700417]

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
28 May 2008 John D'Errico

Yes, this does extend the factor code to a larger range.

There are a few problems with the code. It is a little sloppy, generating a few variables for no purpose at all. For example, quotient and result are two variables that are set in the code, but then never used again. Look at the results of mlint on your code. It will streamline your code, but also maybe find some bugs too.

Myfactor also lacks an H1 line. This is nice when you need to use lookfor. Perhaps 6 months from now, when you cannot remember the name of this function, nor why somebody called it myfactor. lookfor will help you, but ONLY if the author bothers to write a descriptive first comment line with good keywords in it.

More irritating, myfactor ONLY runs for certain inputs. If you call it with a small number, it returns only the irritating message

myfactor(123456)
 
For numbers up to 2^32, use MATLAB's built-in function factor.m

Yes, you must first test your number to see if it is smaller than 2^32-1, otherwise myfactor issues this snotty remark. Would it really have been that difficult to just call the Matlab factor instead? Or simpler, just remove this silly test completely? If you do so, the code actually works. So why be so stubborn and obnoxious?

Next, I did remove the offending, irritating test, so that I could compare this code to factor. Factor was generally faster, by about 10-20% for most random inputs that I tried, but there were some cases where myfactor was the faster of the two.

My irritation almost drove me to rate this a 2. Perhaps this deserves a 2.5 rating. If you modify the code yourself to make it useable, I suppose that brings it up to 3.

Please login to add a comment or rating.
Updates
28 May 2008

Add more keywords

29 May 2008

Attempt to improve presentation, following John D'Errico's review.

Tag Activity for this File
Tag Applied By Date/Time
factor John T. McCarthy 22 Oct 2008 10:03:40
factors John T. McCarthy 22 Oct 2008 10:03:40
factorize John T. McCarthy 22 Oct 2008 10:03:40
prime factor John T. McCarthy 22 Oct 2008 10:03:40
prime factors John T. McCarthy 22 Oct 2008 10:03:40
large number John T. McCarthy 22 Oct 2008 10:03:40

Contact us at files@mathworks.com