We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #72: Counting fractions
  4. Discussions

Project Euler #72: Counting fractions

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • Amrit07
    11 months ago+ 0 comments

    All test case pass java 8 ` import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        int N = 1000000;
        int[] phi = new int[N + 1];
        for(int i = 0; i < phi.length; i++) {
            phi[i] = i;
        }
        for(int i = 2; i <= N; i++) {
            if(phi[i] == i) {
                for(int k = 1; k * i <= N; k++) {
                    phi[k * i] -= phi[k * i] / i;
                }
            }
        }
        long[] sums = new long[phi.length];
        for(int i = 2; i <= N; i++) {
            sums[i] = sums[i - 1] + phi[i];
        }
        try(Scanner sc = new Scanner(System.in)) {
            int T = sc.nextInt();
            while(T > 0) {
                N = sc.nextInt();
                System.out.println(sums[N]);
                T--;
            }
        }
    }
    

    }

    0|
    Permalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature