Project Euler NO46

Christian Goldbach 提出每個奇合數都可以寫作一個質數與一個平方數的二倍之和:

9 = 7 + 2×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12

但是這個推測是錯誤的。

最小的不能寫作一個質數與一個平方數的二倍之和的奇合數是多少?


public class Problem46
{
	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()
	{
		l1 : for (int i = 35; ; i += 2)
		{
			if (iszhishu(i))
			{
				continue;
			}
			
			for (int j = 1; ; j++)
			{
				int t = 2 * j * j;
				if (t > i)
				{
					break;
				}
				
				if (iszhishu(i - t))
				{
					continue l1;
				}
			}
			
			System.out.println(i);
			break;
		}
	}
	
	static boolean iszhishu(int n)
	{
		for (int i = 2; i <= Math.sqrt(n); i++)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
		
		return true;
	}
}


answer:  5777
time:  8



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