Lesson 3.3 Popcorn Hacks and Homework

Popcorn Hack #1

Javascript Version

%%js
function arithmeticOperations(a, b) {
    let addition = a + b;
    let subtraction = a - b;
    let multiplication = a * b;
    let division = b !== 0 ? a / b : 'Undefined (Division by zero)';
    let modulus = b !== 0 ? a % b : 'Undefined (Modulo by zero)';
    
    return {
        Addition: addition,
        Subtraction: subtraction,
        Multiplication: multiplication,
        Division: division,
        Modulus: modulus
    };
}

let result = arithmeticOperations(10, 2);
console.log(result);
<IPython.core.display.Javascript object>

Python Version

def arithmetic_operations(a, b):
    addition = a + b
    subtraction = a - b
    multiplication = a * b
    division = a / b if b != 0 else 'Undefined (Division by zero)'
    modulus = a % b if b != 0 else 'Undefined (Modulo by zero)'
    
    return {
        'Addition': addition,
        'Subtraction': subtraction,
        'Multiplication': multiplication,
        'Division': division,
        'Modulus': modulus
    }

result = arithmetic_operations(10, 2)
print(result)
{'Addition': 12, 'Subtraction': 8, 'Multiplication': 20, 'Division': 5.0, 'Modulus': 0}

Popcorn Hack #2

Javascript Version

%%js
function fibonacci(n) {
    if (n <= 0) {
        return "Input should be a positive integer.";
    } else if (n === 1) {
        return 0;
    } else if (n === 2) {
        return 1;
    } else {
        let a = 0, b = 1;
        for (let i = 2; i < n; i++) {
            let next = a + b;
            a = b;
            b = next;
        }
        return b;
    }
}

let n = 7;
let nthFibonacci = fibonacci(n);
console.log(`The ${n}th Fibonacci number is: ${nthFibonacci}`);
<IPython.core.display.Javascript object>

Python Version

def fibonacci(n):
    if n <= 0:
        return "Input should be a positive integer."
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        a, b = 0, 1
        for _ in range(2, n):
            a, b = b, a + b
        return b

n = 7
nth_fibonacci = fibonacci(n)
print(f"The {n}th Fibonacci number is: {nth_fibonacci}")
The 7th Fibonacci number is: 8

Homework Lesson 3.3

Javascript Version

%%js
function matrixMultiply(A, B) {
    return [
        [A[0][0] * B[0][0] + A[0][1] * B[1][0], A[0][0] * B[0][1] + A[0][1] * B[1][1]],
        [A[1][0] * B[0][0] + A[1][1] * B[1][0], A[1][0] * B[0][1] + A[1][1] * B[1][1]]
    ];
}

function matrixPower(M, power) {
    if (power < 0) throw new Error("Power must be a non-negative integer.");

    let result = [[1, 0], [0, 1]]; // Identity matrix
    while (power > 0) {
        if (power % 2 === 1) {
            result = matrixMultiply(result, M);
        }
        M = matrixMultiply(M, M);
        power = Math.floor(power / 2);
    }
    return result;
}

function fibonacciMatrix(n) {
    if (n < 0) throw new Error("Fibonacci number is not defined for negative integers.");
    if (n === 0) return 0;
    if (n === 1) return 1;

    const F = [[1, 1], [1, 0

<IPython.core.display.Javascript object>

Python Version

import logging
import sys

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')

def matrix_multiply(A, B):
    return [[A[0][0] * B[0][0] + A[0][1] * B[1][0],
             A[0][0] * B[0][1] + A[0][1] * B[1][1]],
            [A[1][0] * B[0][0] + A[1][1] * B[1][0],
             A[1][0] * B[0][1] + A[1][1] * B[1][1]]]

def matrix_power(M, power):
    if power < 0:
        raise ValueError("Power must be a non-negative integer.")
    
    result = [[1, 0],
              [0, 1]]
    
    while power > 0:
        if power % 2 == 1:
            result = matrix_multiply(result, M)
        M = matrix_multiply(M, M)
        power //= 2
    
    return result

def fibonacci_matrix(n):
    if not isinstance(n, int):
        raise TypeError("Input must be an integer.")
    if n < 0:
        raise ValueError("Fibonacci number is not defined for negative integers.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    
    F = [[1, 1],
         [1, 0]]
    
    result = matrix_power(F, n-1)
    
    logging.info(f"Matrix raised to power {n-1}:\n{result}")
    
    return result[0][0]

def fibonacci_dp(n):
    if n < 0:
        raise ValueError("Fibonacci number is not defined for negative integers.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1

    fib = [0] * (n + 1)
    fib[0] = 0
    fib[1] = 1

    for i in range(2, n + 1):
        fib[i] = fib[i - 1] + fib[i - 2]
        logging.debug(f"Fibonacci[{i}] = {fib[i]}")

    return fib[n]

def fibonacci_recursive(n):
    if n < 0:
        raise ValueError("Fibonacci number is not defined for negative integers.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    
    logging.debug(f"Calculating Fibonacci({n}) recursively.")
    return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

def validate_input(user_input):
    try:
        value = int(user_input)
        if value < 0:
            raise ValueError
        return value
    except ValueError:
        raise ValueError("Please enter a valid non-negative integer.")

def main():
    try:
        user_input = input("Enter the position of the Fibonacci number you want to calculate: ")
        n = validate_input(user_input)

        print("\nChoose the algorithm:")
        print("1. Matrix Exponentiation")
        print("2. Dynamic Programming")
        print("3. Recursion")
        choice = input("Enter your choice (1/2/3): ")

        if choice == '1':
            fib_n = fibonacci_matrix(n)
        elif choice == '2':
            fib_n = fibonacci_dp(n)
        elif choice == '3':
            fib_n = fibonacci_recursive(n)
        else:
            raise ValueError("Invalid choice. Please select 1, 2, or 3.")

        print(f"Fibonacci number at position {n} is: {fib_n}")
    except ValueError as ve:
        logging.error(ve)
    except Exception as e:
        logging.error(f"An unexpected error occurred: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()

Choose the algorithm:
1. Matrix Exponentiation
2. Dynamic Programming
3. Recursion


INFO:Matrix raised to power 3:
[[3, 2], [2, 1]]


Fibonacci number at position 4 is: 3