Showing posts with label Prime Sum. Show all posts
Showing posts with label Prime Sum. Show all posts

Saturday, July 2, 2022

Prime Sum

 Prime Sum

Problem Statement
Given a range from l to r, Your task is to find the sum of the second last digit of the prime numbers between that range.


import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    static int n = 100000;
    static boolean isPrime[] = new boolean[n+1]; // O(n)

    static void primeSeive(){
        //bit arr
        //assume all are primes
        for(int i=0;i<=n;i++){
            isPrime[i] = true;
        }
        //mark 0 and 1 as not prime
        isPrime[0] = isPrime[1] = false;
        int sqrt = (int)Math.sqrt(n);
        // 2..sqrt(n)
        for(int i=2;i<=sqrt;i++){
            if(isPrime[i]){
                for(int j=i*i;j<=n;j=j+i){
                    isPrime[j] = false;
                }
            }
        }
    }
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        primeSeive();
        for(int t=0;t<tc;t++){
            int l = sc.nextInt();
            int r = sc.nextInt();
            int sum = 0;
            for(int i=l;i<=r;i++){
                if(i>=10 && isPrime[i]){
                    sum = sum + (i/10)%10;
                }
            }
            System.out.println(sum);
        }
                      // Your code here
    }
}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...