Syntax The power function in Python can be easily computed by importing the math module as follows: You can alternatively use the ** syntax as follows: Modular Exponentiation The power function can also be used as: This is called modular exponentiation which is useful in some computer science applications especially public key cryptography, for example:
Introduction Any computer algorithm requires two control flow structures: iteration and selection. Our topic today is mainly about iteration using the Python for loop. The goal of this post is to achieve three objectives: Provide a beginner introduction to the for loop control structure Include example code snippets for copy and paste lovers Cover some
Insertion Sort Welcome to part 8 on loop invariants. Please refer to the first part (introduction) here. Today, we are going to discuss insertion sort algorithm. Let us get started. Problem definition Given an array (A) of (n) numbers. Assume the array starts at position (1). Develop an algorithm to sort the array in increasing
Bubble Sort Hello! This is part 7 in a series of short articles on loop invariants. You can refer to the first part (introduction) here. Today, we are going to discuss bubble sort algorithm in the context of loop invariants. Problem definition Given an array (A) of (n) numbers. Develop an algorithm to sort the
Linear Search Hello! This is part 6 in a series of short articles on loop invariants. You can refer to the first part (introduction) here. Today, we are going to develop an algorithm for linear search using loop invariants. Let us get started. Problem definition Given an array (A) containing (n) numbers and given some
Integer Division This is the 5th part in a series of short articles on loop invariants. You can refer to the first part (introduction) here. Our example for today is a little bit harder than the previous examples. We are going to develop an algorithm to do integer division. Let us see how can we
Factorial Hello everyone! This is the 4th part in a series of short articles on loop invariants. You can refer to the first part (introduction) here. We are going to continue talking about loop invariants. Let us see how to find an appropriate loop invariant to calculate the factorial of a positive integer. Problem definition
Power Function Hello! This is the third part in a series of short articles on loop invariants. You can refer to the first part (introduction) here. In this article we will develop an algorithm to calculate the nth power of a positive integer. Problem definition Given two positive integers (x) and (n), develop an algorithm
Array sum Hello! This is the second part in a series of short articles on loop invariants. You can refer to the first part (introduction) here. In this article we will develop an algorithm to calculate the sum of all elements in an array. It is very easy to solve this problem using intuition but
Introduction Today, I am going to talk about a confusing concept in algorithm design. I think you have already guessed from the title of this article. Yes, let us talk about the puzzling topic of Loop Invariants. I personally believe it is a mathematical concept more than it is a computer science concept simply because