相关疑难解决方法(0)

创建庞大的字典

在我对素数的追求中,我已经问过这样一个问题:无法创建巨大的数组,这些数组导致我基于数组字典创建我自己的假数组类...:private Dictionary<int, Array> arrays = new Dictionary<int, Array>();

我可以10 000 000 000使用下面的代码知道创建很多bool(如)的伪数组:

public class CustomArray
{
    private Dictionary<int, Array> arrays = new Dictionary<int, Array>();

    public CustomArray(ulong lenght)
    {
        int i = 0;
        while (lenght > 0x7FFFFFC7)
        {
            lenght -= 0x7FFFFFC7;
            arrays[i] = new bool[0x7FFFFFC7];
            i++;
        }
        arrays[i] = new bool[lenght];
    }
}
Run Code Online (Sandbox Code Playgroud)

但是一旦我要求CustomArray 100 000 000 000元素,它就崩溃了.它适用于25次首次迭代(我的Dictionary包含25个0x7FFFFFC7元素的数组),但随后它会因OutOfMemory异常而崩溃.

剩下的,我有16GB内存,VS2013,程序是用64位编译的,我启用了gcAllowVeryLargeObjects选项,我在任务管理器中看不到任何内存峰值.


我怎样才能避免这个错误?

c# arrays dictionary

5
推荐指数
1
解决办法
188
查看次数

找到不到200万的所有素数之和需要多长时间?

我试图解决这个项目欧拉问题.我实现了euler的筛子作为java中的帮助类.它适用于小数字.但是,当我输入200万作为限制时,它不会返回答案.我使用Netbeans IDE.我等了很多个小时一次,但仍然没有打印答案.当我停止运行代码时,它给出了以下结果

Java结果:2147483647
BUILD SUCCESSFUL(总时间:2,097分43秒)

这个答案是不正确的.即使等了这么多时间,这也是不正确的.虽然相同的代码返回较小限制的正确答案.

在这个页面的底部给出了一个非常简单的算法.

我的实现是这样的:

package support;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author admin
 */
public class SieveOfEuler {
    int upperLimit;
    List<Integer> primeNumbers;

    public SieveOfEuler(int upperLimit){
        this.upperLimit = upperLimit;
        primeNumbers = new ArrayList<Integer>();
        for(int i = 2 ; i <= upperLimit ; i++)
            primeNumbers.add(i);
        generatePrimes();
    }

    private void generatePrimes(){
        int currentPrimeIndex = 0;
        int currentPrime = 2;
        while(currentPrime <= Math.sqrt(upperLimit)){
            ArrayList<Integer> toBeRemoved = new ArrayList<Integer>();
            for(int i = currentPrimeIndex ; i …
Run Code Online (Sandbox Code Playgroud)

java algorithm logic primes

4
推荐指数
1
解决办法
2695
查看次数

项目Euler#10 Java解决方案无法正常工作

我试图找到素数<2,000,000的总和.这是我在Java中的解决方案,但我似乎无法得到正确的答案.请对可能出现的问题提供一些意见,并对代码的一般建议表示赞赏.

打印'sum'给出:1308111344,这是不正确的.

编辑:感谢您的帮助.将int更改为long和<to <=并且它完美无缺,除了是找到素数的低效方法:)

/*
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
class Helper{
 public void run(){
  Integer sum = 0;
  for(int i = 2; i < 2000000; i++){
   if(isPrime(i))
    sum += i;   
  }
  System.out.println(sum);
 }

 private boolean isPrime(int nr){
  if(nr == 2)
   return true;
  else if(nr == 1)
   return false;
  if(nr % 2 == 0)
   return false;

  for(int …
Run Code Online (Sandbox Code Playgroud)

java math primes

3
推荐指数
2
解决办法
5420
查看次数

所有素数低于200万的总和

项目欧拉的问题10:

该程序针对较小的数字运行,并且减速到数十万的爬行.在200万,即使程序看起来仍在运行,答案也无法显示.

我正在尝试实施Eratosthenes筛选.它应该非常快.我的做法有什么问题?

import java.util.ArrayList;

public class p010
{
  /**
   * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17
   * Find the sum of all the primes below two million.
   * @param args
   */
  public static void main(String[] args)
  {
    ArrayList<Integer> primes = new ArrayList<Integer>();
    int upper = 2000000;
    for (int i = 2; i < upper; i++)
    {
      primes.add(i);
    }
    int sum = 0;
    for (int i = …
Run Code Online (Sandbox Code Playgroud)

java primes sieve-of-eratosthenes

1
推荐指数
1
解决办法
4073
查看次数

使用5100万个素数快速迭代数据结构

对于加载5100个素数然后迭代它们的任务,最好的数据结构(在java中)是什么?

例如,我需要知道在1000000000和相同数字减去100000之间的素数.

java primes data-structures

0
推荐指数
1
解决办法
1091
查看次数

0
推荐指数
1
解决办法
7463
查看次数

如何在c#中显示素数

如何在c#中显示1到100之间的素数?

c#

-3
推荐指数
1
解决办法
2120
查看次数