Sort by

recency

|

94 Discussions

|

  • + 0 comments

    int solve(long long n, long long m) { // Prime flags in the interval between n and m // It's like Eratosthenes sieve but with offset vector intervalNumbers(m - n + 1, true); if (intervalNumbers.size() < 2) { return 0; } if (n == 1) { intervalNumbers[0] = false; }

    // Eratosthenes sieve from 0 to square root of m
    const int lim = sqrt(m);
    vector<bool> eratosthenesSieve(lim + 1, true);
    eratosthenesSieve[0] = false;
    eratosthenesSieve[1] = false;
    
    // Set flags + add primes to vector
    vector<long long> primes;
    for(int i = 2; i <= lim; ++i) {
        if(eratosthenesSieve[i]) {
            primes.push_back(i);
            for(int j = i * 2; j <= lim; j += i) {
                eratosthenesSieve[j] = false;
                if ( (j >= n) && (j <= m) ) {
                    intervalNumbers[j - n] = false;
                }
            }
        }
    }
    
    // Set flags for the interval 
    for(const long long i: primes) {
        for(int j = max(i * 2, (n + i  - 1) / i * i); j <= m; j += i) {
            intervalNumbers[j - n] = false;            
        }
    }
    
    // Calculating
    long long int twinCount = 0;
    for(size_t i = 0 ; i < intervalNumbers.size() - 2; ++i) {
        if(intervalNumbers[i] && intervalNumbers[i + 2]) {
            ++twinCount;
        }
    } 
    
    return twinCount;
    

    }

  • + 0 comments

    Looking for unique name tags in Dubai? Custom Patches UAE offers personalized name badges that enhance your branding and make your staff stand out. Our high-quality materials and custom designs ensure your name tag reflects expertise. Perfect for events, conferences, or everyday work.

  • + 0 comments
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class Solution {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		System.out.println(solve(n, m));
    
    		sc.close();
    	}
    
    	static int solve(int n, int m) {
    		int size = m - n + 1;
    		List<Integer> primeDivisors = sievePrimes(size);
    
    		boolean[] primes = new boolean[size];
    		Arrays.fill(primes, true);
    
    		for (int primeDivisor : primeDivisors) {
    			for (int i = n / primeDivisor * primeDivisor; i <= m; i += primeDivisor) {
    				if (i >= n && i > primeDivisor) {
    					primes[i - n] = false;
    				}
    			}
    		}
    
    		int result = 0;
    		for (int i = 0; i + 2 < primes.length; i++) {
    			if (n + i >= 2 && primes[i] && primes[i + 2]) {
    				result++;
    			}
    		}
    		return result;
    	}
    
    	static List<Integer> sievePrimes(int limit) {
    		boolean[] primes = new boolean[limit + 1];
    		Arrays.fill(primes, true);
    
    		List<Integer> result = new ArrayList<Integer>();
    		for (int i = 2; i < primes.length; i++) {
    			if (primes[i]) {
    				result.add(i);
    
    				for (long j = (long) i * i; j < primes.length; j += i) {
    					primes[(int) j] = false;
    				}
    			}
    		}
    		return result;
    	}
    }
    
  • + 0 comments

    It's great to see articles like this, I strands nyt really like the way you present the content so neatly and so easy to understand. If you can regularly publish good content like this, it will definitely be something that not only me but I think everyone will think the same.

  • + 0 comments

    good nice.