Find all factors of any integer with interactive demo and code examples
Factors are numbers we can multiply together to get another number. For example, factors of 12 are:
1 × 12 = 12
2 × 6 = 12
3 × 4 = 12
So the factors of 12 are 1, 2, 3, 4, 6, and 12.
This program finds all factors of a number entered by the user. We use the simple logic that a number is divisible by all its factors. In programming terms, if number % factor == 0, then the divisor is a factor.
Enter any positive integer to find its factors:
The algorithm checks divisibility from 1 to the square root of the number for efficiency. For each divisor found, both the divisor and the quotient are added to the factors list.
This VB6 code finds factors and displays them in a list box.
Private Sub cmdFindFactors_Click() ' Clear previous results lstFactors.Clear ' Get the number from text box Dim num As Long num = Val(txtNumber.Text) ' Always include 1 and the number itself lstFactors.AddItem "1" ' Find factors from 2 to square root of num Dim i As Long For i = 2 To Sqr(num) If num Mod i = 0 Then lstFactors.AddItem CStr(i) ' Add divisor ' Add quotient if it's different If i <> num \ i Then lstFactors.AddItem CStr(num \ i) End If End If Next i ' Add the number itself lstFactors.AddItem CStr(num) ' Sort the factors Call SortListBox(lstFactors) ' Show factor count lblCount.Caption = CStr(lstFactors.ListCount) & " factors found" End Sub
Modern VB.NET implementation with efficient algorithm.
Private Sub btnFindFactors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindFactors.Click ' Clear previous results lstFactors.Items.Clear() ' Parse input number Dim number As Long If Not Long.TryParse(txtNumber.Text, number) Or number < 1 Then MessageBox.Show("Please enter a valid positive integer") Return End If ' Create a list to store factors Dim factors As New List(Of Long) ' Add 1 and the number itself factors.Add(1) If number > 1 Then factors.Add(number) End If ' Find factors from 2 to square root of number Dim limit As Long = Math.Sqrt(number) For i As Long = 2 To limit If number Mod i = 0 Then factors.Add(i) ' Add divisor ' Add quotient if it's different Dim quotient As Long = number \ i If quotient <> i Then factors.Add(quotient) End If End If Next ' Sort and display factors factors.Sort() lstFactors.DataSource = factors lblCount.Text = $"{factors.Count} factors found" ' Check if prime If factors.Count = 2 Then lblPrime.Visible = True Else lblPrime.Visible = False End If End Sub
The most efficient way to find all factors of a number is to iterate only up to the square root of the number. This approach has O(√n) time complexity compared to O(n) for a naive approach.
If a number n has a factor i, then n/i is also a factor.
For n = 1,000,000, we only need 1,000 checks instead of 1,000,000.
1. Add 1 to factors
2. For i from 2 to √n:
- If n % i == 0, add i and n/i
3. Add n
Modify the factors finder to also determine if the number is prime. A prime number has exactly two distinct factors: 1 and itself.
Hint: After finding all factors, check if the count is 2.