鱼C论坛

 找回密码
 立即注册
查看: 6346|回复: 39

题目12:第一个拥有超过500个约数的三角形数是多少?

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

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

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

x
本帖最后由 欧拉计划 于 2015-4-21 16:17 编辑
Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that  28  is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

题目:

三角形数序列是由对自然数的连加构造而成的。所以第七个三角形数是 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 那么三角形数序列中的前十个是:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

下面我们列出前七个三角形数的约数:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
可以看出 28 是第一个拥有超过 5 个约数的三角形数。

那么第一个拥有超过 500 个约数的三角形数是多少?

评分

参与人数 1鱼币 +1 收起 理由
cwhsmile + 1 (三角形数76576500, 第12375)用时11s

查看全部评分

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

使用道具 举报

发表于 2015-8-2 20:48:28 | 显示全部楼层
本帖最后由 翅膀团 于 2015-11-16 14:13 编辑
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.     int a=0,i=0,j,n=1;
  5.     while(i != 500)
  6.     {
  7.         i=0;
  8.         a += n;
  9.         for(j=1;j<=a;j++)
  10.         {
  11.         if(!(a % j))
  12.         {
  13.         i++;
  14.         }
  15.         }
  16.         n++;
  17.     }
  18.     printf("第一个拥有超过 500 个约数的三角形数是%d\n",a);
  19. }
复制代码


如果有错误希望指出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

匿名鱼油  发表于 2015-12-2 15:41:29
严重超时了,电脑跑不出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具

发表于 2016-3-2 22:21:33 | 显示全部楼层
游客 112.80.78.x 发表于 2015-12-2 15:41
严重超时了,电脑跑不出来

那样的确效率太低,这个差不多是效率最高的 求约数个数的方法
int main()
{
    int i=1,j=2,total=1,t;
    while(total<=500)
    {
        i+=j;
        j++;
        total=1;
        for(t=1;t*t<=i;t++)
        {
            if(i%t==0)
            {
                if(i==t*t)
                    total++;
                else
                    total+=2;
            }
        }
    }
    printf("%d\n",i);
   
}

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

使用道具 举报

发表于 2016-6-13 18:18:36 | 显示全部楼层
  1. def g(x):
  2.     i=1
  3.     n=0
  4.     while i**2<=x:
  5.         if x%i==0:n+=2
  6.         i+=1
  7.     if (i-1)**2==x:n-=1
  8.         
  9.     return n

  10. i=0
  11. x=0
  12. while g(x)<=500:
  13.     i+=1
  14.     x+=i
  15. print (x)
  16.    
复制代码

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

使用道具 举报

发表于 2016-8-24 12:40:02 | 显示全部楼层
  1. import math
  2. count = 0
  3. total = 0
  4. list1 = []

  5. while True:
  6.       count += 1
  7.       total += count
  8.       
  9.       for each in range(1,int(math.sqrt(total))+1):
  10.             if total % each == 0:
  11.                   list1.append(each)
  12.                   
  13.       if len(list1) > 250:    #列表里的约数的个数是总约数的一半
  14.             print(len(list1))
  15.             print(total)
  16.             break
  17.       list1 = []              
复制代码

10多秒出结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-26 23:30:13 | 显示全部楼层
  1. from math import *
  2. def fun(x):
  3.     return (1+x)*x//2
  4. num = 1


  5. def fun2(x):
  6.     n = 0
  7.     for i in range(1,floor(sqrt(x))+1):
  8.         if x%i == 0:
  9.             n += 1
  10.     if floor(sqrt(x)) == sqrt(x):
  11.         return 2*n-1
  12.     else:
  13.         return 2*n

  14. k = fun2(fun(num))

  15. while k<=500:
  16.     num += 1
  17.     k = fun2(fun(num))

  18. print(fun(num))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-17 17:17:49 | 显示全部楼层
  1. length:576 76576500
  2. [Finished in 25.7s]
