qibbxxt 发表于 2010-9-27 10:36:48

【讨论】找出最大的6位回文数

原帖来自:http://forum.simwe.com/viewthread.php?tid=858417&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D508
是用mathematica解的,我用matlab来求解,发在这里,大家一起讨论

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.

Find the largest palindrome made from the product of two 3-digit numbers.
找出最大的回文数字,它们是由两个3位数字的乘积。
代码如下:
clear;clc;close all
tic;
=meshgrid(100:999);
c=a.*b;
d=c(c>=10^5);
e=sum(bsxfun(@times,mod(floor(bsxfun(@rdivide,repmat(d,1,6),10.^(5:-1:0))),10),10.^(0:5)),2);
f=max(d(d==e));
toc

f =
      906609
Elapsed time is 0.239026 seconds.

shamohu 发表于 2010-9-27 16:16:18

转化为优化问题,可用1stOpt求解:
Constant p1=9, n=3;
IntParameterp(2:n)=,x(2)=;
ConstStr f= Sum(i=1:n)(p*10^(2*n-i))+ Sum(i=1:n)(p*10^(n-i));
MaxFunction f;
            f=x1*x2;
目标函数值(最大): 906609
p2: 0
p3: 6
x1: 913
x2: 993

约束函数:
   1: 9*10^5+p2*10^4+p3*10^3+p3*10^2+p2*10^1+9*10^0-(x1*x2) = 0

将代码“n=3”改为“n=4”,可得8位数的回文:
标函数值(最大): 94222249
p2: 4
p3: 2
p4: 2
x1: 9647
x2: 9767

约束函数:
   1: 9*10^7+p2*10^6+p3*10^5+p4*10^4+p4*10^3+p3*10^2+p2*10^1+9*10^0-(x1*x2) = 0

同样:
10位:9468338649(x1: 96747,x2: 97867)
12位:986946649689 (x1: 987987,x2: 998947)
页: [1]
查看完整版本: 【讨论】找出最大的6位回文数