Aren’t you bored coding in the same traditional logic over these years. It’s time to bring some differences. Well, be it any programming language. When we are asked to do some sort of basic programming exercises like sorting, searching, prime number checking, etc. we are all driven towards the same boat, which is the same coding logic. Now, we are in the past 2020, so let’s also take an initiative to change our thinking styles when it comes to coding. It is because, only if we think differently, we can code differently.
Lets dive deep into those wonderful coding tips.
Check whether a given number is Odd or Even
Of course, what a simple question. We can easily check for even or odd using the modulus operator. You want to know how? Just see the below Python code snippet –
Python3
a = 4 if (a % 2 = = 0 ): print ( "even" ) else : print ( "odd" ) |
Output:
even
Here, the modulus operation is used to determine the remainder obtained. All even numbers are divisible by 2. That means, upon dividing an even number say 4 with 2, we will obtain the remainder as 0. So applying modulus operation between the two numbers will leave the answer as 0. This basic principle is always applied to programming whenever we are asked to check for odd and even numbers.
Can you think it differently? We all have taught our brains the logic that dividing a number by 2 determines whether it’s odd or even. And that line of code is followed traditionally by all those who code. So let’s start excavating an evergreen, but the unused method .
Python3
a = 4 if (a & 1 ): print ( "odd" ) else : print ( "even" ) |
Output:
even
This was done using bitwise AND operation. Cool enough!
Bitwise operators are operating on integers bit by bit. This is done by converting the integers into binary and then performing logical AND operation on each of the corresponding bits.
Note: For more information, refer to Python Bitwise Operators
Checking whether a given number is prime or not?
Those numbers which are divisible by 1 and that number only are called prime numbers. Examples for prime numbers are 2, 3, 5, 7, etc. which do not have any other factors(excluding themselves and 1). We all have been taught about the below logic for checking the prime numbers.
Python3
a = 10 if a> 1 : for i in range ( 2 , (a / / 2 ) + 1 ): if a % i = = 0 : print (a, "is not prime " ) break else : print (a, "is prime " ) else : print (a, "is not prime " ) |
Output:
10 is not prime
But we can optimize it even more. And it will look like:
Python3
a = 10 i = 2 if a> 1 : while (i * i<a): if a % i = = 0 : print (a, "is not prime " ) break else : print (a, "is prime " ) i + = 1 else : print (a, "is not prime " ) |
Output:
10 is not prime
Both the codes are correct and output the same result. But the second code is more optimized because to check for factors, it is sufficient to loop till the square root of the given number instead of checking until its half.
In the above code, we are checking for the factors of a given number. And after each iteration, we have to increment the value of the loop variable ‘i’. It functions till i^2 less than the given number.
Multiply a given number k by 2^k and display the output
This can be coded easily in any language by basic mathematical steps. So the steps are:
- Assign a value, say 5 to a variable ‘k’
- Then, we have to store the value of 2^5 in another variable j.
- Multiply the contents of both k and j.
- Display the output .(Here 160 will be the resultant output)
This can be coded in a few lines and the code is as follows:
Python3
k = 5 j = 2 * * k p = k * j print (p) |
Output:
160
But there is another method which is much simpler and efficient. That is by using shift-operator. Left shift operation on the variable k, k times will result in the same function as displayed above. So the optimized code will become:
Python3
k = 5 print (k<<k) |
Output:
160
Both these will result in the same output.
Divide a given number by 2^k and display the output
The dividing function also works similar to the multiply function. Only the operator changes. So the steps for dividing a number by 2^k are listed below:
- Assign a value, say 5 to a variable ‘k’
- Then, we have to store the value of 2^5 in another variable j.
- Divide the contents of both k and j.
- Display the output .(Here 160 will be the resultant output)
The Python 3 code for basic division is as follows:
Python3
k = 5 j = 2 * * k p = k / / j print (p) |
Output:
0
We can use ‘//’ for integer division and ‘/’ for floor division as per our requirement.And the optimized code using right shift operation will look like :
Python3
k = 5 print (k>>k) |
Output:
0
Both the codes will result in the output ‘0’(integer division).
Note: For more information, refer to Python Bitwise Operators.
All these operations already existed since history. But we forgot to practice using them. For our changing future, let’s adopt these small changes and become a part of it.