1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Working with digits in Python

  1. Jul 17, 2012 #1
    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..
    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
    return False
  2. jcsd
  3. Jul 18, 2012 #2


    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?
  4. Jul 18, 2012 #3


    User Avatar
    Science Advisor

    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.
  5. Jul 18, 2012 #4
    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
    || return True
  6. Jul 18, 2012 #5


    User Avatar
    Science Advisor

    Whats the data type of the numbers? Integer? Long? How many bits allocated?
  7. Jul 18, 2012 #6


    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
  8. Jul 18, 2012 #7
    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..
  9. Jul 18, 2012 #8


    Staff: Mentor

    Thanks - yes I meant % but somehow typed &.
  10. Jul 18, 2012 #9
    Consider converting your integer to a string:
    s = str(x)

    Then you can deal with the characters in the string-- i.e., digits.
  11. Jul 18, 2012 #10


    User Avatar
    Science Advisor
    Homework Helper

    Now that's a excellent suggestion.
  12. Jul 19, 2012 #11
    Thanks!! That's exactly what I was looking for...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook