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);
11 ビュー (過去 30 日間)
古いコメントを表示
remaining question: 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.)
function n = palin_product(dig,lim)
%Two for loops are created to loop through the multiplication
for dig = 100:999
for lim = 100:999
number = dig*lim;
if ispalindrome(number)
n = [n number];
else
n=0;
end
end
end
n = max(vec)
5 件のコメント
John D'Errico
2017 年 5 月 27 日
編集済み: John D'Errico
2017 年 5 月 27 日
I won't do your homework. But I did offer some suggestions. And you have still not asked a question that is about MATLAB, beyond a doit4me.
回答 (1 件)
Jan
2017 年 5 月 27 日
@Wasi: The question is completely unclear.
1. In the title you write "n = palin_product(dig,lim)", but what are n, dig and lim?
2. The code does not create any 2 digit number, therefore I do not see the relation to the problem mentioned in the title.
3. dig and lim are at first provided as input, but then overwritten in the loops.
4. The function ispalindrome() is unknown.
5. n is overwritten repeatedly by 0 inside the loops and
6. finally n is ingnored completely, but overwritten by n = max(vec), where vec is undefined.
7. n = [n number] must fail, when n is undefined at the beginning.
8. You forgot to ask a question. It is not clear which kind of assistance you want.
The comments do not shed ligt on the problem also: I cannot imagine, what "How Palindromes come?" means.
It is not the purpose of this forum that we solve Project Euler problems for you. It is much more useful, if you try it by your own and if you have any at least partially working code, ask a specific question about a specific problem. Currently we see a partial description of the original problem and some code which do not look related (except that a multiplication appears and the term "ispalindrome") or working.
Sit down. Get a cup of coffee. Think twice. The Answers forum is populated by Matlab users, how are willing to assist you to solve Matlab problems. We cannot read your mind or guess the purpose of your code. We are not a programming service and you will not get a lesson in general programming techniques here. The forum cannot replace a programming course.
I have the impression that you are completely lost with this Project Euler problem. Therefore I suggest you to restart from scratch. Find out what is asked exactly in teh question. Then pick one detail after the other and solve it. Do not procede to the next step until one part is solved. E.g. start with creating 2 numbers with 2 digits. If this does not work reliably, multiplicating them increases the confusion level only.
1 件のコメント
Sam
2018 年 4 月 16 日
Here's the full question: 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.)
Here's an explanation breaking it down step by step. Comments are included to give more detailed explanations.
function [n] = palin_product(dig,lim)
%What does the function do?
%a palindromic number = reads same both ways (eg 1001)
%the largest palindrome made from the product of two 2-digit numbers is
%9009 = 91 x 99.
%This function has to return the largest palindrome smaller than 'lim' that
%is the product of two 'dig' digit numbers
%(eg if dig = 2, then the highest possible 2 dig numbers are 99 x 99)
%If no such number exists, the function returns 0
UL = (10^dig) - 1
LL = 10^(dig - 1)
%UL is the largest 'dig' digit number (upper limit)
%LL is the lower limit of a 'dig' digit number: = 10^(dig-1)
%example: if 'dig' = 2 --> the numbers to check are 10-99
%ie 10^(2-1) = 10 to (10^2)-1 = 99
c = UL: -1: LL;
%this is a list of all the 'dig' digit numbers to check
%ie for 2 digit numbers would be 10-99
%Future hint:
%to verify a palindrome:
%will need to use c = num2str(__)
%and see if it is equal to itself read in reverse by using c(end:-1:1)
n = 0;
list = [];
plist = [];
%set up number of iterations and blank lists to fill in
%
for x = 1:numel(c)
%to get all possible products: we will have to multiply each individual number in the vector by every number in the vector (the whole vector)
n = n+1;
yy = c(n) * c;
%multiply (*) (not .*) the n'th element of row vector c by all other values
%then put this into a new row vector
list = [list yy];
%this should form a row vector of ALL possible 'dig' digit products
end
for y = 1:length(list)
forward = num2str(list(y));
backward = forward(end:-1:1);
if forward == backward;
plist = [plist list(y)];
%this should form a list of only the palindromes...a 'plist'
end
end
under_lim = plist < lim
%this gives a logical (0 or 1) row vector of all the plist indices (palindrome
%list) that are under the limit
final_list = plist(under_lim)
%the final list to choose the largest palindrome from
n = max(final_list)
if isempty(n)
n = 0
%accounts for if there is NOT a palindrome that is less than the limit inputted
end
参考
カテゴリ
Help Center および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!