How to find number of prime numbers between two integers

Let $\pi(x) = \#\{p\leq x \mid p \mbox{ is prime}\}$ be the prime counting function. The Prime Number Theorem tells us that $$ \pi(x) \sim \frac{x}{\log x}. $$ (That is $\lim_{x\to \infty} \frac{\pi(x)}{x/\log x}=1$.) So, roughly speaking, around a large $x$, the probability that an integer is a prime is $1/\log x$. Thus, naively, one may expect that the number of primes in an interval $(x,y]$, for large $x$ is about $(y-x)/\log x$, and in a heuristic formula, $$ \pi(y)-\pi(x)\sim \frac{(y-x)}{\log x} = \frac{h}{\log x}. \qquad(*) $$ Here $h=y-x$ is the length of the interval. This heuristic makes senses only for $h$ which is much bigger than $\log x$.

From the Prime Number Theorem $(*)$ holds if $h\sim \lambda x$, where $\lambda>0 $ is fixed. From Riemann Hypothesis $(*)$ holds for $h\sim x^{1/2+\epsilon}$ for any fixed $\epsilon>0$. (Because the RH gives the error term in the PNT.) There are unconditional results by Huxley and Heath-Brown showing $(*)$ for $h$ roughly being $x^{7/12}$.

If $h=\log x \frac{\log \log x \cdot \log\log\log\log x}{\log\log\log x}$, then $(*)$ fails for a sequence $x_n \to \infty$. To deal with `small' intervals Selberg worked with almost all $x$. Namely he considered $(*)$ for all $x\in \mathbb{R}_{+}\smallsetminus S$, where $|S\cap (0,x]|=o(x)$. In this sense $(*)$ holds if $h/\log^2 x\to 0$ conditionally on RH and for $h=x^{19/77+\epsilon}$ unconditionally.

There are also works on the case $h\sim\lambda \log x$. There the distribution of the number of primes on intervals of this size is Poission with parameter $\lambda$, conditionally on the Hardy-Littlewood prime tuple conjecture. I think this is due to Gallagher.


Finding the number of prime numbers between two given numbers $x$ and $y$ such that $x$ < $y$

int count_primes_between(int x, int y) {    
    int j, i, count=0;
    int flag;

    for(i = x; i <= y; i++) {
        flag = 0;
        for(j = 2; j <= i/2; j++) {
            if(!(i%j)) {
                flag = 1;
                break;
            }
            if(!flag) count++;  
        }   
    }
    return count;
}