palindrome numbers

palindrome numbers

Hi,

does somebody in here know an algorithm to check if a given integer is a
palindrome?

Cheers
Robert

palindrome numbers

Hi Robert!

perl -le 'print +(reverse(\$ARGV[0]) eq \$ARGV[0]) ? q(yes) : q(no)' 1234567654321

Robert> Hi, does somebody in here know an algorithm to check if a given
Robert> integer is a palindrome?

Robert> Cheers Robert

--
Sincerely,
Dmitry

--- www.karasik.eu.org ---

Life ain't fair, but the root password helps.
- BOFH

palindrome numbers

Robert> Hi, does somebody in here know an algorithm to check if a given
Robert> integer is a palindrome?

Convert it to a textual representation (in 10-base), store it into a
character string and check if the string is a palindrome.

Robert> Cheers Robert

--

University of Tampere, Department of Computer and Information Sciences
Tampere, Finland                                      http://www.cs.uta.fi/~av/

palindrome numbers

On Wed, 04 Sep 2002 10:48:42 +0200, Robert Spielmann said:

Quote:> Hi,

> does somebody in here know an algorithm to check if a given integer is a
> palindrome?

Start:
While there are still digits in the number,
if the most significant digit !=  the least significant digit
Number is not a palindrome. Quit.
else
discard least significant & most significant digits & goto
Start

Number is a palindrome.

Hope this helps.
Dave.

--
David Neary,
E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html

palindrome numbers

> On Wed, 04 Sep 2002 10:48:42 +0200, Robert Spielmann said:
> > Hi,

> > does somebody in here know an algorithm to check if a given integer is a
> > palindrome?

> Start:
> While there are still digits in the number,
>   if the most significant digit !=  the least significant digit
>     Number is not a palindrome. Quit.
>   else
>     discard least significant & most significant digits & goto
>     Start

> Number is a palindrome.

> Hope this helps.
> Dave.

Here's another:

1) Convert number to decimal string. If it already is, make sure it's
perfectly clean (no decimal point, etcetera).

2) Make a copy of the string with the contents reversed.

3) Compare the two strings. If they're the same, the number is a
palindrome.

And here's one more:

1) Make two copies of the number, one as an integer or long type, one
as a string.

2) Reverse the string and convert to the same type as the number

3) Compare the two numbers. If they're the same, the number is a
palindrome.

What works best depends on a lot of factors including how you got the
number in the first place, how large it might be, and what tools you're
using (bash or C?).

DS

I was reading one of the Kerninghan's bibles and found an exercise
that I can't solve.

Kerninghan says to search palindromes (words spelled the same backwards
as forwards) in a dictionary using grep.

This is what I have done:
egrep '^(\(.\)\(.\)\(.\)\3\2\1)|(\(.\)\(.\)\5\4) ' \$1

it is only for 4 and 6 letter words but it doesn't work; the error message
says:
egrep: Invalid back reference

it works when I search only for 6 letter words but it fails when I
add the grouping parentheses and | to test also for 4 letter words

Why does it fail?
Thanks a lot.

--
________________________________________________
/                                               /|
/  Quitar ANTIBASURA para contestar por e-mail  / |
/    Remove ANTIBASURA to reply via e-mail      / /
/       Manuel Moreno - Albacete - Spain        / /
/                                               / /
/_______________________________________________/ /
|_______________________________________________|/