Reverse number and add until palindrome help?

I’m trying to make an Applescript that when a number is entered, it gets reversed and added to itself until it is a palindrome. This is my code:

set interror to 0
repeat until interror is 2
	set interror to 0
	try
		display dialog "Enter number below" default answer ""
		set pal to text returned of result as integer
	on error
		set interror to 1
	end try
	if interror is 0 then set interror to 2
end repeat
repeat
	set backwards to reverse of characters of pal as integer
	if backwards = pal then exit repeat
	set pal to backwards + pal
end repeat
display dialog pal

When I run it, if I enter in a number, say 12, it crashes and this is the result:

Result:
error "Can't make reverse of every character of 12 into type integer." number -1700 from reverse of every character of 12 to integer

I’m pretty new to this, so I don’t know why it’s doing this. Any help? Thanks

Hi,

characters affects only text, not numbers.
This solution uses two variables, one as number for the math and one as text for the character reversing


repeat
	try
		display dialog "Enter number below" default answer ""
		set pal to text returned of result
		set palInt to pal as integer
		exit repeat
	end try
end repeat
repeat
	set backwards to (reverse of characters of pal as text) as integer
	if backwards = palInt then exit repeat
	set palInt to backwards + palInt
	set pal to palInt as text
end repeat
display dialog pal


the as text coercion is necessary, because the result of characters is a list

Hi, kyle1320. Welcome to MacScripter.

I don’t know if your script is intended to address a need or is just a coding exercise. In the latter case, Stefan’s answered your question. I’d just add that when coercing a list to text, it’s always good practice to set ‘AppleScript’s text item delimiters’ explicitly to a known value first ” in this case the empty string “” ” to ensure that no unwanted characters are inadvertently interpolated into the text. (I’ve done this in my own script below.)

My own effort here adds the individual digits of the numbers. While more cumbersome than yours, it avoids some of the problems encountered when coercing between numbers and text and can thus handle much larger “numbers” (though it may take a while with enormous ones!) and negatives.


repeat
	try
		display dialog "Enter number below" default answer ""
		set n to (text returned of result) as integer
		exit repeat
	end try
end repeat

-- Get a list of the individual digits as integers.
set isNegative to (n < 0)
if (isNegative) then set n to -n
set digits to {n mod 10 div 1}
set n to n div 10
repeat until (n is 0)
	set beginning of digits to n mod 10 div 1
	set n to n div 10
end repeat

-- If the number's not already a palindrome, add its reverse to it.
-- Repeat with successive results until a palindrome emerges.
set backwards to reverse of digits
repeat until (backwards = digits)
	set carry to 0
	repeat with i from (count digits) to 1 by -1
		set columnSum to (item i of digits) + (item i of backwards) + carry
		set item i of digits to columnSum mod 10
		set carry to columnSum div 10
	end repeat
	if (carry > 0) then set beginning of digits to carry
	set backwards to reverse of digits
end repeat

-- Coerce the result to text.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
if (isNegative) then
	set pal to "-" & digits
else
	set pal to digits as text
end if
set AppleScript's text item delimiters to astid

display dialog pal

I gave this a go, just for interest and got some strange results with the number 5798. With Stephan’s script gives this message:

With Nigel’s script it just runs until I get bored and hit the stop button.

Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

I just took it on trust that a palindrome would be reached. :rolleyes: It seems that in some cases, there either isn’t one or it’s very large…

Thanks for the help all, I added a counter so it will stop after 100 loops, and an array to see the process it goes through. This is just a coding exercise, trying to get more familiar with the basics of AS. I’m not sure what all this does (from Nigel’s code):

 set carry to 0
   repeat with i from (count digits) to 1 by -1
       set columnSum to (item i of digits) + (item i of backwards) + carry
       set item i of digits to columnSum mod 10
       set carry to columnSum div 10
   end repeat
   if (carry > 0) then set beginning of digits to carry

Could someone explain this to me? I think I understand the rest. Are you adding the digits together manually? Why can’t you just add them using +?

Yes, as if you were adding the columns on a piece of paper.

-- digits is a list of integers representing the digits of the input number, eg. {7, 8, 9}.

set backwards to reverse of digits --> {9, 8, 7}.
repeat until (backwards = digits)
	-- There's no carry before you start, so make it zero for the first iteration of the repeat below.
	set carry to 0
	-- Repeat with all the "columns" in the numbers, starting from the right (ie. the units).
	repeat with i from (count digits) to 1 by -1
		-- Add together the two numbers' digits in this column, plus any carry.
		set columnSum to (item i of digits) + (item i of backwards) + carry
		-- Put the units digit of the result back into the digits list at that point.
		set item i of digits to columnSum mod 10
		-- Keep the tens digit of the result as a carry for the next addition.
		set carry to columnSum div 10
	end repeat
	-- If there's a carry after adding all the exisiting columns, insert it at the beginning of the digits list, creating a new column at the beginning.
	if (carry > 0) then set beginning of digits to carry
	
	-- digits now contains the digits of the addition result.
	-- Get the reverse of that and go back to the top of the outer repeat.
	set backwards to reverse of digits
end repeat

If you mean “Why can’t you just add the complete numbers together?”, the answer is, of course, that you can. But once the results start to get very large, they’ll be rendered into text in exponential notation, eg. “9.87654321E+8”. The reversed versions of these will be be totally useless, which is why I decided to do things the hard way. :wink:

Hard, perhaps, but rather elegant just the same. :cool: