How to Define and Work with Digits in Python?

  • Context: Python 
  • Thread starter Thread starter Avatrin
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around how to define and work with numbers in Python based on their individual digits, focusing on concepts such as pandigital numbers, palindromes, and circular primes. Participants explore the necessary commands and algorithms to identify these types of numbers.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation

Main Points Raised

  • Some participants express uncertainty about the commands needed to define and test properties of numbers based on their digits.
  • One participant suggests that a palindromic number reads the same both ways and proposes a structure for testing 3-digit and 4-digit palindromes.
  • Another participant emphasizes the importance of defining the terms and algorithms before writing code.
  • Several participants discuss methods for isolating individual digits, including using the modulus operator and integer division.
  • There is a suggestion to convert integers to strings to facilitate working with individual digits.
  • Clarifications are made regarding the correct use of the modulus operator in examples provided.

Areas of Agreement / Disagreement

Participants generally agree on the need to define the properties of numbers and explore algorithms before coding. However, there is no consensus on specific commands or methods, as various approaches are proposed and discussed.

Contextual Notes

Some participants mention specific problems from Project Euler that involve digits in numbers, indicating a focus on integers, particularly large ones with up to 1000 digits.

Avatrin
Messages
242
Reaction score
6

Homework Statement


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?

Homework 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.

The Attempt at a Solution


I don't know what commands to use.

def palindrome(n):
if n is ??:
return True
else:
return False
 
Technology news on Phys.org
Avatrin said:

Homework Statement


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?

Homework 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.

The Attempt at a Solution


I don't know what commands to use.

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

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?
 
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 what's going on in your head.
 
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 don't know how to isolate individual digits. I don't 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
 
Whats the data type of the numbers? Integer? Long? How many bits allocated?
 
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
 
chiro said:
Whats the data type of the numbers? Integer? Long? How many bits allocated?

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).

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

For example, 931 & 10 == 1
I think you mean 931%10..
 
Thanks - yes I meant % but somehow typed &.
 
Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.
 
  • #10
awkward said:
Consider converting your integer to a string:
s = str(x)

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

Now that's a excellent suggestion.
 
  • #11
awkward said:
Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.
Thanks! That's exactly what I was looking for...
 

Similar threads

Replies
55
Views
7K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
6K