about palindrome number check

%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

Stephen23
Stephen23 on 14 May 2021
Edited: Stephen23 on 14 May 2021
"what should i do"
First you have to store the number using a data class that has sufficient precision for the number of digits you require (this might be char), otherwise any operations you perform on that number are meaningless.
How is the number provided to you?
how can i store enough precision for the number of digits,the calculation alway return result have 'e+',i have tried %format long
Stephen23
Stephen23 on 14 May 2021
Edited: Stephen23 on 14 May 2021
"...the calculation alway return result have 'e+',i have tried %format long"
The format command only controls how numbers are displayed, it it totally unrelated to how numeric values are stored.
"how can i store enough precision for the number of digits"
That really depends on the number of digits that you need to store, which so far you have not told us. General solution for arbitrary number of digits: use char.
How do you get this number: Is the number provided in a text file? Or in a binary file? Or via an automated code-checking tool that your school uses? Or do you have to enter it by hand / copy-and-paste it from your assigment task?
"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
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

i still dont get it,how can i store the digits with uint8 or char.the function uint8(n) and char(n) dont return satisfied result
Jan
Jan on 14 May 2021
Edited: Jan on 14 May 2021
Where do you get the numbers from? How are these numbers represented? Do they have more than 16 digits? This depends on the task you have to solve.
number = uint8([3,1,4,1,5,9,2,6,5])
% Of course this works with doubles also:
number = [3,1,4,1,5,9,2,6,5]
%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
%=> i use for loop to get that number
for i=1:inf
n=n+n
end
Break your number up into a series of decimal digits. Now proceed the same way you would in decimal, working with a vector of digits, doing carries. For example,
1 8 7 3 +
3 7 8 1
==========
4 15 15 4 ==> 4 15+1 5 4 ==> 4 16 5 4 ==> 5 6 5 4
5 6 5 4 +
4 5 6 5
==========
9 11 11 9 ==> 9 11+1 1 9 ==> 9 12 1 9 ==> 9+1 2 1 9 ==> 10 2 1 9 ==> 1 0 2 1 9
Jan
Jan on 15 May 2021
Edited: Jan on 15 May 2021
@Nguyen Huy: In the original question you wrote: n=1.76536927729736e+16 . This number is larger than 2^53. Therefore there is no exact representation of the integer value in double format. You cannot know the last digits of this number, so the input is either 17653692772973600, or 17653692772973601, or 17653692772973602, ... Therefore I asked you, where which input data are comming from and how they are provided. You do not answer this question for clarification.
d = dec2base(n, 10) - '0'; % Number in double format => digits
while ~isequal(d, flip(d))
d = d + flip(d);
% Now care for the carrying as explained by Walter. You need a
% loop from length(d):-1:1
end
for loop to inf is legal in MATLAB, but it will only loop to one less than one of the powers of 2. It would take a long long long time to get there. When you eventually control-c to interrupt, it will give you a message telling you the maximum it would have aimed for.
sscanf('176536927729736018', '%1d')
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

Asked:

on 14 May 2021

Edited:

Jan
on 15 May 2021

Community Treasure Hunt

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

Start Hunting!