Problem 1: Basic Arithmetic

Define two variables, a and b, with values of your choice. Compute the following operations and log each result to the console:

  1. Sum of a and b
  2. Difference of a and b
  3. Product of a and b
  4. Quotient of a divided by b

Console.log code

console.log(“Sum:”, a + b);
console.log(“Difference:”, a - b);
console.log(“Product:”, a * b);
console.log(“Quotient:”, a / b);

%%js

let a = 5;
let b = 10;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
<IPython.core.display.Javascript object>

Problem 2: Modulus Operator

Define two numbers, num1 and num2. Use the modulus operator % to find the remainder when num1 is divided by num2. Log the result to the console.

Instructions:

  1. Define a variable num1 (the dividend).
  2. Define a variable num2 (the divisor).
  3. Use the modulus operator num1 % num2 to calculate the remainder.
  4. Log the result to the console using console.log().

console.log code

console.log(“Remainder:”, YOURVARIABLE (CORRECT MATHEMATICAL OPERATION) YOUROTHERVARIABLE); // Output: 1

%%js

let num1 = 10;
let num2 = 5;

console.log("num1 % num2: " + (num1 % num2));
<IPython.core.display.Javascript object>

Problem 3: Increment and Decrement

Define a variable count and set it to an initial value. Increment and then decrement the variable in two different ways:

  1. Using the ++ and -- operators.
  2. Using the += and -= operators. Log the result after each operation.

    Instructions:

  3. Define a variable count and assign it an initial value (e.g., 10).
  4. Use ++ to increment the variable by 1 and log the result.
  5. Use -- to decrement the variable by 1 and log the result.
  6. Use += to increment the variable by a specific value and log the result.
  7. Use -= to decrement the variable by a specific value and log the result.
  8. Log result 3 times (Use these operations to increment/decremnt your number atleast 3 times)

    Example:

    ```javascript let count = 10; console.log(“Initial Count:”, count); // Output: 10 // Increment using ++ count++;

%%js

let count = 10;
console.log("Initial value: " + count);

// Increment using ++
count++;
console.log("After increment: " + count);

// Decrement using --
count--;
console.log("After decrement: " + count);

// Increment using +=
count += 5;
console.log("After addition: " + count);

// Decrement using -=
count -= 5;
console.log("After subtraction: " + count);

// Multiply using *=
count *= 2;
console.log("After multiplication: " + count);

// Divide using /=
count /= 2;
console.log("After division: " + count);

// Using Math.pow() for exponentiation
count = Math.pow(count, 2);
console.log("After exponentiation: " + count);

// Using Math.sqrt() for square root
count = Math.sqrt(count);
console.log("After square root: " + count);

// Using Math.random() to add a random number
count += Math.random();
console.log("After adding a random number: " + count);

// Using Math.floor() to round down
count = Math.floor(count);
console.log("After rounding down: " + count);

// For fun, let's implement Euler's Totient
function gcd(a, b) {
    if (a == 0) {
        return b;
    }

    return gcd(b % a, a);
}

function phi(n) {
    let result = 1;

    for (let i = 2; i < n; i++) {
        if (gcd(i, n) == 1) {
            result++;
        }
    }

    return result;
}

console.log("Euler's Totient for " + count + ": " + phi(count));

count = 256321;
console.log("Euler's Totient for " + count + ": " + phi(count));
<IPython.core.display.Javascript object>