Sitemap
Chamika Jayasinghe

Chamika Jayasinghe

Computer Science Engineering Undergrad | DevOps Enthusiast | Quality over Quantity

Solving Math Using Python

8 min readAug 14, 2022

In my previous article I discussed about some cool projects to build, in order to polish your Python skills. In case you haven’t read it yet, I’d recommend you to read that first. You can find the link to that post here. In this article, I am going to talk about how you can use Python to solve some interesting mathematical problems. I will be discussing them from easier questions to more difficult ones.😎 Make sure to read the article till the end as I am going to try my best to explain each questions one by one so that you could follow along easily. To make the most out of this, make sure to code them on your own before looking at the explanation.

Press enter or click to view image in full size
Math with Python

Here is what you are going to learn.

  1. Odd & Even numbers
  2. Prime numbers
  3. Triangular Numbers
  4. Finding The Least Common Multiple
  5. Fibonacci Sequence
  6. The Collatz Conjecture
  7. ODD & EVEN NUMBERS

If a natural number is evenly divisible by 2 then that number is considered even. If not it is considered an odd number.

Identifying odd & even numbers

Let’s now build a Python program to check whether a given number is even or odd.

  • First of all we need to get the user input. Here we should make sure to get an integer input from the user.
  • Then we should check whether there is a remainder, after dividing the inputted number by 2. If there is, we need to print “X is an odd number”. If there is no remainder then we should print “X is an even number.” (Here X refers to the number inputted by the user.)
  • Hint:- You can use the modulo operator (%)to do this task.
Press enter or click to view image in full size
Checking whether a number is odd or even

Let’s now see how to find all the even numbers within a given range. Here are the features, our program should have.

  • First of all, the program should ask the user for the starting and the ending index.
  • Once the user has given the starting and the ending values then we need to check for the numbers within that certain range one by one. If a number is even, then we need to add those numbers to a separate list. (I’ve named this list as “even”)
  • Finally we need to print out that list containing all the even numbers within the range.
Press enter or click to view image in full size
Getting even numbers within a range — Solution Code no. 1

Here is how you can solve this question using a lambda function. Lambda functions are one time used throwaway functions. If you want to learn more about Lambda functions, then refer this.

even = list(filter(lambda x:  x % 2 == 0, range(user1, user2)))
Press enter or click to view image in full size
Getting even numbers within a range — Solution Code no. 2

Here is how you can solve this question using List comprehensions. You can find more about list comprehensions here.

even = [i for i in range(user1, user2) if i % 2 == 0]
Press enter or click to view image in full size
Getting even numbers within a range — Solution Code no. 3

2. PRIME NUMBERS

If a number is evenly divisible by 1, and the number itself, such numbers are called prime numbers. Our aim is to write a code that checks whether a given number is prime or not.

Prime and Composite numbers

Our program should have the following features.

  • It should take the user input to check whether the number is prime or not.
  • Main logic :- We need to write a for loop considering the numbers within the range from 2 till the inputted number. (Here the user inputted number must be exclusive of the range)
  • Then we need to write a conditional logic within that loop. We have to use the modulo operator here as well. If the ‘if statement’ doesn’t get triggered we need to break out of the loop and print the following statement.(“X is a prime number” — Here X means the user inputted number)
Press enter or click to view image in full size
Check whether a given number is prime or not

Now that we know how to write a code to check whether a number is prime or not, let’s now take a look at how to find the first n prime numbers.

  • First of all we need to ask the user about, how many prime numbers do they want. We can do this by taking user input.
  • We need to define an empty list called primes so that we can add the prime numbers into that list and later on print it.
  • After that we need to write a while loop with a for loop and if & else statements inside that.
Press enter or click to view image in full size
Getting the first n primes

3. TRIANGULAR NUMBERS

Get Chamika Jayasinghe’s stories in your inbox

Join Medium for free to get updates from this writer.

First of all let’s see what triangular numbers are. The triangular number sequence is the representation of the numbers in the form of triangle arranged in a series. Take a look at the following diagram for more.

