HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Calculating the ranks of classmates' exam grades

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classmatesgradestheranksexamcalculating

Problem

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 input = new ArrayList();

        ArrayList result = new ArrayList();
        // int input[] = new int[t];
        // int result[] = new int[t];
        for (int i = 0; i  1) {
            if (a % i == 0) {
                {
                    if (b % i == 0)
                        return false;
                }
                a = a / i;
            } else
                i++;
        }

        return true;
    }
}

Solution

in your checkRelativePrime method you have

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;
}


  • you have an extra set of brackets



-
you have a statement in purgatory.

a = a / i;


it sits in-between the ending bracket of your if statement and the else statement that is supposed to be attached to the if statement

-
if you use brackets for one part of an if statement, you should use them for the entire if statement. I recommend using brackets even for one-liners no matter what, it's cleaner and leaves less room for mistakes.

I think you wanted to keep that if statement separate from the declaration to the a variable, but you don't need to do that(especially if you are using brackets properly)

like this

while (a > 1) {
    if (a % i == 0) {
        if (b % i == 0) {
            return false;
        }
        a = a / i;
    } else {
        i++;
    }
}


if you enter that if statement and return false then a = a /i; will never happen and your boolean method will return false

Code Snippets

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;
}
while (a > 1) {
    if (a % i == 0) {
        if (b % i == 0) {
            return false;
        }
        a = a / i;
    } else {
        i++;
    }
}

Context

StackExchange Code Review Q#54405, answer score: 3

Revisions (0)

No revisions yet.