In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentagonal number, I can only wonder if we have to deal with septagonal numbers in Problem 46. Anyway the problem reads

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

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

It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 – 22 = 48, is not pentagonal.

Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference is pentagonal and D = |Pk – Pj| is minimised; what is the value of D?

I have found a solution which I will present to you in just a few lines, but I must admit that I haven’t been able to justify why it is the right solution it gives us. The solution I have made just finds a solution to the problem which happens to the right solution.

I did not want to generate a list of pentagonal numbers, so I wanted to make a small function which checks if a given number is pentagonal by checking if the inverse function yields an integer, just like in the solution to  Problem 42. We could rather easily calculate the inverse function as we did with the inverse function for triangular numbers, or we can cheat and peak at the pentagonal number entry at Wikipedia.

The inverse function is

That enables us to make a C# function that checks if a number is pentagonal

1
2
3
4
private bool isPentagonal(int number) {
    double penTest = (Math.Sqrt(1 + 24 * number) + 1.0) / 6.0;
    return penTest == ((int)penTest);
}

Once we have this crucial function we can make two nested loops to check pentagonal numbers until we find two where the sum and difference are pentagonal as well. I am frustrated since I can’t figure out why this one is the first. I can prove that it is indeed minimal by testing other numbers until the difference of two numbers reach the result of this problem. However I haven’t done that.

The outer loop of the algorithm counts upwards generating and the inner loop counting downwards testing all pentagonal numbers less than the one generated by the outer loop. The code looks like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int result = 0;
bool notFound = true;
int i = 1;
 
while (notFound) {
    i++;
    int n = i * (3 * i - 1) / 2;
 
    for (int j = i-1; j > 0; j--) {
        int m = j * (3 * j - 1) / 2;
        if (isPentagonal(n - m) && isPentagonal(n + m)) {
            result = n-m;
            notFound = false;
            break;
        }
    }
}

and gives the result

1
2
3
k = 2167, j = 1020
The value of D is 5482660
Solution took 35 ms

Wrapping up

I can see that many other people also can’t give the definitive answer to why the result is correct. If you understand the problem better than I do, please let me know exactly why I find the right solution.

You can as usual find the source code for the problem here.

ref

Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.的更多相关文章

  1. Project Euler 44 Sub-string divisibility( 二分 )

    题意:五边形数由公式Pn=n(3n−1)/2生成,在所有和差均为五边形数的五边形数对Pj和Pk中,找出使D = |Pk − Pj|最小的一对:此时D的值是多少? 思路:二分找和差 /********* ...

  2. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  3. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  4. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  5. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  6. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  7. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  8. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  9. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

随机推荐

  1. PowerDesigner16 生成的备注脚本,在sql server 2008 中报“对象名 'sysproperties' 无效”的错误的解决办法

    主要是在建模时我们对表.列增加了些说明注释,而Sql2005之后系统表sysproperties已废弃删除而改用sys.extended_properties所致. 1.修改Table TableCo ...

  2. Attribute基本介绍

    一.基础知识点 1.什么是Attribute? MSDN:公共语言运行时允许你添加类似关键字的说明,叫做Attribute,它可以对程序中的元素进行标注,如类型.字段.方法和属性等.Attribute ...

  3. wap webapp app区别

    “手机WAP版网站”.“手机触屏版网站”.“手机APP应用软件”: wap webapp app区别 电脑版:台式机或者笔记本访问,兼容各个浏览器: Wap版:用于传统智能手机,屏幕小,适合使用手机键 ...

  4. Java学习之路(五):常见的对象操作

    Object对象 我们先来介绍一下API API(Application Programming Interface):应用程序编程接口 Java API 就是Java提供给我们使用的类,这些类将底层 ...

  5. redis 数据库迁移

    老大让把 一台机器上 redis 中所有的数据,迁移到另一台机器上 查了一下可以拷贝 rdb 文件, 此方法只适用于迁移到一个新的库, 迁移到正在使用的库就不行了, 而且 rdb 里面是所有的 db, ...

  6. Tomcat *的安装和运行(绿色版和安装版都适用)

    不多说,直接上干货! 前提, Tomcat *的下载(绿色版和安装版都适用) 一.Tomcat的安装版 1.新建安装目录 2.放置安装版的tomcat 3.双击 4.点击 I agree 5.选择“F ...

  7. Java中的语法树结构

    1.JCTypeParameter class B<T extends CA&IA&IB> { ...} 截图如下: 接口继承了StatementTree接口,而实现类实现 ...

  8. 数据库~Mysql里的Explain说明

    对于mysql的执行计划可以在select前添加Explain来实现,它可以告诉我们你的语句性能如何. 下面是对explain的具体说明,也都是官方的,以后进行参考. id SELECT识别符.这是S ...

  9. PHP之string之str_split()函数使用

    str_split (PHP 5, PHP 7) str_split - Convert a string to an array str_split - 将字符串转换为数组 Description ...

  10. android的计算器

    今天我闲着无聊,便想仿照Iphone的计算器自己写一个出来玩玩,于是就开动脑经,来场头脑风暴了!我拿什么了写呢?wp?这是个不错的选择,但是我最近在研究安卓的开发(求各位MSP不要烧我,我买不起MAC ...