Press enter or click to view image in full size
Triangular Numbers

For example if we want the 5th triangular number then we need to get the sum of all numbers from 1 to 5. (T5 = 1 + 2 + 3 + 4 + 5 = 15). In the same way if we need the 6th triangular number then we need to get the sum of numbers from 1 to 6.

Now that we have a good understanding about triangular numbers let’s see how we can write a program to get the first n triangular numbers. We can achieve this task by writing a code as follows.

Press enter or click to view image in full size
Get first n triangular numbers

4. LEAST COMMON MULTIPLE

Let’s assume that we were given 2 distinct numbers and we were asked to find the least common multiple (LCM) of these 2 numbers. Our aim is to write a program instead of manually doing the calculation. First, let’s see what LCM means. The LCM of either 2 or more numbers, is the smallest number which can be exactly divided by each of the given number.

Least Common Multiple (LCM)

Here’s how you can take 2 numbers from the user and find their LCM using a Python program.

Press enter or click to view image in full size
Finding the LCM of 2 numbers

Here’s how you can use the same exact code to find the LCM of 3 numbers.

number3 = int(input("Enter the third number: "))
if x % number1 == 0 and x % number2 == 0 and x % number3 == 0:
Press enter or click to view image in full size
Finding the LCM of 3 numbers

5. FIBONACCI NUMBERS

The Fibonacci numbers are the following sequence of numbers.

[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...]

As you can see the next number in the sequence is founded by adding up the two numbers before it. For example the number after 21 should be 34, since 21 + 13 = 34. Likewise the element after 34 should be 55, since 34 + 21 =55.

Press enter or click to view image in full size
Fibonacci Sequence

If you are curious about this sequence watch the video below.

Our task is to write a program which outputs the first n Fibonacci numbers. Here’s how you can do that.

Press enter or click to view image in full size
Getting the first n Fibonacci numbers (Sample Solution)

6. THE COLLATZ CONJECTURE

First of all let’s see what Collatz sequence is . Let’s take a positive integer, and denote it as “n”. If this integer is odd we will multiply it by 3 and then add up 1 to the result. (3n + 1). If the initial integer is even we will just divide it by 2. (n/2) Let’s now take a number and generate a sequence according to the couple of rules mentioned above.

Ex 1:- Let starting number be 13.

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

Ex 2 :- Let starting number be 17

17→ 52→ 26→ 13→ 40→ 20→ 10→ 5→ 16→ 8→ 4→ 2→ 1

Ex 3 :- Let starting number be 33

33→ 100→ 50→ 25→ 76→ 38→ 19→ 58→ 29→ 88→ 44→ 22→ 11→ 34→ 17→ 52→ 26→ 13→ 40→ 20→ 10→ 5→ 16→ 8→ 4→ 2→ 1

As you can see, according to the given examples above it is quite evident that the sequence will eventually always arrive at 1. This is what called “The Collatz Sequence”. However mathematicians are yet to figure out the reason why this happens.

Press enter or click to view image in full size
Collatz Sequence

Well, our task is to write a Python program for an user inputted positive integer displaying its Collatz sequence. Here is the code I wrote. Feel free to let me know if you know any other method as this is what I could come up with.

Press enter or click to view image in full size
Collatz Sequence — Python Code
Collatz sequence for number 74

We have come to the end of the article. I do hope that you learned something worthwhile from this. I’d highly appreciate it if you could comment down your suggestions as I am keen on improving my writing skills. I am so excited to write about another interesting topic “Object Oriented Programming (OOP) in Python” in my next article. So make sure to follow me on Medium to see more of quality content like this. Don’t forget to connect with me on Linkedin & Twitter as well.😃

Buy me a coffee here. — https://www.buymeacoffee.com/chamikajaya

--

--

Chamika Jayasinghe
Chamika Jayasinghe

Written by Chamika Jayasinghe

Computer Science Engineering Undergrad | DevOps Enthusiast | Quality over Quantity