鱼C论坛

 找回密码
 立即注册
查看: 2974|回复: 14

题目45:在40755之后最小的既是五角数又是六角数的三角数是多少?

[复制链接]
发表于 2015-5-16 23:21:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 欧拉计划 于 2015-5-16 23:25 编辑
Triangular, pentagonal, and hexagonal

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle                BaiduShurufa_2015-5-16_23-15-3.png                1, 3, 6, 10, 15, ...
Pentagonal          BaiduShurufa_2015-5-16_23-15-29.png                1, 5, 12, 22, 35, ...
Hexagonal            BaiduShurufa_2015-5-16_23-16-20.png                  1, 6, 15, 28, 45, ...

It can be verified that BaiduShurufa_2015-5-16_23-11-15.png .

Find the next triangle number that is also pentagonal and hexagonal.

题目:

三角数,五角数和六角数分别通过以下公式定义:

三角数                BaiduShurufa_2015-5-16_23-15-3.png                 1, 3, 6, 10, 15, ...
五角数                BaiduShurufa_2015-5-16_23-15-29.png                1, 5, 12, 22, 35, ...
六角数                BaiduShurufa_2015-5-16_23-16-20.png                   1, 6, 15, 28, 45, ...

可以证实 BaiduShurufa_2015-5-16_23-11-15.png .

找出这之后的下一个既是五角数又是六角数的三角数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-22 23:55:49 | 显示全部楼层
  1. Tn= T 55385
  2. Pn= P 31977
  3. Hn= H 27693
  4. 1533776805.0
  5. [Finished in 36.8s]

  6. Tn, Pn, Hn = [],[],[]
  7. for n in range(286,100000):
  8.         Tn.append (n * (n+1) / 2)
  9. for n in range(286,100000):
  10.         if n * (2 * n - 1) <= Tn[-1]:
  11.                 Pn.append (n * (3 * n - 1) / 2)
  12.                 Hn.append (n * (2 * n - 1))
  13. for each in Tn:
  14.         if each in Pn and each in Hn:
  15.                 print ('Tn= T',Tn.index(each)+286)
  16.                 print ('Pn= P',Pn.index(each)+286)
  17.                 print ('Hn= H',Hn.index(each)+286)
  18.                 print (each)
  19.                 break
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-12 14:28:47 | 显示全部楼层
换了个算法,通过六角数反过来测试五角数和三角数,快了近1000倍!!!
大概30多毫秒出结果~
  1. import time

  2. def triangular(n): # Function to test if a number is triangular
  3.     if ((8*n + 1)**0.5 - 1) % 2 == 0:
  4.         return True
  5.     else:
  6.         return False

  7. def pentagonal(n): # Function to test if a number is pentagonal
  8.     if (1 + (24*n + 1)**0.5) % 6 == 0:
  9.         return True
  10.     else:
  11.         return False

  12. def hexagonal(n): # Function to test if a number is hexagonal
  13.     if (1 + (8*n + 1)**0.5) % 4 == 0:
  14.         return True
  15.     else:
  16.         return False
  17. p = 41328 # This number is the 144th hexagonal number. This is why f = 144.
  18. f = 144
  19. start = time.time()
  20. while True:
  21.     if triangular(p) == True and pentagonal(p) == True and hexagonal(p) == True:
  22.         break
  23.     f += 1
  24.     p = f*(2*f - 1) # Change p into the next hexagonal number
  25. print(str(p) + " found in an amazing %s milliseconds!" % ("{0:.2f}".format((time.time() - start) * 1000)))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-10-25 20:16:04 | 显示全部楼层
本帖最后由 飘飞的白杨 于 2016-10-25 20:17 编辑

六角数一定是三角数
第 n个六边形数同时是第 2n-1个三角形数
  1. n = 144
  2. while True:
  3.     m = n*(2*n-1)
  4.     if ((m*24+1)**0.5+1)%6 == 0:
  5.         print(m)
  6.         break
  7.     n += 1
复制代码

