简体   繁体   English

计算素数的最后一列

[英]Calculate last column of prime numbers

I'm getting the prime numbers with following code: 我通过以下代码获取素数:

public static void printPrimeNumbers(int numberOfPrimes){
    final int NUMBER_OF_PRIMES_PER_LINE = 10;
    int count = 0;
    int number = 2;


    while (count < numberOfPrimes){
        //print the prime numbers and increase count
        if (isPrime(number)){
            count++;

            if (count % NUMBER_OF_PRIMES_PER_LINE == 0){
                System.out.printf("%-5s\n", number);
            }else {
                System.out.printf("%-5s", number);
            }

        }
        number++;
    }

}

private static boolean isPrime(int number) {
    for (int divisor = 2; divisor <= number / 2 ; divisor++) {
        if (number % divisor == 0){ // if true number is not prime
            return false;
        }
    }
    return true;
}

It's worked well, what i need to get the sum of last column primes, suppose i insert printPrimeNumbers(50) there will be 5 rows with 5 numbers in columns i want to get the sum of last column, how to achieve that? 它工作得很好,我需要得到最后一列素数的总和,假设我插入printPrimeNumbers(50) ,我想得到最后一列的总和,将有5行有5个数字的列,我该如何实现?

What you need to do is include a new variable to hold the value of the sum. 您需要做的是包括一个新变量来保存总和的值。 With that variable, you need to add the value of the number at the end of each column. 使用该变量,您需要在每列的末尾添加数字的值。 Identifying where to go to a new line will show you when to add to the new sum variable. 确定要换行的位置将向您显示何时添加到新的sum变量。 I've written in where you need to add variables/values but assigned these values to ?'s which you need to replace with the actual code to make this work. 我写过您需要在其中添加变量/值的地方,但是将这些值分配给了?,您需要用实际的代码替换这些值才能完成此工作。

public static void printPrimeNumbers(int numberOfPrimes){
    final int NUMBER_OF_PRIMES_PER_LINE = 10;
    int count = 0;
    int number = 2;
    int sum = ?; 


    while (count < numberOfPrimes){
        //print the prime numbers and increase count
        if (isPrime(number)){
            count++;

            if (count % NUMBER_OF_PRIMES_PER_LINE == 0){
                System.out.printf("%-5s\n", number);
                sum += ?;
            }else {
                System.out.printf("%-5s", number);
            }

        }
        number++;
    }

}

private static boolean isPrime(int number) {
    for (int divisor = 2; divisor <= number / 2 ; divisor++) {
        if (number % divisor == 0){ // if true number is not prime
            return false;
        }
    }
    return true;
}

You could edit your 您可以编辑

printPrimeNumbers() printPrimeNumbers()

method to be: 方法是:

public static void printPrimeNumbers(int numberOfPrimes){
    final int NUMBER_OF_PRIMES_PER_LINE = 10;
    int count = 0;
    int number = 2;
    **List<int> lastColumnPrimes = new ArrayList();**


    while (count < numberOfPrimes){
        //print the prime numbers and increase count
        if (isPrime(number)){
            count++;

            if (count % NUMBER_OF_PRIMES_PER_LINE == 0){
                System.out.printf("%-5s\n", number);
                l**astColumnPrimes.add(number);**
            }else {
                System.out.printf("%-5s", number);
            }

        }
        number++;
    }

    **int lastColumnSum = 0;
    for ( int n : lastColumnPrimes ) {
        lastColumnSum += n;
    }
    System.out.printf("Sum of numbers in last column is " + lastColumnSum);**

}

Notice the added lines by the double asterisks (**). 注意双星号(**)所添加的行。 Very few additions needed, just a list to keep track of the numbers in the last column, a loop to get the sum, and finally a print statement to report the sum. 只需很少的添加,只需一个列表即可跟踪最后一列中的数字,一个循环即可获取总和,最后还有一个打印语句即可报告总和。 Viola! 中提琴!

If you don't need to keep track of the numbers in the last column, and just want the sum, then you can leave out the list and loop, and just use an int to tally the values instead of a List. 如果您不需要跟踪最后一列中的数字,而只需要求和,那么您可以省略列表并循环,而只需使用一个int来计算值而不是List。

Create a new variable and initilize it to 0. Then you can just add to it in your if statement where you check for a new line. 创建一个新变量并将其初始化为0。然后您可以在if语句中将其添加到其中,以检查新行。 And then print out the total at the end. 然后在最后打印出总计。

int total = 0;


while (count < numberOfPrimes){
    //print the prime numbers and increase count
    if (isPrime(number)){
        count++;

        if (count % NUMBER_OF_PRIMES_PER_LINE == 0){
            System.out.printf("%-5s\n", number);
            total += number;
        }else {
            System.out.printf("%-5s", number);
        }

    }
    number++;
}
System.out.println("Total:" + total);

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

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