palindrome numbers

palindrome numbers

Post by Robert Spielman » Thu, 05 Sep 2002 17:48:42



Hi,

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

Cheers
Robert

 
 
 

palindrome numbers

Post by Dmitry Karasi » Thu, 05 Sep 2002 20:26:19


        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

Post by Arto Viitane » Thu, 05 Sep 2002 20:34:09


 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

Post by Dave Near » Thu, 05 Sep 2002 22:18:29


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

Post by David Schwart » Thu, 05 Sep 2002 23:56:28



> 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

 
 
 

1. searching palindromes with grep

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        / /
 /                                               / /
/_______________________________________________/ /
|_______________________________________________|/

2. EMERGENCY!!!!!!!!HOW TO CONVERT ALL USER ACCOUNTS TO NEW NAMING CONVENTION

3. Help: Palindrome Fast 2000

4. X crashing

5. Palindrome backup for Linux?

6. 2.5.47: oops/panic in ide_inint_queue

7. Number Nine....Number Nine....Number Nine

8. crating a static executable from shared objects.

9. Need to increase number number of waitstates...

10. Numbering a text file (UNIX 'number' command)

11. eth0: mismatched read page pointers [number] vs. [number] error

12. Grep-ing numbers that appear before a particular number?

13. how to get instance number from minor number?