samedi 4 juillet 2020

Code produces the wrong output for the problem below

Expected output is 4 but i am getting 3

Four functions You are given four functions:

A(x) = Number of i where 1 <= i <= x and gcd(i,x)=1

B(x) = Sum of A(d) where d divides the number x

c(x)= Sum of exponents of each prime in the prime factorization of B(x)

D(x) = Sum of C(i) where 1 <= i <= x

If B(x)=p1^a1 p2^a2 and pk^ak here P1,P2,...,Pk are prime factors of B(x), then C(x) is a1 + a2+...+ ak.

You are given an integer N. Your task is to find the value of D(N).

Input format

The first line contains T denoting the number of test cases.

The first line of each test case consists of an integer N.

Output format

For each test case. print a single integer denoting the value of D(N) in a new line.

Constraints

1 <= T <= lO^6 1 <= N <= 10^6

Sample Input 

1
4

Sample Output

4

Explanation 

For n = 4 
C(1)= 0
C(2)= 1
C(3)= 1
C(4)= 2
D(4)=C(1)+C(2)+C(3)+C(4) = 4

So ans will  be 4

Here is my solution

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int n = sc.nextInt();
        for(int i=0;i<t;i++){
            int sum_d = 0, sum, count;
            for(int q=1;q<=n;q++){
                sum = 0;
                count = 0;
                 int ans = b(q);
                while (ans%2 == 0){
                    count++;
                    ans /= 2;
                }
                sum += count;
                int j = 3;
                while(j*j <= ans){
                    count = 0;
                    while(ans%j == 0){
                        count++;
                        ans /= j;
                    }
                    sum += count;
                    j += 2;
                }
                sum_d += sum;
            }

            System.out.println(sum_d);
        }
    }



    public static int gcd( int j,  int n){
        if (j == n)
            return j;
        if (j > n)
            return gcd(j-n, n);
        return gcd(j, n-j);
    }
    public static int b(int n){
        int counter, sum_ = 0;
        for(int i=1;i<=n;i++){
            if (n % i == 0){
                counter = 0;
                for(int j=1;j<=i;j++){
                    int g = gcd(j, n);
                    if (g == 1)
                        counter++;
                }
                sum_ += counter;
            }
        }
        return sum_;
    }
}


Aucun commentaire:

Enregistrer un commentaire