fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. //int n, m;
  15. Scanner in = new Scanner(System.in);
  16.  
  17. final int MAXN = 10000001;
  18. int simple[]=new int[MAXN+1];
  19. for(int i = 2; i <= MAXN; i++){
  20. simple[i] = 1;
  21. }
  22. for(int i = 2; i <= Math.sqrt(MAXN); i++){
  23. if(simple[i]==1){
  24. for(int j = i * i; j <= MAXN; j += i){
  25. simple[j] = 0;
  26. }
  27. }
  28. }
  29.  
  30. int count[]=new int[MAXN+1];
  31. int lastSum = 0;
  32. for(int i = 1; i < MAXN; i++){
  33. count[i] = lastSum + simple[i];
  34. lastSum = count[i];
  35. }
  36. while(in.hasNextInt()) {
  37. int m = in.nextInt();
  38. int n = in.nextInt();
  39. int answer = count[n] - count[m] + simple[m];
  40. System.out.println();
  41. System.out.println(answer);
  42. }
  43. }
  44. }
Success #stdin #stdout 0.22s 2184192KB
stdin
2 20
70 110
5 150
stdout
8

10

33