# Working with digits in Python

1. Jul 17, 2012

### Avatrin

1. The problem statement, all variables and given/known data
This is not about one problem, it is rather about a type of problem:

How do I define, in Python, a number which is defined by its individual digits? For instance:
Pandigital numbers..
Palindromes..
Circular primes..

What commands do I need to work with to find these kinds of numbers?

2. Relevant equations
An n-digit number is pandigital if it makes use of all the digits 1 to n exactly once.

A palindromic number reads the same both ways.

3. The attempt at a solution
I dont know what commands to use.

def palindrome(n):
if n is ??:
return True
else:
return False

2. Jul 18, 2012

### Staff: Mentor

There are no "commands" to use. You need to write code that will test a number to do these things. Before you start writing code, you need to start with the definitions of these terms, and see what you can put together as an algorithm. For example, what does it mean that a "palindromic number reads the same both ways"? If I told you that I was thinking of a 3-digit number, how would you determine whether it was palindromic, without me telling you the number?

3. Jul 18, 2012

### chiro

Hey Avatrin and welcome to the forums.

Following on from Mark44's advice, can you define for us the things which are trying to check or convert to?

Even if its not in code, doing this will help you actually get to that stage (and you need to do this before you can even write the code anyway). Just try and write it in english and then make it more formal so we can see whats going on in your head.

4. Jul 18, 2012

### Avatrin

Well, a 3-digit palindrome has the form xyx (x can be equal to y). For example:
909, 414 and 333

So, if I am testing a 3-digit palindrome:
if first digit = third digit:
Return True

If I am testing a 4-digit palindrome:
if first digit = fourth digit and second digit = third digit:
Return True

The reason I asked for commands is because I dont know how to isolate individual digits. I dont even know if the pseudo-code above is how the actual code will look like.

Testing pandigital numbers:
length(number) = n
for i in range(n):
|| if i+1 is not in number:
|||| return False
|||| break
else:
|| return True

5. Jul 18, 2012

### chiro

Whats the data type of the numbers? Integer? Long? How many bits allocated?

6. Jul 18, 2012

### Staff: Mentor

You can peel off the 1's digit by using the modulus operator, &, working in modulo 10.

For example, 931 & 10 == 1

You can shift all of the digits to the right by doing integer division by 10.

For example, 931 / 10 == 93

7. Jul 18, 2012

### Avatrin

Well, I am trying to solve various problems on Project Euler that involve digits in numbers (problems 4, 8, 16, 20 etc). So, yes, I am talking about integers. The largest integer we are talking about has 1000 digits (problem 8).

I think you mean 931%10..

8. Jul 18, 2012

### Staff: Mentor

Thanks - yes I meant % but somehow typed &.

9. Jul 18, 2012

### awkward

Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.

10. Jul 18, 2012

### Dick

Now that's a excellent suggestion.

11. Jul 19, 2012

### Avatrin

Thanks!! That's exactly what I was looking for...