Project Euler NO29

考慮 ab 在 2 ≤ a ≤ 5,2 ≤ b ≤ 5下的所有整數組合:


22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
如果將這些數字排序,並去除重複的,我們得到如下15個數字的序列:


4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125


ab 在 2 ≤ a ≤ 100,2 ≤ b ≤ 100 下生成的序列中有多少個不同的項?




考慮 ab 在 2 ≤ a ≤ 5,2 ≤ b ≤ 5下的所有整數組合:

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
如果將這些數字排序,並去除重複的,我們得到如下15個數字的序列:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

ab 在 2 ≤ a ≤ 100,2 ≤ b ≤ 100 下生成的序列中有多少個不同的項?



時間過長大哭
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;


public class Problem29
{
	public static void main(String[] args)
	{
		long start = System.currentTimeMillis();
		System.out.print("answer:  ");
		
		howmany();
		
		long end = System.currentTimeMillis();
		System.out.print("time:  ");
		System.out.println(end - start);
	}
	
	static void howmany()
	{
		List<BigInteger> all = new ArrayList<>();
		for (int a = 2; a <= 100; a++)
		{
			for (int b = 2; b<= 100; b++)
			{
				all.add(BigInteger.valueOf(a).pow(b));
			}
		}
		
		for (int i = 0; i < all.size(); i++)
		{
			for (int j = i + 1; j < all.size(); j++)
			{
				if (all.get(i).equals(all.get(j)))
				{
					all.remove(j);
					j--;
				}
			}
		}
		
		System.out.println(all.size());
	}
}

answer:  9183
time:  1065

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章