In this question, would dig be 2 inputs, such as 91 and 99? And lim can be any number the user calls?

3 次查看(过去 30 天)
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Write a function that is called this way:
>> n = palin_product(dig,lim);
The function returns the largest palindrome smaller than lim that is the product of two dig digit numbers. If no such number exists, the function returns 0. (Inspired by Project Euler.)

采纳的回答

Stephen23
Stephen23 2017-9-7
编辑:Stephen23 2017-9-7
I know that this is homework, but someone needed to demonstrate how this can be done quite simply, without using slow and unnecessary third-party functions: I believe that champions2015 and future readers deserve to be shown that MATLAB code can be neat, efficient, and straightforward. Note that this code actually does exactly what the question asks for!
function n = palin_product(dig,lim)
n = 0;
V = 10.^dig-1:-1:10.^(dig-1);
for k1 = V
for k2 = V
p = k1*k2;
if p>n && p<lim
s = sprintf('%d',p);
if all(s==s(end:-1:1))
n = p;
end
end
end
end
end
And tested:
>> palin_product(2,10000)
ans = 9009
>> palin_product(2,9009)
ans = 8448
>> palin_product(2,8448)
ans = 8118
>> palin_product(2,8118)
ans = 8008
>> palin_product(2,8008)
ans = 7227
  6 个评论

请先登录,再进行评论。

更多回答(3 个)

the cyclist
the cyclist 2017-9-6
I would interpret it as follows.
You are trying to find p1 * p2 = n.
  • lim is any number the user chooses (just as you guessed). The output, n, must be smaller than lim.
  • dig is the number of digits that p1 and p2 each have. For example, if p1=91, and p2=99, then dig=2, because they are 2-digit numbers.

Image Analyst
Image Analyst 2017-9-6
Hint:
n = 9009
s = sprintf('%d', n)
flipped_s = fliplr(s)

John BG
John BG 2017-9-7
编辑:Jan 2018-2-25
hi champions2015
the following is a slight variation of what's been asked, this function
  • returns the nearest palindrome to the input
  • also calculates the 2 numbers of digit length dig that multiplied are lim
  • if the input is already a palindrome, it decomposes as requested lim, not seeking any smaller palindrome.
clear all;clc
lim2=8448
dig=2
function [lim r1 r2]=palin_product(dig,lim2)
r1=0;r2=0;
ispal=0;
decom=1;
lim=lim2 % keep start value in lim2, modify lim
while decom
ispal=0;
while ~ispal % find next symmetric integer below input lim2
L1=num2str(lim);
n1=1;n2=length(L1);
while L1(n1)==L1(n2) && n1<=floor(length(L1)/2)
n1=n1+1;n2=n2-1;
end
if L1(n1)==L1(n2) ispal=1; end
if ~ispal lim=lim-1; end
end
s1=10^(dig-1);s2=str2num(repmat('9',1,dig)); % find whether found lim can be decomposed
S=[s1:1:s2];
L=combinator(numel(S),dig,'p','r');
p=1;
s1=S(L(p,1));s2=S(L(p,2));
while ~(s1*s2==lim) && p<length(L)
s1=S(L(p,1));s2=S(L(p,2));
p=p+1;
end
if p<=length(L) && s1*s2==lim
r1=s1;r2=s2
end
if r1==0 && r2==0
lim=lim-1;
end
if r1*r2>0
decom=0;
end
end
end % function
.
the most recent version of the function combinator and its support functions are all packed and freely available from
combinator_update.zip and palin_product.m are both attached to this answer.
.
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
[EDITED: Copyrighted code removed]
  2 个评论
champions2015
champions2015 2017-9-7
Hi John! Thanks for you answer! I'm not entirely sure if I understand how your code works, and it doesn't seem to work either when I run the function... Why are there 2 outputs? Wouldn't the output be simply somthing like 9009?
John BG
John BG 2017-9-7
编辑:John BG 2017-9-7
Hi Champions2015
When the input is already is such palindrome that can be decomposed as requested, why would you want to seek a smaller palindrome that can be decomposed into 2 figures of digit length dig?
My function also calculates the requested digit dig length decomposition.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by