qibbxxt 发表于 2010-9-27 14:47:49

【讨论】ProjEuler[52]:Find the smallest positive integer, x, such that 2x, ...

原问题来自于:http://forum.simwe.com/viewthread.php?tid=908892&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D508
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
找出最小的一个数x,要求它的2倍,3位直到6倍各数字都与原数的数位组成是相同的,只能是顺序不同。

我用matlab写程序,代码如下,但是效率有点低,希望大家一起讨论

function y=ex092703
tic
a=1:2*10^5;
b=a./10.^floor(log10(a));
a=a(b<1.7);
y=a(cell2mat(cellfun(@myfun_1,num2cell(a),'UniformOutput',false)));
toc
function y=myfun_1(x)
y=[];
x0=repmat(x,6,1).*(1:6)';
x1=floor(log10(x))+1;
x2=sort(mod(floor(bsxfun(@rdivide,repmat(x0,1,x1),10.^(x1-1:-1:0))),10),2);
y=size(unique(x2,'rows'),1)==1;

Elapsed time is 13.345225 seconds.
ans =
      142857


另外:这个问题如果从分数的角度看,比较简单,1/7,2/7,……,但是在此仅仅是练习编程思路。

shamohu 发表于 2010-9-27 17:30:56

6位:125874 (2*125874 = 251748)

不知下面的算不算?
7位:1245789 (2*1245789 = 2491578)
8位:12356784 (2*12356784 = 24713568)
9位:123456789 (2*123456789 = 246913578)

qibbxxt 发表于 2010-9-27 17:36:15

2# shamohu
是2倍,3倍,4倍,5倍和6倍的数字全都一样

lengyunfeng 发表于 2010-9-27 19:27:23

不知qi兄程序里的第5、6两行如何理解。我有一些想法,能不能实现以及实现所需花费的时间就有待进一步的工作了。对于N位整数,有以下几点:
1)x与6x的组成数字要相同,那么对于x的第一位,必然要求它只能等于1,因为一旦超过2就会导致数位的增加。这一条就能把N位数范围缩小至1/9;
2)x与5x的组成数字要相同,那么x的所有位里至少要有一个0或者5;
3)x与3x的组成数字要相同,那么组成x的所有位之和要能被3整除。这一条应该也能把数字范围缩小很多。

qibbxxt 发表于 2010-9-27 20:19:17

4# lengyunfeng
leng老弟,5与6行的考虑是第1位的数字是1,第二位的数字小于7,因为1.7*6超过1位了,按照老弟的2和3的建议,我把代码改进了一下,果然效率大增,谢谢老弟

function y=ex092704
tic
a=1:2*10^5;
b=a./10.^floor(log10(a));
a=a(b<1.7);
y=a(cell2mat(cellfun(@myfun_1,num2cell(a),'UniformOutput',false)));
toc
function y=myfun_1(x)
y=false;
x1=floor(log10(x))+1;
y1=mod(floor(x./10.^(x1-1:-1:0)),10);
if ~mod(sum(y1),3) && ismember(5,y1)
    x0=repmat(x,6,1).*(1:6)';
    x2=sort(mod(floor(bsxfun(@rdivide,repmat(x0,1,x1),10.^(x1-1:-1:0))),10),2);
    y=size(unique(x2,'rows'),1)==1;
end

Elapsed time is 4.402067 seconds.
ans =
      142857

shamohu 发表于 2010-9-28 09:06:47

用1stOpt找到一个7位数的:1429857
2倍: 2859714
3倍: 4289571
4倍: 5719428
5倍: 7149285
6倍: 8579142

lin2009 发表于 2010-9-28 09:49:57

8位以上的数为
14299857
142999857
1429999857
14299999857
.....

shamohu 发表于 2010-9-28 10:12:03

本以为不能有重复的数字出现...
页: [1]
查看完整版本: 【讨论】ProjEuler[52]:Find the smallest positive integer, x, such that 2x, ...