Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带计数器的唯一因子分解_Java_Algorithm_Prime Factoring - Fatal编程技术网

Java 带计数器的唯一因子分解

Java 带计数器的唯一因子分解,java,algorithm,prime-factoring,Java,Algorithm,Prime Factoring,我正在尝试用JAVA构建一个程序,它使用唯一的因子分解定理。 我的意思是,从用户那里得到一个大于1的数字,然后用计数器打印所有唯一的因式分解 对于ex,对于100,输出应为 2 2 5 2 23 因为100=2*2*5*5 对于23,输出应为 2 2 5 2 23 如果输入为6,则输出为 2 3 最后一个例子,对于8112,输出应该是 2 4 3 13 2 到目前为止,我实现了一个代码,它给出了第一列并更正了素数。但是,当计数器>1打印第二列时,我没有成功计数 我的代码如下: i

我正在尝试用JAVA构建一个程序,它使用唯一的因子分解定理。 我的意思是,从用户那里得到一个大于1的数字,然后用计数器打印所有唯一的因式分解

对于ex,对于100,输出应为

2 2

5 2
23
因为100=2*2*5*5

对于23,输出应为

2 2

5 2
23
如果输入为6,则输出为

2
3
最后一个例子,对于8112,输出应该是

2 4
3
13 2
到目前为止,我实现了一个代码,它给出了第一列并更正了素数。但是,当计数器>1打印第二列时,我没有成功计数

我的代码如下:

int n = scanner.nextInt();
    int num = 2;
    while (num <= n) {
        int i = 2;
        boolean isPrime = true;
        while (i < num && isPrime) {
            if (num % i == 0) {
                isPrime = false;
            }
            i++;
        }
        if (isPrime) {
            int counter = 1;
            if (n % num == 0) {
                System.out.println(num);
            }
        }
        num++;
    }
int n=scanner.nextInt();
int num=2;

而(num您的
计数器
尚未实现

int n = scanner.nextInt();
int num = 2;
while (num <= n) {
    int i = 2;
    boolean isPrime = true;
    while (i < num && isPrime) {
        if (num % i == 0) {
            isPrime = false;
        }
        i++;
    }
    if (isPrime) {
        int counter = 0;
        while (n >= num && n % num == 0) {
            counter++;
            n /= num;
        }
        if (counter == 1) {
            System.out.println(num);
        }
        else if (counter > 1) {
            System.out.println(num + " " + counter);
        }
    }
    num++;
}
int n=scanner.nextInt();
int num=2;
而(num=num&&n%num==0){
计数器++;
n/=num;
}
如果(计数器==1){
系统输出打印项数(num);
}
否则,如果(计数器>1){
System.out.println(num+“”+计数器);
}
}
num++;
}

找到号码后,您必须继续查找该号码的计数器。下面是我的解决方案

            int ounter = 0;
            if (n % num == 0) {
                System.out.print(num);
                int n1 = n;
                while(n1%num == 0) {
                    ounter ++;
                    n1 = n1/num;
                }
                if(ounter != 1) {
                    System.out.println(" "+ounter);
                }
            }

int counter=1
的用途是什么?它从未在定义的范围内使用。将
n
除以
i
(重复),而
i
是一个因素。您只需检查
i*i@bluessono,这可能就是尚未完全实现的计数器(输入错误)。@bluessono应该是一个“计数器”。当n mod(质数)除以多个相同质数时,此变量需要计算所有时间。