【问题标题】:i am trying to compute primes and am having problems with the primeswithin我正在尝试计算素数,但遇到了素数问题
【发布时间】:2013-09-02 15:49:46
【问题描述】:
//i have this so far

public class Primes {
    private boolean[] nums;
    private int upperbound;

    public Primes(int n) {

        nums = new boolean[n + 1];
        for (int i = 2; i <= n; i++)
            nums[i] = true;
    }

    public static final int DEFAULT_UPPER_BOUND = 100 + 1;

    public boolean isPrime(int x) {
        if (nums[x] == true) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isComposite(int x) {
        if (nums[x] == true) {
            return false;
        } else {
            return true;
        }
    }

    public int getPrimesWithin(int min, int max) {
        for (int n = min; n <= max; n++) {
            if (nums[n] == true) {
                return n;
            }

        }
        return max;

    }

    public String toString() {
        String a = "";
        a += (nums) + " ";
        return a;
    }

    public int getUpperBound() {
        return nums.length;
    }

    public int nthPrime(int n) {
        int count = 0;
        int index = 2;

        while (count < n) {
            if (nums[index] = true) {
                count++;
            }
        }
        return index;
    }

    public void computePrimes(int x) {
        for (int i = 2; i * i <= x; i++) {
            if (nums[i]) {
                for (int j = i; i * j <= x; j++) {
                    nums[i * j] = false;
                }
            }
        }
    }

    void changeUpperBound(int x) {
        upperbound = x;
    }

}

//it needs to fit this

public class Prime {
    public static void main(String[] args) {
        Primes somePrimes = new Primes();
        System.out.println("Default Prime object");
        System.out.println(somePrimes);
        System.out.println("Upper Bound: " + somePrimes.getUpperBound());
        System.out.println("4th prime: " + somePrimes.nthPrime(4));
        System.out.println("7 prime?: " + somePrimes.isPrime(7));
        System.out.println("7 composite?: " + somePrimes.isComposite(7));
        somePrimes.changeUpperBound(50);
        System.out.println(somePrimes);
        int[] primes = somePrimes.getPrimesWithin(40, 50);
        System.out.print("Primes between 40 and 50: ");
        for (int p : primes)
            System.out.print(p + " ");
        System.out.println();

        System.out.println("*******************");

        Primes myPrimes = new Primes(53);
        System.out.println(myPrimes);
        System.out.println("Upper Bound: " + myPrimes.getUpperBound());
        System.out.println("10th prime: " + myPrimes.nthPrime(10));
        System.out.println("15 prime?: " + myPrimes.isPrime(15));
        System.out.println("15 composite?: " + myPrimes.isComposite(15));
        myPrimes.changeUpperBound(200);
        System.out.println(myPrimes);
        int[] primes2 = myPrimes.getPrimesWithin(50, 97);
        System.out.print("Primes between 50 and 97: ");
        for (int p : primes2)
            System.out.print(p + " ");
        System.out.println();
    }
}

// i am not sure how to make the primeswithin work and if you notice any other errors there are probably several

Primes 类的规范:

实例字段:
私有布尔 [] 数字; // 你可以选择另一个逻辑名称 // 是必要大小的变量,或者我们可以只使用 .length 吗?

类常量: 公共静态最终 int DEFAULT_UPPER_BOUND = ?; // 选择一个值并默认使用 // 构造函数。如果上限(可以测试素数的最大数)是 10, // 数组的大小是多少?

访问器: boolean isPrime(int x) boolean isComposite(int x) int nthPrime(int n) // 示例:nthPrime(4) 返回 7,因为 7 是 4 th 鼎盛时期 int[] getPrimesWithin(int min, int max) // 返回 min 和 max 之间的素数数组 String toString() // 返回数据集中所有素数的字符串版本
getUpperBound() // 返回可以测试素数的最大数(最高索引)

修饰符: private void computePrimes() // 使用算法,仅由每个构造函数调用 void changeUpperBound(int x) // 改变数组,使最高索引为 x

构造函数: 素数(整数上限) Primes() // 使用 DEFAULT_UPPER_BOUND 常量

测试仪的输出: 默认 Prime 对象 质数到 100:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 上限:100 第四质数:7 7素数?:真的 7 复合?:假 素数到 50:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 40 到 50 之间的素数:41 43 47


质数到 53:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 上限:53 第 10 个素数:29 15 素数?:假 15 复合?:是的 质数到 200:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 50 到 97 之间的素数:53 59 61 67 71 73 79 83 89 97

【问题讨论】:

  • 在 computePrimes 中,内循环应该是 for (int j = i; j

标签: java


【解决方案1】:

使用ArrayList&lt;Integer&gt; 而不是int[] 作为primesWithin 的返回类型。遍历所需范围内的大数组,并将您遇到的每个素数添加到ArrayList

【讨论】:

  • 不幸的是,这个分配的限制之一是没有数组列表,但这会起作用,谢谢
  • 好的,你想在你的问题中添加限制吗?否则,人们会浪费时间想出越来越多的解决方案,结果却让你告诉他们这不符合规则。
【解决方案2】:

我找到了解决办法:

public int[] getPrimesWithin(int min, int max)
    {
        int count = 0;
        for (int i = min; i <= max; i++)
            if(nums[i])
            {
            count++;
            }

        int[] temp = new int[count];
        count = 0;
        for (int i = min; i <= max; i++)
            if(nums[i])
            {
                temp[count] = i;
                count++;
            }
        return temp;
    }

【讨论】:

  • 请解释你的代码是做什么的以及它是怎么做的。
猜你喜欢
  • 2010-11-09
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2021-11-04
  • 1970-01-01
相关资源
最近更新 更多