复制代码

  1. # 第一个拥有超过500个约数的三角形数是多少?

  2. def yueshu(num):
  3.         yue = []
  4.         for i in range(1, int(num ** 0.5 + 1)):
  5.                 if num % i == 0:
  6.                         yue.append (i)
  7.                         yue.append (num // i)
  8.         yue = set (yue)
  9.         length = len (yue)
  10.         return [yue,length]

  11. snlist = []
  12. suml = 0
  13. for i in range(50000):
  14.         suml += i
  15.         snlist.append (suml)

  16. for each in snlist:
  17.         output = yueshu(each)
  18.         lensum = output[1]
  19.         if lensum >500:
  20.                 yuesum = list(output[0])
  21.                 fin = each
  22.                 break
  23.         else:
  24.                 pass

  25. print (yuesum, 'length:%d' % lensum, fin)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-11 16:36:43 | 显示全部楼层
  1. def euler(x):
  2.     n = 1
  3.     tri_number = 0
  4.     while True:
  5.         list_a = []
  6.         tri_number += n
  7.         for i in range(1,int(tri_number**0.5)+1):
  8.             if not tri_number%i:
  9.                 list_a.append(i)
  10.             if len(list_a) > x/2:
  11.                 return tri_number
  12.         n += 1

  13. if __name__ == '__main__':
  14.     print(euler(500))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-23 15:51:14 | 显示全部楼层
  1. import math
  2. import time
  3. t = time.clock()
  4. def euler12(count=500):
  5.     """
  6.     三角形数序列是由对自然数的连加构造而成的,所以第七个三角形数是1+2+3+4+5+6+7=28
  7.     28的约数有 1,2,4,7,14,28。第一个拥有超过5个约数的三角形数是28
  8.     第一个拥有超过500个约数的三角形数是多少
  9.     """
  10.     n=0
  11.     trinum=0
  12.     while True:
  13.         n += 1
  14.         trinum += n
  15.         temp = [num for num in range(1,int(math.sqrt(trinum))+1) if trinum%num==0]
  16.         if len(temp)>count/2: break
  17.     numlist = [int(trinum/num) for num in temp]
  18.     numlist.extend(temp)
  19.     return trinum,len(numlist),sorted(numlist)

  20. f = euler12(500)
  21. print('{}\n\nresult:{}    count:{}    time:{}'.format(f[2],f[0],f[1],time.clock()-t) )
复制代码


result:76576500    count:576    time:5.11109289657992
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-9 17:01:59 | 显示全部楼层
  1. # encoding:utf-8
  2. from math import sqrt
  3. from time import time

  4. def getTriNumber(N=10000):
  5.     return int((1 + N) * N / 2)
  6. # 拆分素数因子
  7. def findPrimeFactors(n, l_pr):
  8.     if isPrime(n):
  9.         return [n]
  10.     sqr_n = int(sqrt(n)) + 1
  11.     result = list()
  12.     for pr in l_pr:
  13.         if n % pr == 0:
  14.             result.append(pr)
  15.             result.extend(findPrimeFactors(int(n / pr), l_pr))
  16.             break
  17.         if pr > sqr_n:
  18.             break
  19.     return result
  20. #找小于N的所有质数 学习了其他同学的代码
  21. def getPrime(n):
  22.         primes = [True]*n
  23.         primes[0],primes[1]=False,False
  24.         for (i, prime) in enumerate(primes):
  25.             if prime:
  26.                 for j in range(i*i,n,i):
  27.                     primes[j] = False
  28.         return [k for (k,trueprime) in enumerate(primes) if trueprime]   
  29. def isPrime(n):
  30.     if not isinstance(n, int):
  31.         print('输入有误!')
  32.         return
  33.     if n < 4:
  34.         return True;
  35.     if n % 2 == 0 or n % 3 == 0:
  36.         return False
  37.     for i in range(3, int(sqrt(n)) + 1, 2):
  38.         if not n % i:
  39.             return False
  40.     return True

  41. start = time()            
  42. l_pr = getPrime(10000)
  43. num = 1
  44. while True:
  45.     num_fac = 1
  46.     N = getTriNumber(num)
  47.     fac_list = findPrimeFactors(N,l_pr)
  48.     fac_set = set(fac_list)
  49.     for each in fac_set:
  50.         num_fac *= (fac_list.count(each) + 1)
  51.     if num_fac >= 500:
  52.         print('最小的超过500个约数的三角数是: %d, 约数个数为 %d。' % (N,num_fac))
  53.         break
  54.     num +=1
  55. print('cost %.3f sec' % (time() - start))
复制代码


最小的超过500个约数的三角数是: 76576500, 约数个数为 576。
cost 0.444 sec

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

使用道具 举报

发表于 2017-1-14 01:50:51 | 显示全部楼层
本帖最后由 渡风 于 2017-1-22 19:35 编辑

此代码使用matlab编程
Problem12所用时间为9.8363秒
Problem12的答案为76576500
  1. %题目12:第一个拥有超过500个约数的三角形数是多少?
  2. function Output=Problem12(Input)
  3. if nargin==0
  4.   Input=500;
  5. end
  6. tic
  7. N=1;
  8. Num=0;
  9. Start=sum(1:N);
  10. while (Num<=Input)   
  11.      N=N+1;
  12.      Start=sum(1:N);
  13.      Num=Factor_Num(Start);
  14. end
  15. Output=Start;   
  16. toc
  17. disp('此代码使用matlab编程')
  18. disp(['Problem12所用时间为',num2str(toc),'秒'])
  19. disp(['Problem12的答案为',num2str(Output)])
  20. end
  21. %end
  22. %% 子函数
  23. %输入一个数得到一个数的所有因子的数量。
  24. function Output=Factor_Num(Input)
  25. if nargin==0
  26.     Input=10000;
  27. end
  28. if Input==1
  29.     Output=1;
  30. else
  31.     Rank=[];
  32.     Node=floor(sqrt(Input));
  33.     if Node*Node==Input
  34.         Rank=Node;
  35.         Cut=Node-1;
  36.     else
  37.         Cut=Node;
  38.     end
  39.     for ii=Cut:-1:2
  40.         if mod(Input,ii)==0
  41.             Temp=[ii,Rank,Input/ii];
  42.             Rank=Temp;
  43.         end
  44.     end
  45.     Output=length(Rank)+1;
  46. end
  47. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-6 23:05:06 | 显示全部楼层
  1. import time

  2. def triangular_1():
  3.     '寻找第一个拥有超过500个约数的三角形数,不推荐'
  4.     triangle = 1
  5.     i = 1
  6.     count = 0
  7.     while count <= 500:
  8.         count = 0
  9.         triangle = int((i + 1) * i / 2)
  10.         i += 1
  11.         if triangle % 2 == 0:
  12.             for j in range(1, int(triangle / 2)):
  13.                 if triangle % j == 0:
  14.                     count += 1
  15.         else:
  16.             for j in range(1, int(triangle / 3 + 1), 2):
  17.                 if triangle % j == 0:
  18.                     count += 1
  19.         print('%d-->%d' %(triangle, count))
  20.     return triangle

  21. def find_primes(number):
  22.     '筛法找出number个质数'
  23.     list_primes = [True] * (number * 8)
  24.     list_primes[0] = False
  25.     list_primes[1] = False
  26.     list_result = []
  27.     while len(list_result) < number:
  28.         for i in range(2, len(list_primes)):
  29.             if list_primes[i]:
  30.                 for j in range(i ** 2, len(list_primes), i):
  31.                     list_primes[j] = False
  32.         list_result.clear()
  33.         for k in range(2, len(list_primes)):
  34.             if list_primes[k]:
  35.                 list_result.append(k)
  36.         list_primes.extend([True] * number)
  37.     return list_result[:number]

  38. def triangular_2():
  39.     '使用约数个数定理计算'
  40.     list_primes = find_primes(498)
  41.     list_exp = []
  42.     triangle = 1
  43.     result = 1
  44.     i = 2
  45.     while result <= 500:
  46.     #while i < 7:
  47.         triangle = int((i + 1) * i / 2)
  48.         i += 1
  49.         list_exp.clear()
  50.         result = 1
  51.         for each in list_primes:
  52.             if each > triangle:
  53.                 break
  54.             count = 0
  55.             temp = triangle
  56.             while temp % each == 0:
  57.                 temp /= each
  58.                 count += 1
  59.             if count != 0:
  60.                 list_exp.append(count)
  61.         for each in list_exp:
  62.             result *= (each + 1)
  63.     return [triangle, result]

  64. start = time.clock()
  65. print(triangular_2())
  66. end = time.clock()
  67. print('程序执行了%fs。' %(end - start))
复制代码

执行结果:
[76576500, 576]
程序执行了1.046565s。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-14 18:59:03 | 显示全部楼层
  1. num =a=count=0
  2. while count<=500:
  3.     a+=1
  4.     num+=a
  5.     count=0
  6.     for i in range(1,int(num**0.5)+1):
  7.         if not num%i:
  8.             if num==i**2:
  9.                 count+=1
  10.             else:count+=2
  11. print(num)
复制代码

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

使用道具 举报

发表于 2017-4-2 09:31:52 | 显示全部楼层
本帖最后由 JonTargaryen 于 2017-4-2 10:13 编辑

约数个数分解定理
http://baike.baidu.com/item/%E7% ... 90%86?sefr=enterbtn
  1. #include <stdio.h>

  2. int divi_num(int result);

  3. int main(void)
  4. {
  5.     int result = 0;
  6.     int num = 0;
  7.     int count = 0;
  8.     int i, temp;

  9.     while(count <= 500)
  10.     {
  11.         num++;
  12.         result += num;

  13.         count = divi_num(result);
  14.     }

  15.     printf("%d\n", result);

  16.     return 0;
  17. }

  18. int divi_num(int result)
  19. {
  20.     int count = 1;
  21.     int factor_count;
  22.     int i;

  23.     for(i = 2; i <= result; i++)
  24.     {
  25.         factor_count = 0;

  26.         while(!(result % i))
  27.         {
  28.             factor_count++;

  29.             result /= i;
  30.         }

  31.         count *= factor_count + 1;
  32.     }

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

使用道具 举报

发表于 2017-5-1 01:43:38 | 显示全部楼层
  1. #include<stdio.h>

  2. int count_divisor(int num);
  3. int main()
  4. {
  5.         int t_num = 1;
  6.         for (int i = 2;count_divisor(t_num)<=500;i++)
  7.         {
  8.                 t_num += i;
  9.         }
  10.         printf("%d\n", t_num);
  11.         return 0;
  12. }

  13. int count_divisor(int x)
  14. {
  15.         int count = 0;
  16.         int i;
  17.         for (i = 1;i*i < x;i++)
  18.         {
  19.                 if (x%i == 0)
  20.                 {
  21.                         count++;
  22.                 }
  23.         }
  24.         count *= 2;
  25.         if (i*i == x) count++;
  26.         return count;
  27. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-12 16:51:26 | 显示全部楼层
  1. def check(n,t):
  2.     count = 2
  3.     for i in range(2,n//2 + 1):
  4.         if not (n % i):
  5.             count += 1
  6.             if count == t:
  7.                 break
  8.     if count != t:
  9.         return False
  10.     return True


  11. tri = 0
  12. for i in range(1,1000):
  13.     tri += i
  14.     if check(tri,500):
  15.         print(tri)
  16.         break
  17.         
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 15:29:01 | 显示全部楼层
来个冷门的,用AArdio编写
  1. import console;
  2. import time.timer

  3. console.setTitle("test");

  4. var ys = function(n) {
  5.         if (n==1){
  6.                 return 1;
  7.         }
  8.         var c = 2;
  9.         for(i=2;n**0.5;1){
  10.                 if(n%i==0){
  11.                         if(i*i==n){
  12.                                 c += 1;
  13.                         }
  14.                         else{
  15.                                 c += 2;
  16.                         }
  17.                        
  18.                 }
  19.        
  20.         }
  21.         return c
  22. }

  23. time.timer.start();
  24. sum = 0;
  25. s = 1;
  26. while(ys(sum) <= 500){
  27.         sum += s
  28.         s++

  29. }

  30. console.print(ys(sum), sum);

  31. console.print(time.timer.endTick())

  32. console.pause();
复制代码


576     76576500
1690.6272414923
请按任意键继续 ...

运行速度差不多比python高4~5倍
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-4-3 19:34:12 | 显示全部楼层
  1. import time


  2. start = time.time()
  3. n = 0
  4. num = 0
  5. while True:
  6.     factcount = 2
  7.     n += 1
  8.     num += n
  9.     for i in range(2, int(num ** 0.5) + 1):
  10.         if num % i == 0:
  11.             if num != i ** 2:
  12.                 factcount += 2
  13.             else:
  14.                 factcount += 1
  15.     if factcount > 500:
  16.         break
  17. print n, num
  18. print time.time() - start
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-27 17:14:19 | 显示全部楼层
  1. def pri(x):
  2.     t={}
  3.     while x!=1:
  4.         z=x
  5.         if x%2==0:
  6.             x=x//2
  7.             if 2 in t:
  8.                 t[2]+=1
  9.             else:
  10.                 t[2]=1
  11.             continue
  12.         elif x%3==0:
  13.             x=x//3
  14.             if 3 in t:
  15.                 t[3]+=1
  16.             else:
  17.                 t[3]=1
  18.             continue
  19.         else:
  20.             for i in range(3,int(x**0.5)+1,2):
  21.                 if x%i==0:
  22.                     if i in t:
  23.                         t[i]+=1
  24.                     else:
  25.                         t[i]=1
  26.                     x=x//i
  27.                     break
  28.         if z==x:
  29.             x=1
  30.             if z in t:
  31.                 t[z]+=1
  32.             else:
  33.                 t[z]=1
  34.     res=1
  35.     for each in t:
  36.         res*=(t[each]+1)
  37.     return res
  38. x=200
  39. while pri(x*(x+1)//2)<=500:
  40.     x+=1
  41. print(x*(x+1)//2)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 03:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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