Skip to content

Instantly share code, notes, and snippets.

@chrismatthieu
Created October 13, 2025 22:09
Show Gist options
  • Save chrismatthieu/e06cdd5c6c3787d7e68e2c6977d81e9e to your computer and use it in GitHub Desktop.
Save chrismatthieu/e06cdd5c6c3787d7e68e2c6977d81e9e to your computer and use it in GitHub Desktop.
corebrum demo
import time
import json
def factorial(n):
"""Compute factorial of n recursively."""
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
def factorial_iterative(n):
"""Compute factorial of n iteratively (more efficient for large numbers)."""
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
result = 1
for i in range(1, n + 1):
result *= i
return result
# Get input number from the inputs dictionary
number = inputs.get("number", 10)
# Validate input
if not isinstance(number, (int, float)) or number < 0:
raise ValueError(f"Invalid input: {number}. Must be a non-negative number.")
# Convert to integer if it's a float
number = int(number)
# Start timing
start_time = time.time()
# Compute factorial (using iterative method for better performance)
factorial_result = factorial_iterative(number)
# Calculate computation time
computation_time_ms = int((time.time() - start_time) * 1000)
# Create result dictionary
result = {
"factorial": factorial_result,
"input_number": number,
"computation_time_ms": computation_time_ms,
"worker_id": worker_id,
"method": "iterative",
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}
# The result variable will be automatically printed as JSON by the Corebrum wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment