about palindrome number check

4 views (last 30 days)
Nguyen Huy
Nguyen Huy on 14 May 2021
Edited: Jan on 15 May 2021
%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 Comments
Nguyen Huy
Nguyen Huy on 14 May 2021
"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 on 14 May 2021
As Jan wrote you could store the digits as CHAR or UINT8, which also makes your palindrome checking easy.

Sign in to comment.

Accepted Answer

Jan
Jan on 14 May 2021
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 Comments
Nguyen Huy
Nguyen Huy on 15 May 2021
thanks guy,i tríed walter way and success
Jan
Jan on 15 May 2021
Edited: Jan on 15 May 2021
@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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!