繁体   English   中英

查找主要因素,包括其总发生率(Java)

[英]Find prime factors including their total occurrence (Java)

我试图构建一个Java程序来查找'N'数字的LCM。 但首先,我坚持寻找包括其出现在内的数量的总主要因子。 例如(6 = 2x3)和(8 = 2x2x2)。 但我得到的输出是(6)为'2',而(8)只有两个'2'。 另一个在哪里? 我什至在检查整数's'为素数。

package lcm;

import java.util.ArrayList;
import java.util.Scanner;

public class LCM {
  public static boolean isPrime(int numero){
    for (int i = 2; i <= Math.sqrt(numero); i++) {
        if (numero % i == 0) {
            return false;
        }
    }
     return true;
  }
  public static  void factor(int x){    
      int s;
      int copy = x;
      ArrayList<Integer> al = new ArrayList<>();  
      for(s=2;s<copy;s++){
          if(copy%s==0){
              if (isPrime(s)){ 
                  al.add(s);
                  copy/=s;
                  //used for repetition
                  s--;
               }
          } 
       }
       for( int p : al){   
         System.out.println(p);    
       }
   }
public static void main(String[] args) {
    // TODO code application logic here
    int j,k;
    int temp=0;
    System.out.println("Enter no. of numbers");
    Scanner cin = new Scanner(System.in);
    int i = cin.nextInt();
    int []a = new int[i];
    int []b=new int[100];
    System.out.println("Enter numbers one by one");

    for(j=0;j<a.length;j++){ 
        a[j] = cin.nextInt();
    }
    for(j=0;j<a.length;j++){   
        temp=a[j];
        factor(temp);
    }
   }
}

原因是当s = 2且copy在该情况下也变为2时,它跳过了循环,因此仅显示两个2。 尝试在该位置放置<= copy

我认为解决此问题的最佳方法之一是使用递归,因为您必须不断地划分才能找到所有主要因素。

 package leastcommonmultiple; import java.util.ArrayList; import java.util.Scanner; public class Runner { private static LeastCommonMultiple lcm; /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Enter numbers and separate them with a comma: "); Scanner cin = new Scanner(System.in); String[] inputs; String lineInput; try { lineInput = cin.nextLine(); while (lineInput.isEmpty()) { System.out.println("Enter at least 2 numbers separated by a comma "); lineInput = cin.nextLine(); } inputs = lineInput.split(","); int length = inputs.length; int[] numbers = new int[length]; int temp = 0; for (int i = 0; i < length; i++) { if (inputs[i] != null && !inputs[i].isEmpty()) { if ((temp = Math.abs(Integer.valueOf(inputs[i].trim()))) == 0) { throw new IllegalArgumentException("No number should be 0"); } numbers[i] = temp; } } ArrayList<Integer> list = new ArrayList<>(); lcm = new LeastCommonMultiple(); System.out.println("The Least Common Multiple is: " + lcm.getLeastCommonMultipleOfListOfNumbers(numbers)); } catch (Exception e) { System.err.println(e.getMessage()); } } } package leastcommonmultiple; import java.util.ArrayList; public class LeastCommonMultiple { public LeastCommonMultiple() { } /** * @param numbers array of numbers whose LCM should be found. * @assure numbers.length > 1 * @return LCM of numbers contained in numbers array */ public int getLeastCommonMultipleOfListOfNumbers(int[] numbers) throws IllegalArgumentException { int leastCommonMultiple; int length = numbers.length; if ( length <= 1) { throw new IllegalArgumentException("Please enter at least 2 numbers separated by a comma."); } else { leastCommonMultiple = getLeastCommonMultipleOfTwoNumbers(numbers[0], numbers[length-1]); length = length-1; for ( int i=1; i<length; i++ ) { leastCommonMultiple = getLeastCommonMultipleOfTwoNumbers(leastCommonMultiple, numbers[i]); } } return leastCommonMultiple; } private int getLeastCommonMultipleOfTwoNumbers(int number1, int number2) { int leastCommonMultiple = 0; int maxOfTheTwoNumbers = Math.max(number1, number2); int minOfTheTwoNumbers = Math.min(number1, number2); int quotient = 0; if (number1 % number2 == 0 || number2 % number1 == 0) { leastCommonMultiple = maxOfTheTwoNumbers; } else { ArrayList<Integer> primeFactors = getPrimeFactors(minOfTheTwoNumbers, new ArrayList<>()); for (int primeFactor : primeFactors) { if (maxOfTheTwoNumbers % primeFactor == 0) { maxOfTheTwoNumbers = (maxOfTheTwoNumbers / primeFactor); } leastCommonMultiple = minOfTheTwoNumbers * maxOfTheTwoNumbers; } } return leastCommonMultiple; } // recursive methods that finds all the prime factors for a given number /** * @param numero positive number whose prime factors has to be found * @param primeFactors an empty ArrayList where the prime factors will be * stored * @return the ArrayList containing the found prime factors */ private static ArrayList<Integer> getPrimeFactors(int numero, ArrayList<Integer> primeFactors) { int sqrt = (int) Math.sqrt(numero); int quotient; if (isPrime(numero)) { primeFactors.add(numero); } else { if (numero % sqrt == 0) { quotient = numero / sqrt; if (isPrime(sqrt)) { primeFactors.add(sqrt); } else { primeFactors = getPrimeFactors(sqrt, primeFactors); } } else { quotient = numero / (sqrt - 1); if (isPrime(sqrt - 1)) { primeFactors.add(sqrt - 1); } else { primeFactors = getPrimeFactors((sqrt - 1), primeFactors); } } if (!isPrime(quotient)) { primeFactors = getPrimeFactors(quotient, primeFactors); } else { primeFactors.add(quotient); } } return primeFactors; } // Make sure a number is prime public static boolean isPrime(int numero) { int length = (int) Math.sqrt(numero); for (int i = 2; i <= length; i++) { if (numero % i == 0) { return false; } } return true; } } 

这是通过在欧几里得算法上使用二进制拆分来更快的解决方案:

private static int gcd(int a, int b) {
  if (a == 0) return b;
  if (b == 0) return a;
  return gcd(b, a%b);
}

private static int lcm(int a, int b) {
  return a / gcd(a, b) * b;
}

public static int LCM(int[] numbers) {
  int len = numbers.length;
  if (len == 2) return lcm(numbers[0], numbers[1]);
  if (len == 1) return numbers[0];
  if (len == 0) return 1;
  int[] left = Arrays.copyOfRange(numbers, 0, len/2);
  int[] right = Arrays.copyOfRange(numbers, len/2+1, len);
  return lcm(LCM(left), LCM(right));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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