Generate all pairs of factors N = p*q, p <= q, for positive integer N
Updated 29 Mar 2010

View License

I needed a tool that would generate all simple pairs of factors of a positive integer N, where N could be rather large. I also needed this task to be accomplished efficiently. So while I liked the nfactork submission that Matt Fig has posted, I found it to have severe problems with memory for some numbers. Therefore I had to write my own tool for the task that handles the case where exactly two factors are generated.

ans =
1 72
2 36
3 24
4 18
6 12
8 9

The factorpairs function is quite efficient for large N with many factors. It works for any value of N that does not exceed 2^32.

F = factorpairs(factorial(12));

Elapsed time is 0.003767 seconds.

There are 396 distinct pairs of factors generated here.

ans =
396 2

Cite As

John D'Errico (2024). factorpairs (https://www.mathworks.com/matlabcentral/fileexchange/27113-factorpairs), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Creating and Concatenating Matrices in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes