与 完整数 相关的是 亲和数。
用计算机找出10^6(即100万以内的亲和数对),看看时间花多少?
-
- clear all;
- clc;
- start_time = tic;
- fid1 = fopen('wanquanshu.m', 'w'); % 完全数
- fid2 = fopen('qinheshu.m', 'w'); % 亲和数
- for n = 10:10^6
- if isprime(n)
- continue;
- end
- m = sum(find(fix(n ./ (1 : n)) == n ./ (1 : n))) - n; % 约数之和, 低效的算法!
- if m == n
- fprintf(fid1, ' %d\n', n);
- continue;
- end
- nn = sum(find(fix(m ./ (1 : m)) == m ./ (1 : m))) - m;
- if nn == n
- fprintf(fid2, ' %d, %d\n', n, m);
- end
- end
- time_comsuming = toc;
- fprintf(fid1, '\n Elapsed time is %f seconds.\n', time_comsuming);
- fprintf(fid1, '\n Elapsed time is %f seconds.\n', time_comsuming);
- fclose(fid1);
- fclose(fid2);
- !shutdown - s
复制代码 在Celeron(R) CPU 2.66G 760MB内存上,耗时超过24小时!(有点无聊,呵呵)
1、大家看看有没有高效的算法?应该可以利用factor(n)及约数的通式a^n1*b^n2*c^n3...以及accumarray及bsxfun等函数?
结果如下,
2、怎样把重复的亲和数对去掉?
- 220, 284
- 284, 220
- 1184, 1210
- 1210, 1184
- 2620, 2924
- 2924, 2620
- 5020, 5564
- 5564, 5020
- 6232, 6368
- 6368, 6232
- 10744, 10856
- 10856, 10744
- 12285, 14595
- 14595, 12285
- 17296, 18416
- 18416, 17296
- 63020, 76084
- 66928, 66992
- 66992, 66928
- 67095, 71145
- 69615, 87633
- 71145, 67095
- 76084, 63020
- 79750, 88730
- 87633, 69615
- 88730, 79750
- 100485, 124155
- 122265, 139815
- 122368, 123152
- 123152, 122368
- 124155, 100485
- 139815, 122265
- 141664, 153176
- 142310, 168730
- 153176, 141664
- 168730, 142310
- 171856, 176336
- 176272, 180848
- 176336, 171856
- 180848, 176272
- 185368, 203432
- 196724, 202444
- 202444, 196724
- 203432, 185368
- 280540, 365084
- 308620, 389924
- 319550, 430402
- 356408, 399592
- 365084, 280540
- 389924, 308620
- 399592, 356408
- 430402, 319550
- 437456, 455344
- 455344, 437456
- 469028, 486178
- 486178, 469028
- 503056, 514736
- 514736, 503056
- 522405, 525915
- 525915, 522405
- 600392, 669688
- 609928, 686072
- 624184, 691256
- 635624, 712216
- 643336, 652664
- 652664, 643336
- 667964, 783556
- 669688, 600392
- 686072, 609928
- 691256, 624184
- 712216, 635624
- 726104, 796696
- 783556, 667964
- 796696, 726104
- 802725, 863835
- 863835, 802725
- 879712, 901424
- 898216, 980984
- 901424, 879712
- 947835, 1125765
- 980984, 898216
- 998104, 1043096
复制代码
|