about palindrome number check

9 次查看(过去 30 天)
%i have number n and i want to check whether that is palindrome
%but the n is too big so i cant use this code,what should i do
%example n= 1.76536927729736e+16
%my code is
if n==str2num(flip(num2str(n)))
flag=true
else
flag=false
end
  5 个评论
Nguyen Huy
Nguyen Huy 2021-5-14
"That really depends on the number of digits that you need to store, which so far you have not told us".Sry about that,i have number 'n',and i have to sum=n+ reverse(n) until i have palindrome
example: n=347
so sum=347+734=1292, 1292+2921=4213, 4213+3124=7337 then stop,because 7337 is palindrome
so i dont know how many digits the sum will have
Stephen23
Stephen23 2021-5-14
As Jan wrote you could store the digits as CHAR or UINT8, which also makes your palindrome checking easy.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-5-14
For numbers up to 2^53 the type double represents the digits exactly. This is 9-0e15. See flintmax.
Your example n=1.76536927729736e+16 exceed this limit. Therefore doubles are not useful fpr your needs. You cannot even store the value exactly in a double. Maybe you need an UINT8 vector containing the digits, or a CHAR vector.
It does not matter, if the value is displayed in the e notation or not, because this can be controlled by the command: format
  9 个评论
Nguyen Huy
Nguyen Huy 2021-5-15
thanks guy,i tríed walter way and success
Jan
Jan 2021-5-15
编辑:Jan 2021-5-15
@Walter Roberson: You are right. Thanks for finding this wrong assumption.
When I type this in the command window:
for k = 1:inf, end
No error occurs. Stopping this with Ctrl-C show a warning:
Warning: FOR loop index is too large. Truncating to 2147483647. % R2009b/32
Warning: FOR loop index is too large. Truncating to 9223372036854775807. % R2009b/64, 2018b
I've deleted my wrong claim. Maybe this failed in Matlab 6.5 and I did not try it again.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by