about palindrome number check

조회 수: 8 (최근 30일)
Nguyen Huy
Nguyen Huy 2021년 5월 14일
편집: Jan 2021년 5월 15일
%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