# Iam getting a TLE for a code to print sum of all primes between given 2 numbers(inclusive) in java

go评论49阅读模式

Iam getting a TLE for a code to print sum of all primes between given 2 numbers(inclusive) in java

# 问题

``````import java.io.*;
import java.util.*;

class TestClass {
public static long prime(long l, long r) {
boolean flag = false;
long count = 0;
long i, j;
for (i = l; i <= r; i++) {
flag = false;
if (i >= 2) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
flag = true;
break;
}
}
if (!flag) {
count += i;
}
}
}
return count;
}

public static void main(String args[]) throws IOException {

int i = 0;
int x = t;
long res[] = new long[t];
while (t > 0) {
long l = Long.parseLong(s.nextToken());
long r = Long.parseLong(s.nextToken());
res[i] = prime(l, r);
t--;
i++;
}
for (i = 0; i < x; i++) {
System.out.println(res[i]);
}
}
}
``````

`

``````  import java.io.*;
import java.util.*;
class TestClass {
public static long prime(long l,long r){
boolean flag=false;
long count=0;
long i,j;
for(i=l;i&lt;=r;i++){
flag=false;
if(i&gt;=2){
for(j=2;j&lt;i;j++){
if(i%j==0){
flag=true;
break;
}
}
if(flag==false){
count+=i;
}
}
}
return count;
}
public static void main(String args[] ) throws IOException {
int i=0;
int x=t;
long res[]=new long[t];
while(t&gt;0){
long l=Long.parseLong(s.nextToken());
long r=Long.parseLong(s.nextToken());
res[i]=prime(l,r);
t--;
i++;
}
for(i=0;i&lt;x;i++){
System.out.println(res[i]);
}
``````

`

I don't know what is the reason for TLE for my code. How can I optimize my code inorder to get rid of that error? Can anyone help me to find the optimal solution for my code.

# 答案1

``````import java.io.BufferedReader;
import java.io.IOException;

class TestClass {

public static long sumPrime(long l, long r) {
long count = 0;
for (long n = l; n <= r; n++) {
if (isPrime(n)) count += n;
}
return count;
}

public static boolean isPrime(long n) {
if (n < 2) return false;
if (n == 2) return true;
for (long i = 2; i <= (long) Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] args) throws IOException {
System.out.print("Loop: ");
long[] res = new long[t];
for (int i = 0; i < t; i++) {
System.out.print("(l-r)?: ");
long l = Long.parseLong(s[0]);
long r = Long.parseLong(s[1]);
res[i] = sumPrime(l, r);
}
System.out.print("Result: ");
for (int i = 0; i < t; i++) {
System.out.printf("%d  ", res[i]);
}
}
}
``````

Split your code, eliminate unnecessary variables. For prime numbers, just repeat up to sqrt (n):

``````import java.io.BufferedReader;
import java.io.IOException;
class TestClass {
public static long sumPrime(long l, long r) {
long count = 0;
for (long n = l; n &lt;= r; n++) {
if (isPrime(n)) count += n;
}
return count;
}
public static boolean isPrime(long n) {
if (n &lt; 2) return false;
if (n == 2) return true;
for (long i = 2; i &lt;= (long) Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) throws IOException {
System.out.print(&quot;Loop: &quot;);
long[] res = new long[t];
for (int i = 0; i &lt; t; i++) {
System.out.print(&quot;(l-r)?: &quot;);
long l = Long.parseLong(s[0]);
long r = Long.parseLong(s[1]);
res[i] = sumPrime(l, r);
}
System.out.print(&quot;Result: &quot;);
for (int i = 0; i &lt; t; i++) {
System.out.printf(&quot;%d  &quot;, res[i]);
}
}
}
``````

• 本文由 发表于 2020年7月22日 10:22:06
• 转载请务必保留本文链接：https://go.coder-hub.com/63025826.html
• java

go 45

go 49

go 76

go 46