fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. static boolean isprime(int n)
  12. {
  13. if(n==2)
  14. return true;
  15. else if(n%2==0)
  16. return false;
  17. for(int i=3;i<=Math.sqrt(n);i+=2)
  18. {
  19. if(n%i==0)
  20. return false;
  21. }
  22. return true;
  23. }
  24. public static void main (String[] args) throws java.lang.Exception
  25. {
  26. // your code goes here
  27. Scanner sc=new Scanner(System.in);
  28. int t=sc.nextInt();
  29. while(t-->0)
  30. {
  31. int n=sc.nextInt();
  32.  
  33. // 2 is the only even prime no.
  34. if(n==2)
  35. System.out.println("1");
  36.  
  37. /* Goldbach theo
  38. -> any even no. can be represented as sum of 2 primes
  39. */
  40. else if(n%2==0)
  41. System.out.println("2");
  42.  
  43. // if num is odd
  44. else
  45. {
  46. // if num is odd and prime , then as we know ans for prime =1
  47. if(isprime(n))
  48. System.out.println("1");
  49.  
  50. /* if the num is odd it can be represented as
  51. odd = eve+odd (here eve , odd both shls be prime)
  52.  
  53. the only even prime is 2 so we need to check if n-2 is prime
  54. if its prime then ans=2
  55. */
  56. else if(isprime(n-2))
  57. System.out.println("2");
  58. /* goldbach told any odd num can be represented as sum of 1, 2 or 3 prime numbers , we
  59. already tries cases for 1, 2
  60. so the remaining answer is 3
  61. */
  62. else
  63. System.out.println("3");
  64. }
  65. }
  66. }
  67. }
Success #stdin #stdout 0.15s 54532KB
stdin
3
2
5
8
stdout
1
1
2