<MEMORY>Project Euler NO44

利用以下的公式可以测试一个正整数x是否是五边形数

n = \frac{\sqrt{24x+1} + 1}{6}.
  • 若n是自然数,则x是五边形数,而且恰为第n个五边形数。
  • 若n不是自然数,则x不是五边形数。

摘自维基百科

五角数通过如下公式定义:Pn=n(3n−1)/2。前十个五角数是:

1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...

可以看出P4 + P7 = 22 + 70 = 92 = P8. 但是它们的差70 − 22 = 48却不是五角数。

找出最小的五角数对Pj 和 Pk,, 使得它们的和与差都是五角数,并且D = |Pk − Pj| 取到最小。这时D的值是多少?


public class Problem44
{
	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 = 2;; i++)
		{
			l2 : for (int j = i - 1; j > 0; j--)
			{
				int t1 = i * (3 * i - 1) / 2;
				int t2 = j * (3 * j - 1) / 2;
				int add = t1 + t2;
				int subtration = t1 - t2;

				double tempadd = (Math.sqrt(24 * add + 1) + 1) / 6;
				double tempsub = (Math.sqrt(24 * subtration + 1) + 1) / 6;
				
				if ((int)tempadd != tempadd)
				{
					continue l2;
				}
				if ((int)tempsub != tempsub)
				{
					continue l2;
				}
				System.out.println(subtration);
				break l1;
			}
		}
	}

}



answer:  5482660
time:  67


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值