View Code of Problem 27

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String[] split = scanner.nextLine().split(" ");
            Integer v1 = Integer.valueOf(split[0]);
            Integer v2 = Integer.valueOf(split[1]);
            if (v1 > v2) {
                int temp = v2;
                v2 = v1;
                v1 = temp;
            }
            int sum = 0;
            for (int i = v1 + 1; i < v2; i++) {
                if(i == 1){
                    continue;
                }
                if (i == 2) {
                    sum += i;
                    continue;
                }
                boolean flag = false;
                for (int j = 2; j <= Math.sqrt(i) + 1; j++) {
                    if (i % j == 0) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    sum += i;
                }
            }
            System.out.println(sum);
        }
    }
}

Double click to view unformatted code.


Back to problem 27