Math Applications

Python Applied to Fun Math Applications Problems

Let us apply built-in functions and User Defined Functions to Math Applications. We will connect math concepts you have learnt in school to Python statements.

Humankind’s current knowledge is based on the discoveries, inventions, and philosophical thoughts of so many great scientists who lived before us. You will get a brief look into the very important concepts developed by legendary scientists, mathematicians, and philosophers from 250 B.C. to the middle ages. Some of the works of famous mathematicians / philosophers we will learn about are Fibonacci and Archimedes. You will also learn about an important mathematical constant (pi) that appears in all scientific fields, like physics, math, and engineering.

In addition, we will revisit the recursive techniques we learned in earlier pages and apply them to several interesting concepts.

Let us start with a few basic math concepts.

Exponents

You saw in an earlier chapter that there is an arithmetic operator called exponents (symbol **).  Exponents mean to raise a number to a certain power. For example, 5**2 raises 5 to the power of 2 and is equal to 5*5 = 25. Similarly, 6**3 raises 6 to power 3 and is equivalent to 6*6*6, which is equal to 216. Now let us do an example to see how Python handles exponents.

PROGRAM EXAMPLE: EXPONENTS
>>> # Python program to find exponents of number
>>> # Use nested for-range loops.
>>> for n in range(2,5): #1) n is the exponent. x will be raised to the power n
	for x in range(2,6): #2) This is the inner loop.
		result = x**n
		print(x, ' to the power ', n, '= ', result)

		
2  to the power  2 =  4
3  to the power  2 =  9
4  to the power  2 =  16
5  to the power  2 =  25
2  to the power  3 =  8
3  to the power  3 =  27
4  to the power  3 =  64
5  to the power  3 =  125
2  to the power  4 =  16
3  to the power  4 =  81
4  to the power  4 =  256
5  to the power  4 =  625
>>>

Detailed Explanation of the Program:

The line numbers below refer to comments 1) and 2) in the above program.

  1. The program has two for loops.  The outer loop is for the exponent – the raise to the power number (n).
  2. The inner loop is for the number ‘x’, which we want to raise the power of.  This inner loop cycles x from 2 to 5.

‘n’ (outer loop counter) starts with a value of 2 and loops the inner loop (‘x’) from 2 through 5. 

In the first outer loop, n = 2, so x ( 2, 3, 4, 5) is raised to the power n, which is 2 currently.

For the second iteration of the outer loop, n becomes 3, so x (2, 3, 4, 5) is raised to the power 3 by the inner loop.

The above sequence repeats for n = 4.

After n = 4 loop is completed by looping ‘x’ through 2, 3, 4, and 5, the program stops.

Now let us see how Python can find the prime numbers

Math Applications – Prime Numbers

prime number is a whole number that cannot be divided by any number other than by ‘1’ and by itself.

Thus, the prime number does not have any factors. This means that a prime number cannot be formed by multiplying two smaller natural numbers.

In other words, a prime number is a whole number that is not a product of any two numbers, meaning they do not have any integer factors. Putting it another way, if you divide a prime number by any smaller number, it will always result in a remainder.

Prime Numbers Examples

For example, the number 11 is prime, because it cannot be divided by any other smaller number (from 2 to 10) except by itself.  On the other hand, the number 6 can be divided by 2 and 3, meaning it has factors 2 and 3. Therefore, the number 6 is not prime.   

The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.  Note that the number ‘1’ is not a prime number.

The number ‘2’ is the only even prime number.  All other even numbers are not prime numbers because all even numbers are divisible by 2, leaving no remainder.

All prime numbers are odd.

Largest Known Prime Number

In around 300B.C., Euclid proved that there are an infinite number of prime numbers. 

As a curiosity, the largest known prime number (as of January 2020) that has been discovered by using modern computers is (282,589,933 − 1). This number has 24,862,048 digits.

See the link below: https://en.wikipedia.org/wiki/Largest_known_prime_number

Composite Numbers

Composite numbers are the opposite of prime numbers. A number that can be formed by multiplication of two numbers is called composite number. They are integers that do have integer factors.

Any integer that is not prime is a composite number. It is ‘composed’ of other integer factors. For example, the number 12 is a composite number (and so is not prime) because it has the integer factors 2, 2, and 3 resulting in the product 2× 2 × 3 = 12.

How To Determine If A Number Is Prime Or Composite

How do we find out if a given number is prime or not? It is easy. 

Suppose we want to find out if the number n is prime.

The procedure to determine if a number is prime (or not) is as follows.

  • Divide the number ‘n’ by 2.
  • Check the division result to see if there is a remainder.
  • Continue dividing the number ‘n’ by numbers 2, 3, 4, 5…. up to (n – 1).
  • If the result of every division has a remainder, it means the number ‘n’ is not divisible by any number smaller than itself. This means the number ‘n’ does not have factors. Therefore, it is prime.
  • If, however, the result of any division produces a zero remainder, it means that the number ‘n’ is cleanly divisible by a number. Hence, it is not prime.

Using the above algorithm, we can write a Python program.  We will use the for-range loop. The program tests if the number 11 is prime.  As a next step, we will test if the number 12 is prime. You can use any number you like and test this approach.

