How to check whether a integer of type mpz_t is a palindrome

Pedro Gimeno gmpdiscuss at formauri.es
Wed Feb 8 11:16:05 CET 2012


Just to throw another idea on the table...

The difference between a number and the base-10 reversed version of the
number is a multiple of 9. Couldn't that be used as a sieve?

For example, for 4 digits, abcd - dcba is a multiple of 9 for any number
with decimal digits a, b, c, d: 1523-3251 is -1728, multiple of 9;
1524-4251=-2727, multiple of 9; 1520-0251=1269, multiple of 9;
1424-4241=-2817, multiple of 9, and so on.

So, the sieving process could maybe calculate portions of the head and
the tail of the number, and see if their difference is a multiple of 9,
rejecting the number otherwise. If it passes the test, then a more
detailed analysis can be done.


More information about the gmp-discuss mailing list