题目链接:https://projecteuler.net/problem=72

 

真分数;n/d

d ≤ 1,000,000时候的真分数有多少个

 

public class P72{
    void run(){
        int max_n = 1000000;
        long[] phi = cal_phi(max_n+1);
        long sum=0;
        phi[1] = 0;
        for(int i =1;i<=max_n;i++)
            sum+=phi[i];
        System.out.println(sum);
    }
//    303963552391
//    194ms
    long[] cal_phi(int max_n){
        long[] phi = new long[max_n+1];
        for(int i=1;i<max_n;i++){
            phi[i] += i;
            for(int j =2*i;j<max_n;j+=i)
                phi[j]-=phi[i];
        }
        return phi;
    }
    public static void main(String[] args){
        long t0 = System.currentTimeMillis();
        new P72().run();
        long t1= System.currentTimeMillis();
        System.out.println((t1-t0)+"ms");
    }
}