Skip to the content.

3.3_ipynb_2_

Popcorn Hacks

%%js
// Popcorn Hack 1
function performOperations(num1, num2) {
    /*
    Perform basic arithmetic operations on two numbers.
    */
    const addition = num1 + num2;
    const subtraction = num1 - num2;
    const multiplication = num1 * num2;
    const division = num2 !== 0 ? num1 / num2 : "Cannot divide by zero";
    const modulo = num2 !== 0 ? num1 % num2 : "Cannot mod by zero";

    return {
        Addition: addition,
        Subtraction: subtraction,
        Multiplication: multiplication,
        Division: division,
        Modulo: modulo
    };
}

// Example usage
const result = performOperations(10, 5);
for (let operation in result) {
    console.log(`${operation}: ${result[operation]}`);
}
#Popcorn Hack 1
def perform_operations(num1, num2):
    """
    Perform basic arithmetic operations on two numbers.
    """
    addition = num1 + num2
    subtraction = num1 - num2
    multiplication = num1 * num2
    division = num1 / num2 if num2 != 0 else "Cannot divide by zero"
    modulo = num1 % num2 if num2 != 0 else "Cannot mod by zero"

    return {
        "Addition": addition,
        "Subtraction": subtraction,
        "Multiplication": multiplication,
        "Division": division,
        "Modulo": modulo
    }

# Example usage
result = perform_operations(10, 5)
for operation, value in result.items():
    print(f"{operation}: {value}")
#Popcorn hack 2
def fibonacci(n):
    """
    Return the nth Fibonacci number.
    """
    if n < 0:
        raise ValueError("Input should be a non-negative integer.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1

    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b

# Example usage
n = 8  # Change this to get different values in the Fibonacci sequence
result = fibonacci(n)
print(f"The {n}th Fibonacci number is: {result}")
%%js
// Popcorn hack 2
function fibonacci(n) {
    /*
    Return the nth Fibonacci number.
    */
    if (n < 0) {
        throw new Error("Input should be a non-negative integer.");
    } else if (n === 0) {
        return 0;
    } else if (n === 1) {
        return 1;
    }

    let a = 0, b = 1;
    for (let i = 2; i <= n; i++) {
        let temp = a;
        a = b;
        b = temp + b;
    }
    return b;
}

// Example usage
const n = 8;  // Change this to get different values in the Fibonacci sequence
const result = fibonacci(n);
console.log(`The ${n}th Fibonacci number is: ${result}`);

Homework Hacks

%%js
// Homework hack 1
// Recursive Method
function fibonacciRecursive(n) {
    if (n < 0) {
        throw new Error("Input should be a non-negative integer.");
    } else if (n === 0) {
        return 0;
    } else if (n === 1) {
        return 1;
    }
    return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}

// Iterative Method
function fibonacciIterative(n) {
    if (n < 0) {
        throw new Error("Input should be a non-negative integer.");
    } else if (n === 0) {
        return 0;
    } else if (n === 1) {
        return 1;
    }

    let a = 0, b = 1;
    for (let i = 2; i <= n; i++) {
        let temp = a;
        a = b;
        b = temp + b;
    }
    return b;
}

// Dynamic Programming with Memoization
const fibonacciMemoization = (function() {
    const memo = {};
    return function fib(n) {
        if (n in memo) {
            return memo[n];
        }
        if (n < 0) {
            throw new Error("Input should be a non-negative integer.");
        } else if (n === 0) {
            return 0;
        } else if (n === 1) {
            return 1;
        }
        memo[n] = fib(n - 1) + fib(n - 2);
        return memo[n];
    }
})();

// Binet's Formula
function fibonacciBinet(n) {
    if (n < 0) {
        throw new Error("Input should be a non-negative integer.");
    }
    const phi = (1 + Math.sqrt(5)) / 2;
    return Math.round((Math.pow(phi, n) - Math.pow(1 - phi, n)) / Math.sqrt(5));
}

// Example usage
const n = 8;  // Change this to get different values in the Fibonacci sequence

console.log(`Recursive: The ${n}th Fibonacci number is: ${fibonacciRecursive(n)}`);
console.log(`Iterative: The ${n}th Fibonacci number is: ${fibonacciIterative(n)}`);
console.log(`Memoization: The ${n}th Fibonacci number is: ${fibonacciMemoization(n)}`);
console.log(`Binet's Formula: The ${n}th Fibonacci number is: ${fibonacciBinet(n)}`);
#Popcorn hack 1
# Recursive Method
def fibonacci_recursive(n):
    if n < 0:
        raise ValueError("Input should be a non-negative integer.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

# Iterative Method
def fibonacci_iterative(n):
    if n < 0:
        raise ValueError("Input should be a non-negative integer.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1

    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b

# Dynamic Programming with Memoization
def fibonacci_memoization(n, memo={}):
    if n in memo:
        return memo[n]
    if n < 0:
        raise ValueError("Input should be a non-negative integer.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1

    memo[n] = fibonacci_memoization(n - 1, memo) + fibonacci_memoization(n - 2, memo)
    return memo[n]

# Binet's Formula
import math
def fibonacci_binet(n):
    if n < 0:
        raise ValueError("Input should be a non-negative integer.")
    phi = (1 + math.sqrt(5)) / 2
    return round((phi**n - (1 - phi)**n) / math.sqrt(5))

# Example usage
n = 8  # Change this to get different values in the Fibonacci sequence

print(f"Recursive: The {n}th Fibonacci number is: {fibonacci_recursive(n)}")
print(f"Iterative: The {n}th Fibonacci number is: {fibonacci_iterative(n)}")
print(f"Memoization: The {n}th Fibonacci number is: {fibonacci_memoization(n)}")
print(f"Binet's Formula: The {n}th Fibonacci number is: {fibonacci_binet(n)}")