Python math program help

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Python math program help

I have math extra credit problems where i have to take 4 numbers, and then find the lowest and greatest products of two two-digit numbers. For example:

The four numbers are 1,2,3,4
Where the least product would be 322 (14x23) and the greatest would be 1312 (41x32).

Anyways, i was wondering if there was a way to create a program in Python where it would find this for me. I don't really want an actual code, just some tips on ways to go about doing this, kinda like how the mission forums work. I don't really care about the extra credit, because Its only worth about 5 points and i already have an a in the class, i just would like the satisfaction of being able to do this (new to python.) thanks in advance
AllByte
New User

Posts: 8
Joined: Thu May 05, 2011 7:47 pm
Blog: View Blog (0)

Re: Python math program help

You could store the four numbers in a string, then use int() to convert pairs into integers that you can multiply, store those results in a list and use min() and max()
"Well it isn't my fault. I shouldn't have been allowed to do something to crash it." "No, you shouldn't have been allowed to buy a computer in the first place"
Phantom Wolf
Poster

Posts: 271
Joined: Wed Mar 03, 2010 8:45 pm
Blog: View Blog (0)

Re: Python math program help

You can do this in Java similarly.
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)

Re: Python math program help

There might be an alternate way as opposed to just brute forcing the all numbers to find the biggest multiple and smallest multiple. I think i might have found a pattern, so correct me if I'm wrong:

Take for instance the number sequence 4 5 6 7 8 9. The smallest number i can make is 47 * 56 = 2632. The way the pattern works is you take the first number (of an ordered sequence, in python you can use .sort() method on a list) in this case the first integer is 4, and use it as the tens for the first number. So it is now 4X * XX (i will use X's for the numbers to come)

Then you take the second smallest number (5 in this case) and use it as your tens for the second 2 digit number. Now 4X * 5X

You now take the 3rd smallest number (6) and use it as your ones for the 2nd number. 4X * 56

Lastly you take the 4th smallest number and use it as your first number's ones. All of this can be accomplished quite easily in python using list indexes. Leaving you with the final answer of 47 * 56

As for finding the highest multiple just reverse the list using .reverse() (so it is in descending order) and just repeat the same procedure above. Good luck
I can't come to bed...
Someone is WRONG on the internet

http://xkcd.com/386/

jgreen45
Poster

Posts: 106
Joined: Wed Feb 25, 2009 6:18 pm
Blog: View Blog (0)

Re: Python math program help

jgreen45 wrote:Take for instance the number sequence 4 5 6 7 8 9. The smallest number i can make is 47 * 56 = 2632.

57 * 46 = ?

Just switch the order a little and it should work ( I think ).

Gatito
Poster

Posts: 113
Joined: Tue Mar 08, 2011 12:55 pm
Blog: View Blog (0)

Re: Python math program help

Gatito wrote:
jgreen45 wrote:Take for instance the number sequence 4 5 6 7 8 9. The smallest number i can make is 47 * 56 = 2632.

57 * 46 = ?

Just switch the order a little and it should work ( I think ).

Damn, i got all excited. Well back to the drawing board
I can't come to bed...
Someone is WRONG on the internet

http://xkcd.com/386/

jgreen45
Poster

Posts: 106
Joined: Wed Feb 25, 2009 6:18 pm
Blog: View Blog (0)

Re: Python math program help

Thanks Phantom Wolf, ill look into that.

-- Sat May 07, 2011 8:13 pm --

Code: Select all
`num1=4num2=5num3=6num4=6a=(str(num1)+str(num2))*(str(num3)+str(num4))b=(str.num1+str.num2)*(str.num4+str.num3)`

What im trying to do is create the variables by adding the strings of the "nums", and then put the variables in a list, and then printing the max and min of that list (I know it's not the greatest, like i said, im new to python so it's all I know.) But in the first case (a), this error occurs: TypeError: can't multiply sequence by non-int of type 'str'. And in the case of b, it says AttributeError: type object 'str' has no attribute 'num1' . Someone tell me what im doing wrong please. I've spent way too much time on this.

P.S. Im using Python 3
AllByte
New User

Posts: 8
Joined: Thu May 05, 2011 7:47 pm
Blog: View Blog (0)

Re: Python math program help

I'm no python expert but this doesn't seem right to me.

a=(str(num1....
b=(str.num1....
First you use "(" and then "." ! I'm pretty sure that causes the AttributeError: type object 'str' has no attribute 'num1.

Anyways when you deal with numbers and mathematical oprations you don't use strings, unless you want to do something fancy!

This is the right way.
Code: Select all
`num1=4num2=5num3=6num4=6a= (num1*10 + num2) * (num3*10 + num4)b= (num1*10 + num2) * (num4*10 + num3)`

Gatito
Poster

Posts: 113
Joined: Tue Mar 08, 2011 12:55 pm
Blog: View Blog (0)

Re: Python math program help

Gatito's solution would work just fine, but I was thinking that you'd want to input the numbers instead of changing and re-running the script.

Your code has two problems, one like Gatito pointed out is that str doesn't have a num1 attribute. The other is you're trying to multiply strings. Here's your code corrected
Code: Select all
`num1 = '4'num2 = '5'num3 = '6'num4 = '6'a = int(num1 + num2) * int(num3 + num4)b = int(num1 + num2) * int(num4 + num3)`

See how I convert the strings to integers before multiplying them? But, of course, they're still strings while being added.

Also, you can put the variables directly into a list, instead of doing it later.
"Well it isn't my fault. I shouldn't have been allowed to do something to crash it." "No, you shouldn't have been allowed to buy a computer in the first place"
Phantom Wolf
Poster

Posts: 271
Joined: Wed Mar 03, 2010 8:45 pm
Blog: View Blog (0)

Re: Python math program help

Ok, thank you both so much for the help. I think i just misunderstood how strings worked. I think i got the program to work now, thanks.
AllByte
New User

Posts: 8
Joined: Thu May 05, 2011 7:47 pm
Blog: View Blog (0)

Next