Skip to content

Instantly share code, notes, and snippets.

@nmsderp
Last active August 16, 2024 03:00
Show Gist options
  • Select an option

  • Save nmsderp/34bd4242dd2369d0a6c22c6312ed1165 to your computer and use it in GitHub Desktop.

Select an option

Save nmsderp/34bd4242dd2369d0a6c22c6312ed1165 to your computer and use it in GitHub Desktop.
# THIS IS FOR THE RASPBERRY PI PICO W WITH MICROPYTHON
import time
import math
import machine
def calculate_pi(n):
pi = 0
for k in range(n):
pi += (1 / (16 ** k)) * (
(4 / (8 * k + 1)) -
(2 / (8 * k + 4)) -
(1 / (8 * k + 5)) -
(1 / (8 * k + 6))
)
return pi
def complex_math_calculations():
factorial_result = math.factorial(100)
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
fibonacci_result = fibonacci(100)
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
prime_result = [i for i in range(1000) if is_prime(i)]
return factorial_result, fibonacci_result, prime_result
def read_temperature():
sensor = machine.ADC(4)
conversion_factor = 3.3 / (65535)
reading = sensor.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706) / 0.001721
return temperature
def stress_test_iteration():
print("Calculating digits of Pi...")
pi_digits = calculate_pi(10)
print(f"Pi: {pi_digits}")
print("Performing complex mathematical calculations...")
factorial_result, fibonacci_result, prime_result = complex_math_calculations()
print(f"Factorial Result: {str(factorial_result)[:50]}...")
print(f"Fibonacci Result: {fibonacci_result}")
print(f"Prime Numbers: {prime_result[:50]}")
print("Reading temperature...")
temperature = read_temperature()
print(f"Temperature: {temperature:.2f} C")
def stress_test(duration_minutes=30):
start_time = time.ticks_ms()
end_time = start_time + duration_minutes * 60 * 1000
iteration_count = 0
while time.ticks_ms() < end_time:
iteration_start_time = time.ticks_ms()
stress_test_iteration()
iteration_end_time = time.ticks_ms()
iteration_duration = time.ticks_diff(iteration_end_time, iteration_start_time)
iteration_count += 1
elapsed_time = time.ticks_diff(iteration_end_time, start_time) // 1000
print(f"Iteration {iteration_count} completed in {iteration_duration} ms")
print(f"Elapsed time: {elapsed_time} seconds")
time.sleep(1)
print("Stress test completed")
stress_test(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment