Java实现除去次方数

** 除去次方数**

自然数的平方数是:1 4 9 16 25 …
自然数的立方数是:1 8 27 64 125 …
自然数的4次方数是:1 16 81 256 …

这些数字都可以称为次方数。
1~10000中,去掉所有的次方数,还剩下多少个数字?

参考答案:
9875

import java.util.ArrayList;

public class Main {
    
    public void printResult() {
        ArrayList<Long> list = new ArrayList<Long>();
        for(long i = 1;i <= 10000;i++)
            list.add(i);
        int count = 2;
        while(true) {
            for(long i = 1;i <= 100;i++) {
                long a = (long) Math.pow(i, count);
                if(a > 10000)
                    break;
                if(list.contains(a)) {
                    list.remove(list.indexOf(a));
                }
            }
            count++;
            if(count > 50)
                break;
        }
        System.out.println(list.size());
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.printResult();
    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/12947346.html