Efficient way to calculate the sum of all primes between a range Problem
I'm trying to write a code in which Q questions are asked and each question contains two numbers A and B. For each question, I have to find the sum of all primes between integers A and B (inclusive). I've written a program that works, but it isn't fast enough to pass all the restrictions. Is there any way to improve the efficiency of my code?
Restrictions
1≤Q≤10^5
1≤A≤B≤10^5
Time limit: 0.6s
import java.io.*;
import java.util.*;

public class sumofprimes{
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static StringTokenizer st;
    static boolean isPrime(int n){ 
        if(n==1)return false; 
        for (int i=2; i*i&lt;=n; i++) 
            if (n%i==0) return false;
        return true; 
    } 
  
    static int sum(int a, int b){ 
        int sum=0; 
        for(int i=b; i&gt;=a; i--) { 
            boolean prime=isPrime(i); 
            if(prime) sum+=i;
        } 
        return sum; 
    } 
    public static void main(String[] args)throws IOException{ 
        int q=readInt();
        for(int i=0; i&lt;q; i++){
            int a=readInt(), b=readInt(); 
            System.out.println(sum(a,b));
        }
    } 
    static String next () throws IOException {
        while (st == null || !st.hasMoreTokens())
           st = new StringTokenizer(br.readLine().trim());
    return st.nextToken();
    }
    static int readInt () throws IOException {
    return Integer.parseInt(next());
    }
}
Nice implementation, it's already efficient but as noted in the comments can be improved. Few general suggestions:

Format the code: many IDE support auto formatting or you can use an online formatter.

Omitting brackets is not good practice: it's better to include the brackets to avoid confusion when reading or changing the code. See the difference from:
static boolean isPrime(int n){ 
      if(n==1)return false; 
      for (int i=2; i*i&lt;=n; i++) 
          if (n%i==0) return false;
      return true; 
} 

To:
static boolean isPrime(int n) {
    if (n == 1) {
        return false;
    }
    for (int i = 2; i * i &lt;= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}


Arrange the methods from general to specific, it's easier to follow. From:
static boolean isPrime(int n){ 
}

static int sum(int a, int b){ 
    //call isPrime
} 

public static void main(String[] args)throws IOException{ 
    // call readint
    // call sum
} 

static String next () throws IOException {
}

static int readInt () throws IOException {
    // call next
}

To:
public static void main(String[] args)throws IOException{ 
    // call readint
    // call sum
}

static int readInt () throws IOException {
    // call next
} 

static String next () throws IOException {
}

static int sum(int a, int b){ 
    //call isPrime
} 

static boolean isPrime(int n){ 
}


The methods readInt and next can be replaced with java.util.Scanner:
Scanner scanner = new Scanner(System.in);
int q = scanner.nextInt();


Close the resources: br.close() (or scanner.close()) at the end of main or if exceptions occur.

PrintWriter pr is not used.

The method sum(int a, int b) does not sum two integers. A better name might be sumPrimesBetween

StringTokenizer is discouraged for new code, from the docs:

StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split method of String
or the java.util.regex package instead.



Performance
As mentioned in the comments, since you know the upper bound 10^5 you can precompute all the prime numbers in advance. The main logic can be:

Precompute all prime numbers until 10^5: you can use an array of boolean initialized to true and then set the corresponding index to false if not prime. For example:

static void computePrimesUntil(int n) {
    isPrime = new boolean[n+1];
    Arrays.fill(isPrime, true);
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i &lt;= n; i++) {
        if (isPrime[i] &amp;&amp; (long) i * i &lt;= n) {
            for (int j = i * i; j &lt;= n; j += i){
                isPrime[j] = false;
            }
        }
    }
}

Code inspired from here.

Parse all the questions from the user
Output the sum of primes for each question

Note: depending on how the total running time is calculated, you need to consider the time to parse the input and print the results. The total running time also depends on the machine where you run the program.