Profile photo for Shivam Saxena

For Java Programmers :)

  1. import java.util.*; 
  2. import java.lang.*; 
  3. import java.io.*; 
  4.  
  5. public class Main 
  6. { 
  7. static class FastReader  
  8. {  
  9. BufferedReader br;  
  10. StringTokenizer st;  
  11.  
  12. public FastReader()  
  13. {  
  14. br = new BufferedReader(new 
  15. InputStreamReader(System.in));  
  16. }  
  17.  
  18. String next()  
  19. {  
  20. while (st == null || !st.hasMoreElements())  
  21. {  
  22. try 
  23. {  
  24. st = new StringTokenizer(br.readLine());  
  25. }  
  26. catch (IOException e)  
  27. {  
  28. e.printStackTrace();  
  29. }  
  30. }  
  31. return st.nextToken();  
  32. }  
  33.  
  34. int nextInt()  
  35. {  
  36. return Integer.parseInt(next());  
  37. }  
  38.  
  39. long nextLong()  
  40. {  
  41. return Long.parseLong(next());  
  42. }  
  43.  
  44. double nextDouble()  
  45. {  
  46. return Double.parseDouble(next());  
  47. }  
  48.  
  49. String nextLine()  
  50. {  
  51. String str = "";  
  52. try 
  53. {  
  54. str = br.readLine();  
  55. }  
  56. catch (IOException e)  
  57. {  
  58. e.printStackTrace();  
  59. }  
  60. return str;  
  61. }  
  62. }  
  63. static int MOD=1000000000+7; 
  64.  
  65. //Brian Kernighan’s Algorithm 
  66. static long countSetBits(long n){ 
  67. if(n==0) return 0; 
  68. return 1+countSetBits(n&(n-1)); 
  69. } 
  70. //Factorial 
  71. static long factorial(long n){ 
  72. if(n==1) return 1; 
  73. if(n==2) return 2; 
  74. if(n==3) return 6; 
  75. return n*factorial(n-1); 
  76. } 
  77. //Euclidean Algorithm 
  78. static long gcd(long A,long B){ 
  79. if(B==0) return A; 
  80. return gcd(B,A%B); 
  81. } 
  82. //Modular Exponentiation 
  83. static long fastExpo(long x,long n){ 
  84. if(n==0) return 1; 
  85. if((n&1)==0) return fastExpo((x*x)%MOD,n/2); 
  86. return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2)); 
  87. } 
  88. //AKS Algorithm 
  89. static boolean isPrime(long n){ 
  90. if(n<=1) return false; 
  91. if(n<=3) return true; 
  92. if(n%2==0 || n%3==0) return false; 
  93. for(int i=5;i*i<=n;i+=6) 
  94. if(n%i==0 || n%(i+2)==0) return false; 
  95. return true; 
  96. } 
  97. //Sieve of eratosthenes 
  98. static int[] findPrimes(int n){ 
  99. boolean isPrime[]=new boolean[n+1]; 
  100. ArrayList<Integer> a=new ArrayList<>(); 
  101. int result[]; 
  102. Arrays.fill(isPrime,true); 
  103. isPrime[0]=false; 
  104. isPrime[1]=false; 
  105. for(int i=2;i*i<=n;++i){ 
  106. if(isPrime[i]==true){ 
  107. for(int j=i*i;j<=n;j+=i) isPrime[j]=false; 
  108. } 
  109. } 
  110. for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i); 
  111. result=new int[a.size()]; 
  112. for(int i=0;i<a.size();i++) result[i]=a.get(i); 
  113. return result; 
  114.  
  115. } 
  116. //Euler Totent function 
  117. static long countCoprimes(long n){ 
  118. ArrayList<Long> prime_factors=new ArrayList<>(); 
  119. long x=n,flag=0; 
  120. while(x%2==0){ 
  121. if(flag==0) prime_factors.add(2L); 
  122. flag=1; 
  123. x/=2; 
  124. } 
  125. for(long i=3;i*i<=x;i+=2){ 
  126. flag=0; 
  127. while(x%i==0){ 
  128. if(flag==0) prime_factors.add(i); 
  129. flag=1; 
  130. x/=i; 
  131. } 
  132. } 
  133. if(x>2) prime_factors.add(x); 
  134. double ans=(double)n; 
  135. for(Long p:prime_factors){ 
  136. ans*=(1.0-(Double)1.0/p); 
  137. } 
  138. return (long)ans; 
  139. } 
  140. public static void main (String[] args) throws java.lang.Exception 
  141. { 
  142. FastReader sc=new FastReader(); 
  143. } 
  144. } 
View 38 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025