Solving Math Using Python
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.
Here is what you are going to learn.
- Odd & Even numbers
- Prime numbers
- Triangular Numbers
- Finding The Least Common Multiple
- Fibonacci Sequence
- The Collatz Conjecture
- 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.
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.
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.
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)))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]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.
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)
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.
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.
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.
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.
Here’s how you can take 2 numbers from the user and find their LCM using a Python program.
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:
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.
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.
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.
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.
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









