欧拉计划44题

 

Pentagon numbers

Pentagonal numbers are generated by the formula, P_n = n * (3 * n - 1) / 2. The first ten pentagonal numbers are:

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

It can be seen that P_4 + P_7 = 22 + 70 = 92 = P_8. However, their difference, 70−22=48, is not pentagonal.

Find the pair of pentagonal numbers, P_j and P_k, for which their sum and difference are pentagonal and D=|P_k - P_j| is minimised; what is the value of D?

五边形数

五边形数由公式P_n = n * (3 * n - 1) / 2给出。前十个五边形数是:

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

可以看出P_4 + P_7 = 22 + 70 = 92 = P_8。然而,它们的差70−22=48并不是五边形数。

在所有和差均为五边形数的五边形数对P_jP_k中,找出使D=|P_k - P_j|最小的一对;此时D的值是多少?

        这个题的难度不在于求D的值,在于枚举上界,那么问题的上界是多少?

        假设D就是求到的最小解

        假设k比j大,那么P_k - P_{k - 1} >= D说明k没有枚举的必要了 ,P_k - P_j >= D说明j没有枚举的必要了;

        为什么因为D要求的是最小值,而P_n会随着n的变大而变大那么,P_k - P_{k - 1} = 3k - 2通过发现,P_k - P_{k - 1}会随着k增大而增大;

        而j是从k-1开始的,那么反过来,j越小,那么P_k - P_{j}就越大,说明也没有枚举的必要了,假如j = k-1时,P_k - P_j >= D同理P_k - P_{k - 1} >= D

        通过上面的两个推倒可以发现,求到的第一个满足差和都位5边形数就是最小的D值;

        那么下面是代码实现:

 

#include <stdio.h>
#include <math.h>
#define MAX_N 1000000

double func(int x) {//五边形数的反函数
    return (sqrt(2.0 * x / 3 + 1.0 / 36.0) + 1.0 / 6.0);
}

int Pentagon(int x) {//求五边形数函数
    return x * (3 * x - 1) / 2;
}

int is_true(int i, int j) {
    long long p_k = Pentagon(i);
    long long p_j = Pentagon(j);
    double num1 = func(p_k - p_j);
    double num2 = func(p_k + p_j);
    if (num1 != floor(num1)) return 0;//没有找到他们和的五边形数
    if (num2 != floor(num2)) return 0;//没有找到他们差的五边形数
    return 1;
}


int main() {
    int flag = 0;
    for (int i = 2; i <= MAX_N; i++) {
        for (int j = i - 1; j > 0; j--) {
            if (!is_true(i, j)) continue;
            printf("%d\n", Pentagon(i) - Pentagon(j));
            flag = 1;
            break;
        }    
        if (flag) break;
    }
    return 0;
}

 最终答案:5482660

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初猿°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值