带计数器的独特分解

时间:2018-10-30 08:13:23

标签: java algorithm prime-factoring

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

例如,对于100,输出应为

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex')) {
                details.useVersion "28.0.0"
            }

            if(details.requested.group == 'androidx.lifecycle'  && !details.requested.name.contains('multidex'))
            {
                details.useVersion "2.0.0"
            }

        }
    }
}

dependencies 
{


 implementation 'androidx.test:runner:1.1.0'
    implementation 'androidx.test.espresso:espresso-core:3.1.0'
        //    androidTestImplementation 'com.android.support.test:runner:1.0.2'
        //    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // lifecycle components
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-core:2.0.0'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0'

    //    implementation 'android.arch.lifecycle:extensions:1.1.1'
    //    kapt "android.arch.lifecycle:compiler:1.1.1"

    // room components
        //    implementation 'android.arch.persistence.room:runtime:1.1.1'
    implementation 'androidx.room:room-runtime:2.0.0'

    //    data binding components
    annotationProcessor "com.android.databinding:compiler:3.1.4"
    implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

    //implementation 'com.google.dagger:dagger-android:2.16'
    implementation 'com.google.dagger:dagger-android-support:2.16'
    // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'
    compile project(path: ':data')
}

因为100 = 2 * 2 * 5 * 5

对于23,输出应为

2 2

5 2

如果输入为6,则输出为

23

最后一个示例,对于8112,输出应为

2
3

到目前为止,我实现了提供第一列并校正素数的代码。但是我没有成功计数计数器> 1来打印第二列。

我的代码如下:

2 4
3
13 2

有什么想法我想念的吗?

2 个答案:

答案 0 :(得分:0)

您的counter尚未实现。

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++;
}

答案 1 :(得分:0)

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

            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);
                }
            }