The questions from hackerearth:
Geeko is in worry now because an exam is coming up and he has to know what rank he can get on exams. So he goes back into the the school records and finds the amazing pattern.
He finds that if a student is having a current rank \$n\$, then his rank on the final exam will be the count positive numbers between in the range \$[1,n]\$ which are relatively prime to \$n\$.
As being a geek, he became curious now and wants to calculate the rank of all his classmates on the final exam. But he finds this task a bit hard, so he asks you programmers to solve this task for him.
Input: The first line of each test file contains a integer \$t\$ denoting the number of test case. Each test case contains a numbers \$n\$ representing the current rank of each student
Output: for each test case output single integer the rank of student in final exam in new line.
Constraints:
\$1 <= t <= 2000\$
\$1 <= n < 10^6\$
My solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
class relative {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
ArrayList<Integer> input = new ArrayList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
// int input[] = new int[t];
// int result[] = new int[t];
for (int i = 0; i < t; i++) {
int count = 1;
int in;
input.add(in = Integer.parseInt(br.readLine()));
for (int j = 2; j < in; j++) {
if (in % j == 0)
continue;
else if (checkRelativePrime(j, in))
count++;
}
result.add(count);
}
for (int i : result)
System.out.println(i);
}
static boolean checkRelativePrime(int a, int b) {
int i = 2;
while (a > 1) {
if (a % i == 0) {
{
if (b % i == 0)
return false;
}
a = a / i;
} else
i++;
}
return true;
}
}