为什么我不能在这个计算从 0 到 n 的所有素数的程序中输入大值(例如 n = 100000)?

守卫护手447

我对编码相当陌生,这学期我刚开始学习 Java。我在胡闹并创建了这个 java 程序,它可以找到从 0 到 n 的所有素数。它使用模数和嵌套的 for 循环并将其输出到数组列表中。现在,我用 n = 5、n = 100、n = 1000 和 n = 10000 对其进行了测试,结果完全正常并给了我准确的结果,但是当我输入 n = 100000 时,控制台只是空白,没有给我任何输出。我意识到对于大数字需要更长的时间,所以我期待等待它对所有数字进行处理,但它只是“放弃了”。

这是某些计算超时的结果吗?是否有一个覆盖,所以我可以用更大的数字来计算这个?我意识到这不是最有效的代码(即 for 循环中的计算可能会执行两次),但这不是问题所在。问题是为什么某个数字会停止输出,有没有办法绕过这个。

我把我的代码放在下面,分成两个块,因为我把它放在两个不同的类中。

谢谢:)

通用文件


import java.util.ArrayList;
import java.util.Scanner;

public class General 
{
    
    
    

    public static void main(String[] args) 
    {
        Scanner number = new Scanner(System.in);
        
        ArrayList<Integer> result = new ArrayList<Integer>();
        
        System.out.print("What is the number you want to find all the primes that are smaller than it: ");
        long n = number.nextInt();
        
        PrimeNumberSeeker pNS = new PrimeNumberSeeker();
        
        result = pNS.PrimesFromZero(n);
        
        for (int m = 0; m < result.size(); m++)
                {
                System.out.print(result.get(m));
                
                if (m+1 >= (result.size()))
                    System.out.print(".");
                else 
                    System.out.print(", ");
                }

    }

}

PrimeNumberSeeker.java


import java.util.ArrayList;


public class PrimeNumberSeeker 
{
    ArrayList<Integer> primes = new ArrayList<Integer>();

    
    public ArrayList<Integer> PrimesFromZero(long n)
    {
        for (int i = 0; i <= n; i++) //selects all numbers from 0 - n inclusive
        {
            int check = 0;
            //System.out.println(i);
            for (int j = 1; j <= i; j++) //we make sure not to include 1 and itself (n)
            {
                
                
                    if (i % j == 0)
                    check += 1;
                
                
                
                //System.out.println(i + " " + j);
            }
            if (check == 2)
                primes.add(i);
                
        }
        
        return primes;
    }
    
}
最大值

这只是一个计算复杂度的问题,您的算法可以在不同方面进行优化以降低其复杂度:

  • 你不需要check变量
  • 您可以将j范围限制为2 到 sqrt(i)
  • 如果i%j==0你能停止你的调查i:它不是主要的

其他建议:

  • 总是使用括号
  • 函数名使用小驼峰
  • 不要混合ints 和longs,对于你的目的ints 就足够了

这个算法不是最好的,但在处理高达 10.000.000(或更多)的数字时效果很好:

class PrimeNumberSeeker {
    public ArrayList<Integer> primesFromZero(int n) {
        ArrayList<Integer> primes = new ArrayList<Integer>();
        
        for (int i = 1; i <= n; i++) {
            boolean isPrime = true; // say your number is prime (we'll verify)
            for (int j = 2; j <= Math.sqrt(i); j++) { // verify if it really is
                if (i % j == 0) {
                    isPrime = false; // it isn't
                    break; // no more checks are needed, go out from this for cycle
                }
            }
            
            if (isPrime) { // if isPrime is still true then i is prime
                primes.add(i);
            }
        }
        
        return primes;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么不能在这个依赖类型的程序中求和推断0 + n = n?

为什么我不能在这个 Angular 9 应用程序中将变量从组件传递到服务?

无法理解为什么在这个大 O 定义中 n0 不是 1?

使用 pthreads 加速从 0 到 N 计算素数的处理。我是否正确使用它们?

Python 循环 N 到 0 或 -N 到 0

最小对,覆盖从0到n的所有整数

为什么我不能在我的 c 程序中使用 "%[^\n]" 扫描集?

为什么我不能在给定的字符串中替换\ n?

Java程序使用列表查找从0到n的“幸运”数字

在 O(n) 中对从 0 到 n^m 的 n 个数字进行排序的算法?其中 m 是常数

在Elixir中运行代码0到N次的简便方法?

这是一个C程序,用于计算从0到n的k之和:nCk。我遇到了细分错误

我的程序有什么问题?我试图找到从n到m的素数列表

¿为什么我不能在这个查询处理程序中连接两个数组?

如何在WinForms图表中创建从(0,0)到(0,n)的线?

假设输入可以达到10亿,那么我该如何编写一个时间和内存效率更高的程序?(打印m和n之间的所有素数)

为什么在javascript中我不能在这个函数中创建对象?

为什么我不能在这个草率模式函数中访问arguments.callee?

为什么我不能在这个 recyclerview 适配器中设置 setOnLongClick ?

从 Curry-0, 1, 2 到 ...n

求和到值 n

随机排序-Fisher Yates,为什么我找不到0到N-1之间的随机数?

我如何计算系列的总和 s = 1=(1! + 2! +3! . . + n!) % 1000000007 其中 n 范围从 0 到 10^6

为正负n创建从0到n的数字数组

如何找到2的n次方。n的范围是0到200

序言:给定N,返回从0到N的数字列表

编辑数字列表 n 以仅包含从 0 到 n

为什么可以使用归纳法中的IHn'(n'= n'+ 0)来证明Coq中的n = n + 0?

给定一个整数从0到N的数组,有多少种方式可以使array [i]不能为i