Skip to content

Instantly share code, notes, and snippets.

@tkoz0
Created March 29, 2021 11:31
Show Gist options
  • Save tkoz0/6b1bdcd61448e9ed6195d7f8663fc60d to your computer and use it in GitHub Desktop.
Save tkoz0/6b1bdcd61448e9ed6195d7f8663fc60d to your computer and use it in GitHub Desktop.
Program to get empirical evidence for the solution to the problem in assclass s2 episode 12
# the problem is in Ansatsu Kyoushitsu (Assassination Classroom) season 2 episode 12
# the answer is 1/2 * a^3
from math import sqrt
from random import random
# corner points for a square and cube (+/- 1 in each dimension)
SQP = [(1,1),(1,-1),(-1,1),(-1,-1)]
CBP = [(1,1,1),(1,1,-1),(1,-1,1),(1,-1,-1),(-1,1,1),(-1,1,-1),(-1,-1,1),(-1,-1,-1)]
# distance between 2 points in euclidean space
def dist(a,b):
assert len(a) == len(b)
return sqrt(sum((a[i]-b[i])**2 for i in range(len(a))))
total = 1000000 # trials to run
count = 0
for _ in range(total):
# generate point in a cube with corner points at +/- 1
x,y,z = 2*random()-1,2*random()-1,2*random()-1
P = (x,y,z)
# if point is closer to the origin than any of the corners
if dist((0,0,0),P) < min(dist(P,p) for p in CBP): count += 1
# ratio of random points closer to the origin, should be close to 0.5
print(count/total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment