Project Euler #72: Counting fractions

Sort by

recency

|

16 Discussions

|

  • + 1 comment

    Python Solution.

    lookup=[i for i in range(10**6+1)]
    for i in range(2,10**6+1):
        if lookup[i]==i:
            for j in range(i,10**6,i):
                lookup[j]-=lookup[j]//i
    for i in range(3,10**6):
        lookup[i]+=lookup[i-1]
    t=int(input().strip())
    for _ in range(t):
        n=int(input().strip())
        print(lookup[n])
    
  • + 0 comments

    javascript solution ?

  • + 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 comments

    All test case pass ` 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 comments

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

    }