PROGRAM EXAMPLE: HOW TO DETERMINE IF A NUMBER IS A PRIME NUMBER
>>> # Program to check if number 11 is prime.
>>> # A number, n, is prime if it not divisible by any numbers from 2 to n-1.
>>> # We will use for-range function to divide 11 
# by 2, 3, 4....10.
>>> # Use % operator. #1)
>>> # Recall that % operator gives us the remainder of the division operation.
>>> # PROGRAM 1
>>> n = 11
>>> for x in range(2,11):
	if n % x == 0: #2) Test if the number 11 by any number from 2 to 10.
		print(n, 'is divisible by ', x, '.', n, ' is not prime.')
		break  #3)
	else:
		print(n, 'is not divisible by any numbers.', n, ' is a prime number.')
		break

	
11 is not divisible by any numbers. 11  is a prime number. # Program result.
>>>
>>> # PROGRAM 2
>>> # Now check if the number 12 is a prime number by dividing 12 by 2, 3, 4….11.
>>> n = 12
>>> for x in range(2,12):
	if n % x == 0:
		print(n, 'is divisible by ', x, '.', n, ' is not prime.')
		break #4)
	else:
		print(n, 'is not divisible by any numbers.', n, ' is a prime number.')
		break #4)

	
12 is divisible by  2 . 12  is not prime.  # Program result.
>>>

Detailed Explantion of the Program:

The explanation below refers to the code lines 1), 2), and 3) in the program above.

Line 1): This step uses the Modulus (%) operator to determine if the division operation results in a remainder. You might recall that the modulus operator gives us the remainder of the division operation. A zero remainder means that the number being tested is divisible by the divisor.

Line 2) >>> for x in range(2,11):

if n % x == 0:

This step uses the for-range() function. It tests if the number 11 is divisible by any number from 2 to 10.

In the first loop of the range function, the looping variable ‘x’ = 2. So, the number 11 is divided by 2 using the modulus operator (%) to check for a remainder.

In the next loop of the range function, the looping variable ‘x’ is incremented by 1. Thus, the divisor becomes 3. The number 11 is divided by 3 using the modulus operator (%) to check for a remainder.

In the next loop of the range function, the looping variable ‘x’ is incremented by 1. Thus, the divisor becomes 4.

The above process is repeated for divisors 5, 6, 7, 8, 9, and 10. After completing the loop for x = 10, the program stops.

But, What If The Remainder Is Zero

This all sounds good. But if one of the divisions results in a zero remainder?

A zero remainder in one of the divisions would indicate that the number being tested is divisible by a divisor. Hence the number under test is not prime. If this case happens, there is no need to continue looping through the rest of the divisors.

If a division results in zero remainder, the program uses the Break statement to get out of the for loop. See line 3) and line 4) in the next section.

The Break Keyword

Line 3): The break keyword is used to stop a code from running. In the above program, we used a new statement, called break. The break statement breaks out of the for loop. When the test condition is evaluated as true, the program does not loop through the rest of the numbers specified in the range function. 

In PROGRAM 2, the program tests if the number 12 is prime or composite.

In the first loop, when the number 12 is divided by 2, the division results in zero remainder. This means the number 12 is divisible by the number 2. The condition 12 % 2 == 0 is evaluated to be true

The break statement then breaks out of the loop and does not test x = 3 through 11.

Also, note in the above program, there is an else: condition in the for loop. The else: part of the block is executed when the loop terminates after completing the whole list in the range () function. This is the case for the first break statement for PROGRAM 1 for n = 11 when the loop terminates after looping through all values of the count variable ‘x’ in the range function.

Math Applications – Factorial

You will learn about Factorials in your middle school math class. Factorials are very easy to understand.  The Factorial of a number ‘n’ is written as n! (called ‘n factorial’).

It is calculated as follows:

n! = 1*2*3*……….*n

As an example, 4! = 1*2*3*4 = 24.

We will write a Python program to calculate the factorial of numbers from 1 to 10.  We will use the for loop along with the range() function.

PROGRAM EXAMPLE: CALCULATE FACTORIAL OF A NUMBER
>>> # Program to calculate factorials
>>> # n! = 1*2*3*....*n
>>> # Define a variable 'factorial' 
>>> #and initialize it value 1 by assignment statement.
>>> factorial = 1                 #1)
>>> # Calculate factorial of numbers from 1 to 10
>>> for x in range(1,11):         #2)
	factorial = factorial * x #3) Multiply factorial by 2, 3, 4, ....10 in a loop
	print('The factorial of ', x, ' = ', factorial)

	
The factorial of  1  =  1
The factorial of  2  =  2
The factorial of  3  =  6
The factorial of  4  =  24
The factorial of  5  =  120
The factorial of  6  =  720
The factorial of  7  =  5040
The factorial of  8  =  40320
The factorial of  9  =  362880
The factorial of  10  =  3628800
>>>

Detailed Explanation of the Program:

Line 1): The program defines a variable called “factorial” and assigns (initializes) it a value of 1.

Line 2): for x in range(1,11):

Line 3): factorial = factorial * x

The for loop with the range function in line 2) loops the code line 3) from 1 to 10.

The program multiplies the value of the variable “factorial” from the previous iteration of the loop by the current value of the loop counter variable ‘x’ in each loop iteration.

Verified by MonsterInsights