答案:1533776805
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-17 19:47:32 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:40 编辑
  1. s = set((i*(i+1)/2) for i in range(1,250000))

  2. t = set((i*(3*i-1)/2) for i in range(1,250000))

  3. n = set((i*(2*i-1)) for i in range(1,250000))

  4. for i in s:
  5.     if i in t and i in n:
  6.         print(i)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-17 19:48:38 | 显示全部楼层
  1. s = set((i*(i+1)/2) for i in range(1,250000))

  2. t = set((i*(3*i-1)/2) for i in range(1,250000))

  3. n = set((i*(2*i-1)) for i in range(1,250000))

  4. for i in s:
  5.     if i in t and i in n:
  6.         print(i)
复制代码

1.0
40755.0
1533776805.0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-16 10:22:40 | 显示全部楼层
  1. # encoding:utf-8
  2. # 找出40755下一个既是三角数、又是六角数和五角数的数字
  3. from time import time
  4. def euler045():
  5.     h_n = 143
  6.     p_n = 165
  7.     t_n = 185
  8.     while True:
  9.         h_n += 1
  10.         Hn = h_n * (2 * h_n - 1)
  11.         Pn = int(p_n * (3 * p_n - 1) / 2)
  12.         while Pn < Hn:
  13.             p_n += 1
  14.             t_n += 1
  15.             Pn = int(p_n * (3 * p_n - 1) / 2)
  16.         if Pn == Hn:
  17.             Tn = int(t_n * (t_n + 1) / 2)
  18.             while Tn < Hn:
  19.                 t_n += 1
  20.                 Tn = int(t_n * (t_n + 1) / 2)
  21.             if Tn == Hn:
  22.                 print('T(%d) = P(%d) = H(%d) = %d' % (t_n, p_n, h_n, Hn))
  23.                 return
  24. if __name__ == '__main__':
  25.     start = time()
  26.     euler045()
  27.     print('cost %.6f sec' % (time() - start))
复制代码

T(55385) = P(31977) = H(27693) = 1533776805
cost 0.046000 sec
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-16 10:34:27 | 显示全部楼层
百度了一下,才明白了3、4楼两位老大的公式,一元二次方程求解完全忘掉了...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-13 09:48:17 | 显示全部楼层
此代码使用matlab编程
Problem45所用时间为1.8334秒
Problem45的答案为1533776805
  1. %% Problem45.m
  2. %最后编辑时间:2017-06-13 9:51 版本1.0
  3. %找出40755后的数,这个数满足同时为三角型,五角型,六角形数
  4. %由于六角形数必定为三角性数,所以不需要搜索三角型数
  5. % Problem45所用时间为1.8334秒
  6. % Problem45的答案为1533776805
  7. function Output = Problem45()
  8. tic

  9. Meet = 0;

  10. Start = 144;

  11. while (Meet == 0)
  12.     Hexa = Start * (2*Start - 1);
  13.     if  IsPentagon(Hexa) == 1
  14.         Meet = 1;
  15.         break
  16.     end
  17.     Start = Start + 1;
  18. end

  19. Output = Hexa;
  20. toc

  21. disp('此代码使用matlab编程')
  22. disp(['Problem45所用时间为',num2str(toc),'秒'])
  23. disp(['Problem45的答案为',num2str(Output)])
  24. end
  25. %% IsPentagon.m
  26. % 测试一个数是否为五角星
  27. % 最后编辑时间:17-06-13 9:03 版本:1.0
  28. % 是五角型数则返回 1,否则返回 0
  29. function Judge = IsPentagon(n)
  30. if nargin == 0
  31. n = 22;
  32. end
  33. if n == 1
  34.     Judge = 1;
  35. else
  36.     front = floor(sqrt(2*n)/2);
  37.     after = ceil(sqrt(2*n)/1.7);
  38.     Judge = 0;
  39.     for ii = after:-1:front
  40.         if n == ((3*ii - 1) * ii)/2
  41.             Judge = 1;
  42.             break
  43.         end
  44.     end
  45. end
  46. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-30 10:56:13 | 显示全部楼层
