Prime numbers calculator takes too much time (JAVA)

ccc

When I run this code to find sum of prime numbers below 20 it works fine, but when try to find sum below 2500000 it takes too much time. It's been at least 20 minutes and it's still running. It seems like it's not working. How do I fix it?

class PrimeSummation {
    public static void main(String[] args) {
        int sum = 0;
        for(int i = 1; i < 2500000; i++) {
            int count = 0;
            for(int j = 1; j < i + 1; j++) {
                if((i%j) == 0) {
                    count++;
                }
            }
            if(count == 2) {
                sum += i;
            }
        }
        System.out.println(sum);
    }
}
Paul Boddington

sum cannot be an int because the answer is 219697708195 whereas Integer.MAX_VALUE is only 2147483647. You must use a long or a BigInteger instead.

Your algorithm is very slow, because for every one of the 2500000 numbers you are starting from scratch to decide whether it is prime or not, and your approach for testing whether a number is prime (try every possible factor) is not very efficient.

다음 코드는 내 컴퓨터에서 약 10 분의 1 초 안에 답변을 생성합니다.

int num = 2500000;
long sum = 0;
boolean[] arr = new boolean[num];
for (int p = 2; p < num; p++) {
    if (!arr[p]) {
        sum += p;
        for (int k = p * 2; k < num; k += p)
            arr[k] = true;
    }
}
System.out.println(sum);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

CalendarView takes much time for displaying

분류에서Dev

Prime Numbers in Java - Algorithms

분류에서Dev

Ubuntu 12.04 installation take too much time

분류에서Dev

All java threads are running on a single core, ultimately taking too much time to execute

분류에서Dev

SignalR serverSentEvents on Windows Server 2008 with IIS 7 POST request taking too much time to complete

분류에서Dev

Python algorithm for prime numbers

분류에서Dev

Dynamics Ax 2012 R2 AIF services refresh takes too long a time

분류에서Dev

Regex grabbing too much text

분류에서Dev

Is this too much memory for mysql on ubuntu?

분류에서Dev

Prime numbers between two large numbers?

분류에서Dev

Java tip calculator

분류에서Dev

Stop a program before it uses too much memory

분류에서Dev

JavaScript Date Difference - one hour too much

분류에서Dev

InternalError : too much recursion-get in jquery

분류에서Dev

SOSL statement returns too much rows

분류에서Dev

Highcharts: too much space for bottom legend

분류에서Dev

MYSQL query consumes too much memory

분류에서Dev

Adding function gives error: too much recursion

분류에서Dev

setMaxResults with huge tables uses too much memory

분류에서Dev

TLsharp CHANNELS_TOO_MUCH 예외

분류에서Dev

media app takes too long to buffer

분류에서Dev

Checksumming large swathes of prime numbers? (for verification)

분류에서Dev

unzip .gz file in Java takes long time using GZInputStream and byte buffer

분류에서Dev

akonadi services and mysqld use too much memory in kubuntu 16.04

분류에서Dev

Audacity playback speed much too fast, on Ubuntu Mate 15.04

분류에서Dev

Gettin crossfilter.js error "too much recursion"

분류에서Dev

malloc in child thread cost too much virtual memory

분류에서Dev

How to show comma separation in numbers on Ubuntu's calculator?

분류에서Dev

Single-threaded program takes too low CPU?

Related 관련 기사

  1. 1

    CalendarView takes much time for displaying

  2. 2

    Prime Numbers in Java - Algorithms

  3. 3

    Ubuntu 12.04 installation take too much time

  4. 4

    All java threads are running on a single core, ultimately taking too much time to execute

  5. 5

    SignalR serverSentEvents on Windows Server 2008 with IIS 7 POST request taking too much time to complete

  6. 6

    Python algorithm for prime numbers

  7. 7

    Dynamics Ax 2012 R2 AIF services refresh takes too long a time

  8. 8

    Regex grabbing too much text

  9. 9

    Is this too much memory for mysql on ubuntu?

  10. 10

    Prime numbers between two large numbers?

  11. 11

    Java tip calculator

  12. 12

    Stop a program before it uses too much memory

  13. 13

    JavaScript Date Difference - one hour too much

  14. 14

    InternalError : too much recursion-get in jquery

  15. 15

    SOSL statement returns too much rows

  16. 16

    Highcharts: too much space for bottom legend

  17. 17

    MYSQL query consumes too much memory

  18. 18

    Adding function gives error: too much recursion

  19. 19

    setMaxResults with huge tables uses too much memory

  20. 20

    TLsharp CHANNELS_TOO_MUCH 예외

  21. 21

    media app takes too long to buffer

  22. 22

    Checksumming large swathes of prime numbers? (for verification)

  23. 23

    unzip .gz file in Java takes long time using GZInputStream and byte buffer

  24. 24

    akonadi services and mysqld use too much memory in kubuntu 16.04

  25. 25

    Audacity playback speed much too fast, on Ubuntu Mate 15.04

  26. 26

    Gettin crossfilter.js error "too much recursion"

  27. 27

    malloc in child thread cost too much virtual memory

  28. 28

    How to show comma separation in numbers on Ubuntu's calculator?

  29. 29

    Single-threaded program takes too low CPU?

뜨겁다태그

보관