如何从循环中删除重复打印?

时间:2017-10-10 23:11:42

标签: java

我被分配了编写程序的任务,该程序列出了所有数字1到10000,它们是两个奇数素数的平方和(例如3 ^ 2 + 5 ^ 2 = 34),然后输出总数这些数字。但是,我的程序没有认识到3 ^ 2 + 5 ^ 2与5 ^ 2 + 3 ^ 2相同。这会导致程序打印大多数数字的副本。这反过来也会影响数字的总数。如何防止程序打印重复项并将其添加到总计数中?

import java.util.ArrayList;

/*
 * Write a program that lists all numbers 1 to 10000 that are the sum of two 
 * odd prime numbers squared (ex. 3^2 + 5^2 = 34), then prints the total 
 * count of these numbers
 */

public class labproblem {

    public static void main(String[] args) {
        // 100*100 = 10000, the maximum
        int maxvalue = 100;

        ArrayList<Integer> primes = new ArrayList<Integer>();

        // Checks every number until max value is reached
        for (int n = 1; n < maxvalue; n++) {
            boolean prime = true;
            // checks if n is prime

            for (int j = 2; j < n; j++) {
                if (n % j == 0) {
                    prime = false;
                    break; // exits this for loop
                }
            }
            if (prime && n != 1) {
                // adds the prime number to the list of prime numbers
                primes.add(n);
            }
        }
        // declaring/initializing
        double totalcount = 0;
        double product = 0;
        // sets i2 to the odd prime numbers
        for (int i2 : primes) {
            // sets i to the odd prime numbers
            for (int i : primes) {
                // starting at 3 first increases i, then i2
                if (i >= 3 && i2 >= 3) {
                    product = Math.pow(i, 2) + Math.pow(i2, 2);

                    // Adds 1 to the count after each execution
                    if (product <= 10000)
                        totalcount = totalcount + 1;
                    // Prints the product of the two primes squared
                    if (product <= 10000)
                        System.out.println(product);

                }
            }
        }
        // Prints the total count of the products
        System.out.println(totalcount);

    }
}

3 个答案:

答案 0 :(得分:0)

您可以使用强力解决方案,即将您的产品添加到列表中,如果已经在列表中,则不再显示它们;做一些事情:

List<Integer> products = new ArrayList<>();

if (!products.contains(product)) {
    products.add(product);
    System.out.println(product);
}

答案 1 :(得分:0)

使用Set。集合不包含重复项。

    Set<Double> set = new HashSet<>();

    for (int i2 : primes) {
        for (int i : primes) {
            if (i >= 3 && i2 >= 3) {
                Double calc = Math.pow(i, 2) + Math.pow(i2, 2);
                if(calc <= 10000){
                    set.add(calc);
                }
            }
        }
    }
            //No need to count or track anything

    // For each Double called 'product' in 'set' send to an anonymous function
    set.forEach((product) -> System.out.println(product));

    System.out.println("Total Count is " + set.size());

这将打印您想要的每个唯一的主要计算,然后是总计。

答案 2 :(得分:-2)

在不改变代码的情况下,如何在下面的代码片段中以**开头的代码之间添加几行代码。

**ArrayList<Integer> processed = new ArrayList<>();**
            //sets i2 to the odd prime numbers
            for (int i2 : primes) {
                **processed.add(i2);**
         // sets i to the odd prime numbers
            for (int i : primes) {
            //starting at 3 first increases i, then i2
            if (i >= 3 && i2 >= 3 **&& !processed.contains(i)**) {
                product = Math.pow(i, 2) + Math.pow(i2, 2);