用的matlab
结果是:
1533776805
时间已过 0.002740 秒。
>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-6 09:32:36 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2021-9-1 10:39 编辑
  1. #include<iostream>
  2. using namespace std;



  3. int main() {
  4.     ios::sync_with_stdio(false);
  5.     unsigned int pentagonal = 40755, pentagonal_n = 165, hexagonal = 40755, hexagonal_n = 143;


  6.     do {
  7.         if (hexagonal > pentagonal) {
  8.             pentagonal += 3 * pentagonal_n + 1;
  9.             pentagonal_n++;
  10.         }
  11.         else {
  12.             hexagonal += (hexagonal_n << 2) + 1;
  13.             hexagonal_n++;
  14.         }
  15.     } while (hexagonal - pentagonal);


  16.     cout << hexagonal << endl;
  17.     return 0;
  18. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-14 10:57:12 | 显示全部楼层
31977 1533776805

Process returned 0 (0x0)   execution time : 0.053 s
Press any key to continue.
分析可知六角数一定是三角数,有H_k=T_2k-1
因此只需验证五角数是否为六角数
暴力打表,二分搜索
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long ll;

  5. const int M = 50000;
  6. ll p[M] = {0},h[M] = {0};

  7. int main(){

  8.   for (int i = 1;i < M;i++){
  9.     p[i] = (ll)i*(3*i-1)/2;
  10.     h[i] = (ll)i*(2*i-1);
  11.         }

  12.         for (int i = 166;i < M;i++){
  13.     if (binary_search(h,h+M,p[i]))
  14.       cout << i << " " << p[i] << endl;
  15.         }

  16.         return 0;
  17. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 19:02:31 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>

  3. int check_Pentagonal(int);
  4. int check_Pentagonal(int num)
  5. {
  6.         double j;
  7.         int i;
  8.         j = (sqrt(24 * (double)num + 1) + 1) / 6;
  9.         i = (sqrt(24 * (double)num + 1) + 1) / 6;
  10.        
  11.         if ((double)i == j)
  12.         {
  13.                 return 1;
  14.         }
  15.         else
  16.         {
  17.                 return 0;
  18.         }
  19. }


  20. main()
  21. {
  22.         int i = 143, j;
  23.         while (1)
  24.         {
  25.                 i++;
  26.                 j = i * (2 * i - 1);
  27.                 if (check_Pentagonal(j))
  28.                 {
  29.                         printf("%u", j);
  30.                         break;
  31.                 }
  32.         }
  33. }
复制代码


1533776805
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-22 15:10:06 | 显示全部楼层
#在40755之后最小的既是五角数又是六角数的三角数是多少
from time import *
'''三角数,五角数和六角数分别通过以下公式定义:
三角数: Tn = n(n+1)/2     1, 3, 6, 10, 15, ...
五角数:  Pn = n(3n+1)/2    1, 5, 12, 22, 35, ...
六角数:  Hn = n(2n-1)      1, 6, 15, 28, 45, ...
可以证实 T285 = P165 = H143 = 40755
找出这之后的下一个既是五角数又是六角数的三角数。
'''
#三角数
def triangle(n):
    i = 286
    t = []
    while i < n:
        t.append(i*(i+1)//2)
        i += 1
    return t
        

#五角数
def pentagonal(n):
    i = 166
    p = []
    while i < n:
        p.append(i*(3*i-1)//2)
        i += 1
    return p
        

#六角数
def hexagonal(n):
    i = 144
    h = []
    while i < n:
        h.append(i*(2*i-1))
        i += 1
    return h
s = time()
n = 1000
while n:
    result = list(set(triangle(n)) & set(pentagonal(n)) & set(hexagonal(n)))
    if result != []:
        print(result[0])
        break
    else:
        n *= 10
e = time()
print("用时%.4f秒" % (e-s))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-24 22:41:02 | 显示全部楼层
  1. import time as t
  2. import numpy as np

  3. start = t.perf_counter()

  4. n_t = 285
  5. while True:
  6.     n_t += 1
  7.     Tn = n_t * (n_t + 1) / 2
  8.     n_p = int(np.sqrt(2 / 3 * Tn)) + 1
  9.     n_h = int(np.sqrt(1 / 2 * Tn)) + 1
  10.     Pn = n_p * (3 * n_p - 1) / 2
  11.     Hn = n_h * (2 * n_h - 1)
  12.     if Tn == Pn == Hn:
  13.         break

  14. print(Tn)
  15. print("It costs %f s" % (t.perf_counter() - start))
复制代码



1533776805.0
It costs 0.119939 s
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 14:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表