Calculating the prime numbers between two integers I've wrote this code to calculate the prime numbers between two integers n and m, and I want to optimise it.

#include&lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;
int main (){
long int T[100][2],n,m,i,j;
int testcase,k;
bool prem;
cin&gt;&gt;testcase;
for(i=0;i&lt;testcase;i++){
   cin&gt;&gt;T[i][0]&gt;&gt;T[i][1];}
for(k=0;k&lt;testcase;k++){
    m=T[k][0];
    n=T[k][1];
for(i=m;i&lt;=n;i++){
    prem=true;
for(j=2;j&lt;i;j++){
    if(i%j==0){prem=false;}}
    if(prem) {cout&lt;&lt;i&lt;&lt;endl;}}}
return 0;}
Use better variable names. Or, alternatively, include comments to show what the variables are for.
Reformat the code with better indentation.
Do not use using namespace std. That is just bad practice.
Instead of looping j from 2 to i, you can just loop upto floor(sqrt(i) + 1).
Do not define i, j and k for such a large scope. Keep their scopes minimal.
Since your inputs would always be positive integers, use unsigned.
This is just a personal convention, and you're not bound to it. I like to define variables in the order bool, char, int, long int, float, double, ... so on.




# include &lt;iostream&gt;
# include &lt;cmath&gt;

# define ULI unsigned long int

int main () {
    bool is_prime;
    int num_cases;
    ULI cases[100][2];
    std::cin&gt;&gt;num_cases;

    for(int i = 0; i &lt; num_cases; i++)
       std::cin &gt;&gt; cases[i][0] &gt;&gt; cases[i][1];
    for(int k = 0; k &lt; num_cases; k++) {
        ULI m = cases[k][0],
            n = cases[k][1];
        for(ULI i = m; i &lt;= n; i++) {
            is_prime = true;
            ULI max = (ULI) sqrt(i) + 1;
            for(ULI j = 2; j &lt; i; j++)
                if(i % j == 0)
                    is_prime = false;
            if(is_prime)
                std::cout&lt;&lt;i&lt;&lt;std::endl;
        }
    }
    return 0;
}