Java中带指数的素数分解

Posted

技术标签:

【中文标题】Java中带指数的素数分解【英文标题】:Prime Factorization With Exponents In Java 【发布时间】:2020-03-23 17:31:40 【问题描述】:

我正在尝试编写一个 java 代码,它以两种形式显示数字的素数分解:乘以和乘以指数。例如,正确的输出如下所示:

输入一个数字

100

100 的素数分解是:

100 = 2 * 2 * 5 * 5

100 = 2^2 * 5^5

除了我当前的代码只输出这个:

输入一个数字

100

100 的素数分解是:

100 = 2 2 5 5

我的代码如下所示:

import java.util.Scanner;
public class Factorization 
public static void main(String[] args)  
    Scanner keyboard = new Scanner(System.in);
    // user inputs variables here
    System.out.println("Enter a number");
    long n = keyboard.nextLong();
    System.out.println("The prime factorization of " + n + " is: ");
    System.out.print(n+" = ");

    // solution for 1 as an input

    if(n==1)
        System.out.println("1");
    

    // for each potential factor
    for (long factor = 2; factor*factor <= n; factor++) 

        // if factor is a factor of n, repeatedly divide it out
        while (n % factor == 0) 
            System.out.print(factor + " "); 
            n = n / factor;
        
    

    // if biggest factor occurs only once, n > 1
    if (n > 1)
        System.out.println(n);
     else 
        System.out.println();
    


我怎样才能让它给出正确的输出?

非常感谢!

【问题讨论】:

【参考方案1】:

打印第一个表单很容易,只需对代码稍作修改,即可打印出*。然而,第二种形式有点棘手,因为您不能在获得因子时直接打印出因子,因为您需要计算每个因子出现的次数才能知道它们的指数。有几种不同的方法可以做到这一点,但我认为最简单的方法是使用 2 个 ArrayList。这些 ArrayList 之一将包含基数,而另一个将包含每个基数的指数。这是我的实现,我添加了一些 cmets 进行澄清。

import java.util.Scanner;
import java.util.ArrayList;
public class Factorization
public static void main(String[] args)  
    Scanner keyboard = new Scanner(System.in);
    // user inputs variables here
    System.out.println("Enter a number");
    long n = keyboard.nextLong();
    long m = n;
    System.out.println("The prime factorization of " + n + " is: ");
    System.out.print(n+" = ");
    ArrayList<Long> bases = new ArrayList<Long>(); //ArrayList containing each unique factor
    ArrayList<Long> exponents = new ArrayList<Long>(); //ArrayList containing exponents, or the amount of times that factor is multiplied

    // solution for 1 as an input

    if(n==1)
        System.out.println("1");
    
    // for each potential factor
    for (long factor = 2; factor*factor <= n; factor++) 

        // if factor is a factor of n, repeatedly divide it out
        while (n % factor == 0) 
            n = n / factor;
            if (!bases.contains(factor)) //if the factor is not already in the bases list, it's unique and should be added
                bases.add(factor);
                exponents.add(1L);
            
            else //if the factor is already in the bases list, increment its exponent by 1
                exponents.set(bases.indexOf(factor), exponents.get(bases.indexOf(factor)) + 1);
            
        
    

    // if biggest factor occurs only once, n > 1
    if (n > 1)
        if (!bases.contains(n))
                bases.add(n);
                exponents.add(1L);
        
        else
            exponents.set(bases.indexOf(n), exponents.get(bases.indexOf(n)) + 1);
        
    
    //printing out the first form
    for (int i = 0; i < bases.size(); i++) 
    
        for (int j = 0; j < exponents.get(i); j++) //each base is printed out an amount of times equal to its exponent
        
            if (i != 0 || j != 0) //making sure we don't print * before the first base
            
                System.out.print(" * ");
            
            System.out.print(bases.get(i));
        
    
    System.out.println();
    System.out.print(m+" = ");
    //printing out the second form
    for (int i = 0; i < bases.size(); i++) 
    
        if (i >= 1) //making sure we don't print * before the first base
        
            System.out.print(" * ");
        
        System.out.print(bases.get(i) + "^" + exponents.get(i));
    


如果您想了解有关 ArrayLists 的更多信息,我建议您在此处查看有关它的官方 Java 文档:https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html。 我希望这会有所帮助。

【讨论】:

以上是关于Java中带指数的素数分解的主要内容,如果未能解决你的问题,请参考以下文章

大数的素数分解

JAVA分解质因子

java_分解质因数

一道cf水题再加两道紫薯题的感悟

算法15---数论6---素数,回文素数 分解质因素

具有素数列表的高效素数分解算法