8

How can I quickly find all factors of a number?

e.g.:

digit: 20
factors: {1*20, 2*10, 4*5, 5*4, 10*2, 20*1}

fronthem
  • 3,617
  • 5
  • 26
  • 49

7 Answers7

4

This is actually a problem for which no good solution is known. For this reason, RSA encryption actually depends on the computational difficulty of factoring numbers. See: Integer Factorization

However, you may be able to speed up the algorithms already given by only looking at numbers up to the square root of n, and checking if they are factors by checking if n % i == 0. If this is true, you can find the corresponding factor greater than n^(.5) by taking n / i.

Patrick
  • 1,128
  • 7
  • 13
3

If the number that you want to find the factors for happens to be odd, you only need to test odd numbers because it is impossible to have an even factor for an odd number. So with a pre-check up front, you can save yourself some processing.

private static List<Integer> findFactors(int num)
{
    int incrementer = 1;
    if (num % 2 != 0)
    {
        incrementer = 2; //only test the odd ones
    }
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 1; i <= num / 2; i=i+incrementer)
    {
        if (num % i == 0)
        {
            list.add(i);
        }
    }
    list.add(num);
    return list;
}
Tot Zam
  • 7,081
  • 9
  • 45
  • 66
Devon
  • 31
  • 2
  • If i wanted to return an array, or do it without lists entirely, how would i do it? – D. Ben Knoble Jan 15 '15 at 20:34
  • I've incorporated the idea of determining the incrementer based on whether the number is even or odd, in a faster solution I provided [here](https://stackoverflow.com/a/45789083/4660897), on a duplicate question. – Tot Zam Aug 21 '17 at 04:06
2

Go through a loop applying modulus to all of the intermediate numbers.

X=1;
WHILE(X<=20)
 IF 20%x == 0
 THEN FACTOR!
 X++;
END
Edgar Velasquez Lim
  • 2,296
  • 14
  • 15
0

You probably wish to go for the modulus operator( % ).

E.g

import java.util.Scanner;

public class Factor {
     public static void main(String[] args) {
     System.out.println("Enter a number whose factors are to be calculated: ");
     Scanner scanNum = new Scanner(System.in);
     int numFac = 0;
     if(scanNum.hasNextInt())  {
        numFac = scanNum.nextInt();
       }

     System.out.println("The Factors of the entered number are:-");

     for(int i = 1; i <= numFac; i++)   {
        if(numFac%i == 0)   {
            System.out.print(i+" ");
          }
     }
   }
  }
sgokhales
  • 49,762
  • 33
  • 123
  • 158
0
public static Integer[] findFactors(int d)
{
    List<Integer> list = new ArrayList<Integer>();
    for(int i = 1; i <= d/2; i++)
    {
        if(d % i == 0) list.add(new Integer(i));
    }
    list.add(new Integer(d));
    return (Integer[]) list.toArray(new Integer[0]);
}

public static void main(String[] args)
{
    Integer[] list = findFactors(20);
    for(Integer i : list) System.out.println(i);
}

Output:

1
2
4
5
10
20
Eng.Fouad
  • 107,075
  • 62
  • 298
  • 390
0
List<Integer> factors = new ArrayList<Integer>();
for (int i = 1; i < NUMBER; i++) {
    if (NUMBER % i == 0) {
        factors.add(i);
    }
}
Nico Huysamen
  • 9,844
  • 9
  • 54
  • 81
0
public class FactorGenerator{      
  private int number;       
  private int i;           
  public FactorGenerator(int numberToFactor){      
    number = numberToFactor;   
  }

  public int nextFactor(){
     while(number % i == 0){
        System.out.print((Math.round(i)) + " "); 
        number =((number / i));
        return i;     
     }     
     return i;     
 }     

 public boolean hasMoreFactors(){      
   for ( i = 2; i <= number; i++){      
     nextFactor(); 
   }
  return false;      
 }      

}

Test Program:      
import java.util.Scanner;        
 public class FactorgeneratorTester{          
   public static void main (String [] args){           
       Scanner in= new Scanner(System.in);         
       System.out.println("input the value");       
       int number = in.nextInt();       
       FactorGenerator fg = new FactorGenerator(number);             
           if (fg.hasMoreFactors()){      
             System.out.println(fg.hasMoreFactors());      
           }            
   }               

}

the input
210
output
2 3 5 7

deepakl.2000
  • 155
  • 1
  • 1
  • 12