英文:
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 {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
int i = 0;
int x = t;
long res[] = new long[t];
while (t > 0) {
StringTokenizer s = new StringTokenizer(br.readLine());
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<=r;i++){
flag=false;
if(i>=2){
for(j=2;j<i;j++){
if(i%j==0){
flag=true;
break;
}
}
if(flag==false){
count+=i;
}
}
}
return count;
}
public static void main(String args[] ) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
int i=0;
int x=t;
long res[]=new long[t];
while(t>0){
StringTokenizer s=new StringTokenizer(br.readLine());
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]);
}
`
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.
Thank you in advance.
答案1
得分: 1
将您的代码分割,消除不必要的变量。对于素数,只需重复到sqrt (n):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Loop: ");
int t = Integer.parseInt(br.readLine());
long[] res = new long[t];
for (int i = 0; i < t; i++) {
System.out.print("(l-r)?: ");
String[] s = br.readLine().split(" ");
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;
import java.io.InputStreamReader;
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 {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Loop: ");
int t = Integer.parseInt(br.readLine());
long[] res = new long[t];
for (int i = 0; i < t; i++) {
System.out.print("(l-r)?: ");
String[] s = br.readLine().split(" ");
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]);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论