Mathematical Expressions

Algorithms:


What is it?

An algorithm is a finite set of instructions designed to perform a specific task. It’s it a set of procedures that you follow step-by-step to achieve a desired outcome. For example, when you bake cookies, you follow a series of specific steps to get the final product.

Real World Example:

  • Getting ready in the morning
    • First wake up and brush
    • Wear shirt/pants/socks etc.
    • Eat breakfast

Coding Example (in JavaScript):

%%javascript
const array = [3, 8, 12, 6, 10, 2]; //defining a list of numbers in an array

function checkForN(arr, n) {
    for(let i = 0; i < arr.length; i++) { //iterating through the array
        if (n === arr[i]) { //checking if the number exists in the array
            console.log(`${true}, ${n} exists at index ${i}`); //printing the result
            return;
        }
    }

    console.log(`${false}, ${n} does not exist in the given array.`); //printing the result if it doesn't exist
}

checkForN(array, 10); //calling the function with the array and checking for the number 10

<IPython.core.display.Javascript object>


Key Components of an Algorithm


Sequencing

What is it?:

The order in which the algoritm is executed.

Real World Example:

  • Creating a PBJ sandwich
    • FIRST grab the bread
    • SECOND apply peanut butter
    • THIRD apply jelly
    • FOURTH join both sides
    • FIFTH cut the sandwich in half

Selection:

What is it?:

Selection involves making decisions within an algorithm based on certain conditions. This is very useful as at a certain point in an algorithm, you may need to make a choice, such as deciding whether to continue after a condition has been met.

Real World Example:

If it’s cold outside, then wear a jacket. Else, go without a jacket.


Iteration/Repetition

What is it?:

Iteration, also known as repetition, is the process of repeating a set of steps until a condition is met. This helps when you need to repeat an action multiple times, sometimes until a condition is met.

Real World Example:

  • Step 1: Brush your teeth.
  • Step 2: Check if they are clean.
    • If Yes: Stop brushing.
    • If No: Repeat starting from Step 1.
%%javascript
// Function to make a cup of tea
function makeTea() {
    // Sequencing: The steps must happen in the order of the code.
    console.log("Step 1: Boil water.");
    console.log("Step 2: Add tea bag to cup.");
    console.log("Step 3: Pour boiling water into the cup.");
    
    // Selection: Making a decision based on a condition.
    let hasSugar = prompt("Do you want sugar in your tea? (yes or no)");
    
    if (hasSugar.toLowerCase() === "yes") {
        console.log("Step 4: Add sugar.");
    } else {
        console.log("Step 4: No sugar added.");
    }
    
    console.log("Step 5: Stir the tea.");
    
    // Iteration: Repeating the stirring process until the tea is mixed well.
    let isMixed = false;
    let stirCount = 0;
    
    while (!isMixed) {
        console.log("Stirring the tea...");
        stirCount++;
        
        if (stirCount >= 3) {
            isMixed = true;
        }
    }
    
    console.log("Tea is ready to drink!");
}

makeTea();
<IPython.core.display.Javascript object>

Output with sugar:

Step 1: Boil water.
Step 2: Add tea bag to cup.
Step 3: Pour boiling water into the cup.
Step 4: Add sugar.
Step 5: Stir the tea.
Stirring the tea...
Stirring the tea...
Stirring the tea...
Tea is ready to drink!

Output without sugar:

Step 1: Boil water.
Step 2: Add tea bag to cup.
Step 3: Pour boiling water into the cup.
Step 4: No sugar added.
Step 5: Stir the tea.
Stirring the tea...
Stirring the tea...
Stirring the tea...
Tea is ready to drink!

Ways to Write an Algorithm


Flowchart:

What is it?:

A flowchart is a visual representation of an algorithm. It uses shapes, arrows, and text to depict the sequence of steps and decisions.

How It Works:

  • Ovals represent the start and end.
  • Rectangles represent instructions or actions.
  • Diamonds represent decisions, often leading to different paths based on Yes/No or True/False conditions.

Example:


Pseudo Code:

What is it?:

Pseudo code is a simplified way to describe an algorithm using plain English. It outlines the logic without worrying about specific programming syntax.

How It Works:

  • Words are used to explain what each section does.
  • The focus is on the structure and flow rather than the exact code.

Example

Start
Put on clothes
If it’s cold outside
    Wear a jacket
Else
    Go without a jacket
Eat breakfast
End

PART 2: Mathematical Operations

  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Division: a / b
  • Modulus (remainder of a/b): a % b
  • Math in order of operations
%%javascript

let a = 10;
let b = 5;

let addition = a + b;
console.log(addition); // Output: 15


let subtraction = a - b;
console.log(subtraction); // Output: 5

let multiplication = a * b;
console.log(multiplication); // Output: 50

let division = a / b;
console.log(division); // Output: 2

let c = 10;
let d = 3;

let modulus = 10 % 3;
console.log(modulus); // Output: 1

let e = 7;
let f = 2;

let result = (a + b) * (c / d) - e % f;
console.log(result); // Output: 49
// The operations inside the parentheses are performed first,
// then multiplication, division, modulus, and subtraction in that order.
<IPython.core.display.Javascript object>