Jennifer Luu
Jennifer Luu

Reputation: 45

Solving Python Riddle

Problem I am supposed to solve: The Riddler is planning his next caper somewhere on Pennsylvania Avenue. The address on Pennsylvania is a four-digit number with the following properties.

All four digits are different, The digit in the thousands place is three times the digit in the tens place, The number is odd, and The sum of the digits is 27.

So I made a for loop that checks each four digit integer and puts the value in a place holder (i.e. thousands, hundreds, etc.) And conditional if statements to fit the conditions. But my problem is that the IDE gives me no error when I run the code, but it does not print my statement at the end. I'm having trouble figuring out what is wrong with my code.

address = 0                     
thousand = 0
hundred = 0
ten = 0
one = 0

for address in range(1000,9999+1):

    thousand = (address/1000)%10    
    hundred = (address/100)%10
    ten = (address/10)%10
    one = (address%10)

if (thousand != hundred) and (thousand != ten) and (thousand != one) and (hundred != ten) and (hundred != one) and (ten !=one):
    if thousand == (3*ten):
        if one % 2 != 0:
            if thousand+hundred+ten+one == 27:
                print("The address is: ",address, "Pennsylvania Ave.") 

It runs but the print statement does not show up.

Upvotes: 1

Views: 785

Answers (2)

Nikos Laufer
Nikos Laufer

Reputation: 1

Number Theory

isop = []

for a in range(1, 10, 1):
    for b in range(0, 10, 1):
        for c in range(0, 10, 1):
            for d in range(0, 10, 1):
                if a!=b and a!=c and a!=d and b!=c and b!=d and c!=d:
                    sum = a+b+c+d
                    if a==3*c and sum == 27:
                        num = a*1000 + b*100 + c*10 + d
                        if num % 2 == 1:
                            isop.append(num)
for i in range(len(isop)):
    print(isop[i])

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114320

All four digits are different, The digit in the thousands place is three times the digit in the tens place, The number is odd, and The sum of the digits is 27.

  • The digit in the thousands place is three times the digit in the tens place: valid combos are 3x1x, 6x2x, 9x3x.
  • The sum of the digits is 27: only 9x3x is possible since 27 - (6 + 2) > 8+7, the maximum remaining digits. In fact 27 - (9 + 3) = 15. The remaining digits can be 9,6 or 8,7
  • All four digits are different: 9,5 is not an option. The number is either 9837 or 9738.
  • The number is odd: it's 9837.

Some pencil and paper works better than a for loop here.

That being said, keep in mind that / is the true division operator in Python 3, irrespective of type. So when address = 6754, for example

thousand = (address / 1000) % 10

evaluates as

thousand = 6.754 % 10

which is just 6.754, not 6 as you were probably hoping. To get 6, use the integer division operator //:

thousand = (address // 1000) % 10

Upvotes: 3

Related Questions