找回密码
 注册
Simdroid-非首页
查看: 82|回复: 0

【讨论】ProjEuler[49]:质数中的等差数列

[复制链接]
发表于 2010-9-27 15:02:31 | 显示全部楼层 |阅读模式 来自 河北廊坊
本帖最后由 qibbxxt 于 2010-9-27 15:18 编辑

原问题来自:http://forum.simwe.com/viewthread.php?tid=911960&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D508
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?

1487, 4817, 8147这三个数有如下特征:都是素数;由相同的数位组成(1,4,7,8);三者构成等差数列。
已知在四位数中仍然有一组与此类似的等差数列,试找出来这三个数,并且按照这三个数字大小递增的顺序写出这十二个位数来

我用matlab 来实现,本来已经答复过,但是还是觉得放在这里大家一起讨论,会好一些
  1. function dd=ex092603
  2. tic
  3. b=3:-1:0;
  4. a=primes(10^length(b)-1);
  5. a=a(a>10^(length(b)-1));
  6. [e,f,g]=unique(sort(mod(floor(bsxfun(@rdivide,repmat(a',1,length(b)),10.^b)),10),2),'rows');
  7. pc=accumarray(g,a',[],@(x){x});
  8. dd=cell2mat(cellfun(@isContinuePrimes,pc,'UniformOutput',false));
  9. toc
  10. function y=isContinuePrimes(x)
  11. y=[];
  12. if length(x)>2
  13.     y0=nchoosek(x,3);
  14.     y1=(max(y0,[],2)+min(y0,[],2))/2;
  15.     y2=y1==mean(y0,2);
  16.     if any(y2)
  17.         y=y0(y2,:);
  18.     end
  19. end
复制代码

  1. Elapsed time is 0.071698 seconds.
  2. ans =
  3.         1487        4817        8147
  4.         6299        9629        2969
复制代码

评分

1

查看全部评分

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

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-6 17:21 , Processed in 0.037602 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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