Monday, June 27, 2022

How to solve simple math problems in Python?

Python programming language like any other programming language has a way of solving simple math problems. The basic math operations that are integrated in Python are: addition (+), subtraction (-), multiplication (*), division (/), power (**), modulo (%), less-then (<), greater-than (>), less-than-equal (<=), greater-than-equal (>=). These operations are used without any library import. Python has a built-in library called math which has all commonly used math functions. However, here we are investigating simple math operations without the help of any library.
Example 1 Solve the following simple mathematical problems and print their solution.
  • 3 + 15 = ?
  • 4*6-3 = ?
  • 3*7 + 4*8 = ?
  • 3**2 - 4**3 = ?
  • 9**(1/2) + 27**(1/3) = ?
  • (10%12) - (3%4) = ?
  • 5+3 < 4+8
  • 50 > -20
  • 30 >= -4
  • 10 <=-4
Solution:
print("3 + 15 = ",3+15)
print("4 * 6 - 3 = ", 4*6-3)
print("3 * 7 - 4 * 8 = ", 3 * 7 - 4 * 8)
print("3**2 - 4**3 = ", 3**2 - 4**3)
print("9**(1/2) + 27**(1/3) = ", 9**(1/2) + 27**(1/3))
print("(10%12) - (3%4) = ",(10%12) - (3%4) )
print("Is 5 + 3 less than 4 + 8", 5+3 < 4+8)
print("Is 50 greater than -20?", 50 > -20)
print("Is 30 greater or equal to -4?", 30 >= -4)
print("Is 10 less or equal to -4?", 10 <= -4)
The output of previous code is given below.
3 + 15 =  18
4 * 6 - 3 = 21
3 * 7 - 4 * 8 = -11
3**2 - 4**3 = -55
9**(1/2) + 27**(1/3) = 6.0
(10%12) - (3%4) = 7
Is 5 + 3 less than 4 + 8 True
Is 50 greater than -20? True
Is 30 greater or equal to -4? True
Is 10 less or equal to -4? False
In the previous example, we saw that it is possible to use basic mathematical operations without the need to call any library. In the following example, we will show the calculation of potencies and roots also without the need to import any Python libraries.
Example 2 Calculate the following mathmatical problems. \begin{eqnarray} 2^2 &=&?\nonumber\\ 2^2 + 3^2 &=& ? \nonumber\\ 4^2 - 3^2 + 2^2 &=& ? \nonumber\\ \sqrt{64} &=& ?\nonumber\\ \sqrt{256} - \sqrt{196} &=& ?\nonumber\\ \sqrt[3]{1000} &=& ? \end{eqnarray}
Solution: The potencies are written with two asterix sign so if we have to write \(2^2\) in Python we will write 2**2. The roots will be written in form of potencies in the form of fractions. For example \(\sqrt{64}\) in Python it will be written as 64**(1/2). All the expressions will be solved/written inside the print function just like in the Example 1.
print("2**2 = ",2**2)
print("2**2 + 3**2 = ",2**2+3**2)
print("4**2 - 3**2 + 2**2 = ",4**2-3**2+2**2)
print("64**(1/2) = ",64**(1/2))
print("256**(1/2) - 196**(1/2) = ",256**(1/2)-196**(1/2))
print("1000**(1/3) = ",round(1000**(1/3),2))
The output of previous code is given below.
2**2 =  4
2**2 + 3**2 = 13
4**2 - 3**2 + 2**2 = 11
64**(1/2) = 8.0
256**(1/2) - 196**(1/2) = 2.0
1000**(1/3) = 10.0
The output in this example is pretty straightforward. The most interesting part of this example is the last command line in which we calculated the \(\sqrt[3]{1000}\). The result of this code is 10 since 10*10*10 is equal to 1000. However, without the use of the round function, the result was 9.9999998. The round function is a built-in Python function that returns a floating-point number that is the rounded version of the specified number, with the specified number of decimals. Since the result was near 10 i.e. 9.99998 using the round function we rounded the number to 10.0. The general form of the round function can be written as:
round(number, number_of_decimals)
So the first argument is the number that we want to round up, and the second argument is the number of decimals that the rounded number will contain. The default number of decimals is 0, which means that the function will return the nearest integer.

No comments:

Post a Comment