about palindrome number check

9 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by