0

I'm trying to use Python to solve my maths homework (you have a two digit number ab, and ab+ba=88) and my program doesn't work.

 a=10
 b=10
 for i in range(100):
      a+=1
      for i in range(100):
          b+=1
          if int(str(a+b))+int(str(b+a))==88:
               print((str(a+b))+"+"+str(b+a))

It gives the output of 44. This isn't the good answer, because a and b have to be different numbers, but I didn't code that restriction yet. More important, the code should find 26;62 and other solutions. How can I find all the solutions? I know the answers, but I want to solve it in this way too.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • `a` and `b` are numbers, so `a+b` is the sum of it, `str(a+b)` is this sum converted to a string and `int(str(a+b))` is that string converted back to number (therefore equivalent simple `a+b`). – Michael Butscher Sep 06 '18 at 18:44
  • Solving it mathematically might be easier... a * 10 + b + b * 10 + a = 88 implies 11(a + b) = 88 implies a + b = 8 implies a = 8 - b. Since a and b are digits, they have to be less than 10 and greater or equal to 0, so b can be 0 to 8 which means a can be 0 to 8 as well. – Derek 朕會功夫 Sep 06 '18 at 18:48

4 Answers4

1

You have a couple of logic problems here. The first is your loop control. You start with

a=10
for i in range(100):
      a+=1

This will run a over the range 10 to 10+100. Now, for each value of a, you do:

b=10
...
  for i in range(100):
      b+=1

First of all, you've changed the value of i, which the outer loop is still trying to use. This is the first problem, the one that's killing your current program. Second, if you fix this by changing the loop index to j (for instance), then you will increment b 100*100 times, so it ends up at 10010.

On top of this, a and b are never individual digits! You've started each as a 2-digit number.

Instead, make two changes:

  1. Put the values you actually want into the for loop.
  2. Use strings, not integers, if that's what you're trying to do.

    for a in "0123456789": for b in "0123456789": if int(a+b) + int(b+a) == 88:

This code makes 2-digit strings. Another way is to work with the single-digit integers, but then use algebra to make the 2-digit numbers:

if (10*a + b) + (10*b + a) == 88: 

Finally, you can simplify the entire process by doing basic algebra on the above expression:

(10*a + b) + (10*b + a) == 88
11*a + 11*b == 88
a + b = 8

From here, you note simply that b = 8-a, and you can reduce your program to a single loop.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Due to a scoping quirk of python for loops, using `i` for both loop control variables won't actually cause a problem. Try it. – Simon Sep 06 '18 at 19:02
1

If I understand correctly, you have a two digit number ab such that ab + ba = 88

Ex) For "ab" = 26 --> 26 + 62 = 88.

If that is the case, this is the solution I propose the solution:

for a in range(10):                          # Iterate over ten's place values [0 to 9]
    for b in range(10):                      # Iterate over one's place values [0 to 9]
        ab = str(a)+str(b)                   # Construct the string "ab"
        if (int(ab) + int(ab[::-1]) == 88):  # Test if ab + ba = 88
            print(ab + " + " + ab[::-1])     # Print the successful value of ab

The logic is that you construct a string ab by combining the possible values of 0 to 10 in each of the ten's and one's place of the two digit number. Then, test if the integer value of ab and its reverse, ba, are equal to 88.

OUTPUT:
08 + 80
17 + 71
26 + 62
35 + 53
44 + 44
53 + 35
62 + 26
71 + 17
80 + 08

NOTE: This method uses a string from the get go in order to avoid the many calls to int(str()).

NOTE: In Python, the operator [::-1] returns the reverse of the string. Just a convenient trick!

  • Thank you dude! I wanted to solve it like your solution, but I didn't remember many things, but now I do! This is the right solution, you understood correctly! :) :) – Zoltán Jeney Sep 06 '18 at 19:31
  • Sure thing! Just one word of advise, it was a little hard to find exactly what you were trying to do in your original post. Next time try to make the problem statement a little more clear, maybe with an example or something. Otherwise, this was a fun problem to solve! – CJ Barcelos Sep 06 '18 at 19:36
0

Let's walk through what your code does.

First you assign both a and b to be 10

a=10
b=10

Then you start a loop that will run 100 times, each time it runs it will increment a by one

for i in range(100):
    a+=1

Then you start a nested loop which will run 100 times each time the outer loop runs once and increments b on each loop. It is important to note that because you have defined b outside of the scope of this loop, it will keep growing. The second time the outer loop runs, b is going to start at 110

What you need to know here is that python is a weakly typed language. Variables take on the same type as the data assigned to them. a and b are integers, so you can already perform mathematical operations on them. Also, there's no reason to typecast the same variable twice in one spot. you should write the if statement like this:

if a + b + b +a == 88:

You can see more easily now that all it is checking is if (a + b) * 2 is equal to 80

Finally you output your answer but, do to the commutative property of addition, you're always going to show the same sum + itself

print((str(a+b))+"+"+str(b+a))

What I would recommend doing here, besides cleaning up your if statement, is resetting b each time the outer loop runs. I'm also unclear why you've instantiated your variable to equal 10 but that's your business.

Try this:

a=10
for i in range(100):
     b=10
     a+=1
     for i in range(100):
         b+=1
         if a+b == 88:
              print((str(a))+"+"+str(b))
Simon
  • 855
  • 9
  • 24
0

Use this code to understand:

a=10
b=10
for i in range(100):
    a+=1
    for i in range(100):
        b+=1
        if int(str(a+b))+int(str(b+a))==88:
            print(a)
            print(b)
            print((str(a+b))+"+"+str(b+a))
            break;

Output:

11
33
44+44

So:

A = 11 B = 33

justcode
  • 1,562
  • 3
  • 14
  • 25