简体   繁体   中英

Finding all the factors of a number in Java?

"Write a program that reads an integer I and displays all its smallest factors in increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.". At the beginning of the program, the user has to enter an integer identifying how many numbers will be factorized.

import java.util.Scanner;

public class Main {

    public static void main(String [] args){

        Scanner input = new Scanner(System.in);

        int size = input.nextInt();

        for(int i = 0; i < size; i++){

            int a = input.nextInt();

            for(int j = 0; j < a; j++){
                if(a%j==0){
                    System.out.println(j);
                }
            }

        }
        input.close();

    }

}

A Better way of finding all the factors is to find the factors till it's square root.

int n = 120;

for(int i = 2; i * i <= n; ++i)//check below it's square root i <= sqrt(n)
 if(n % i == 0){ 
  while(n % i == 0)
  {
  System.out.println(i);
  n /= i;
  }
 }

A much more effective way is to do it with primes.

There cannot be any other prime factor which is even other than 2 so we can skip the even part

int n = 120;

if(n % 2 == 0)
{
 while(n % 2 == 0)
 {
    System.out.println("2");
    n /= 2;
 }
}
for(int i = 3; i * i <= n; i += 2)//odd numbers only
{
 while(n % i == 0)
 {
    n /= i;
    System.out.println(i);
 }
}

A much more efficient way is to use 6*k +- 1 rule,

What is 6*k +- 1 rule?

All prime numbers(except 2 and 3) can be represented by the above formula. Though the reverse might not be true, Consider 6*6 - 1 = 35 divisible by 5.

If it is not a prime, it will have a prime factor less than it's square root.

So we check only for the numbers which follow the above rule.

int i = 1, n = 120;
//check for 2 and 3
if(n % 2 == 0)
{
   while(n % 2 == 0)
   {
      System.out.println("2");
     n /= 2;
   }
}
if(n % 3 == 0)
{
   while(n % 3 == 0)
   {
      System.out.println("3");
      n /= 3;
   }
}
while(true)
{
   int p = 6 * i - 1;
   if(p * p > n)
      break;
   if(n % p == 0)
   {
      while( n % p == 0)
      {
        n /= i;
        System.out.println(p);
      }
   }
 p = 6 * k + 1;
 if(p * p > n)
  break;
 if(n % p == 0)
 {
   while( n % p == 0)
   {
     n /= i;
     System.out.println(p);
   }
 }
}

If the numbers are very huge and there are alot of them, Pre-calculate primes can be helpful

I use Sieve to calculate the primes.

int max = 10000007;
boolean[]primes = new boolean[max];
int []nums = new int[max];
int numOfPrimes = 0;

for(int i = 2; i * i < max; ++i)
 if(!primes[i])
 for(int j = i * i; j < max; j += i)//sieve
  primes[j] = true;
for(int i = 2; i < max; ++i)
 if(!primes[i])
  nums[numOfPrimes++] = i;//we have all the primes now.

int n = 120;

for(int i = 0; i < numOfPrimes; ++i)
{
  int p = nums[i];
  if(p * p > n)
   break;
  if(n % p == 0)
  {
   while(n % p == 0)
   {
    n /= p;
    System.out.println(p);
    }
  }
}

You should divide the number:

for(int j = 2; j < a; j++){ // start dividing from 2
    if(a%j==0){
        System.out.println(j);
        a/=j; // divide a with j (there is remainder 0 because of condition)
        j--; // do j once more
    }
}

Try this one:

package bölüm05;

import java.util.Scanner;

public class B05S16 {

    public static void main(String[] args) {

        Scanner java = new Scanner(System.in);
        System.out.println("Bir tamsayı giriniz");
        int sayı = java.nextInt();
        int i = 2;

        while (sayı > 1) {
            if (sayı % i == 0) {
                sayı = sayı / i;
                System.out.print(i + ",");
            } else {
                i++;
            }
        }
        java.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM