Okay, it took two days but the grader program is saying my answer is correct, so I've got this one beat.
Need to find Palindromic number with multipliers that are both n digits long.
1 次查看(过去 30 天)
显示 更早的评论
Okay, I seek palindromic number with multipliers that are both "n" digits long. Once I find a palindromic candidate, I must determine if it has multiples of "n" digits. For 100, I first locate 99, then I search for the multiples. Once it hits 9 the multiple is ll, and given that the "n" digit limit is 1 for this case, 9 and 11 won't work, because they aren't both "n" digits long. Once I start hitting combinations like 9 and 11, using a smaller integer than 9 as the multiple makes no sense, because they will always be larger than 11, which means they will never be n=1 long. At this point I need to start looking for a smaller palindrome, so I start the function over again using "q-1", which is one less than the previous palindrome candidate. I don't know how to stop the program, which I need to do at the bottom. How does one simply stop Matlab programatically?
function n = palin_product( dig,lim )
if isnumeric(dig)== false
n = 0;
return;
end
if isnumeric(lim)==false
n = 0;
return;
end
c = floor(dig);
multiplier1=10^c;
multiplier1 = multiplier1-1;
L=num2str(lim-1);
n = lim-1;
len = length(L);
if mod(L,2)==0
nucount=L/2;
else
nucount =floor(L/2);
end
q = lim-1
for q =q:-1:1
consider= q;
L = num2str(q);
if ceil(length(L)/2)-floor(length(L)/2)==0
nucount= length(L)/2;
else
nucount= floor(length(L)/2);
end
for count = 1:nucount
if L(count)==L(length(L)-(count-1))
palin=true;
else
palin =false;
break;
end
if palin ==true && count==nucount
n = q;
break
end
end
if palin==true
break;
end
end
for counter = (10^dig)-1:-1:10^(dig-1)
if mod(q,counter)==0
test = q/counter;
if test >10^(dig-1)
q = q-1;
break;
end
n = q;
break;
else
n =0;
end
end
if length(num2str(q))<dig
stop;
end
n = palin_product(dig,q);
end
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!