#OK to post homework
#Joseph Koutsoutis, 03-16-2025, Assignment 14

read `C14.txt`:

#1

ra:=proc(K): rand(-K..K)():end:

RandomBell1 := proc(K) local A0, A1, B0, B1, V, i:
  A0:=C1QG()[4]:
  A1:=C1QG()[2]:
  B0:= expand(-(C1QG()[2]+C1QG()[4])/sqrt(2)):
  B1:= expand((C1QG()[2]-C1QG()[4])/sqrt(2)):

  V := [ra(K)+I*ra(K),ra(K)+ra(K)*I,ra(K)+I*ra(K),ra(K)+I*ra(K)]:
  V := expand(V / sqrt(add(abs(V[i])^2, i=1..4))):
  Bell(A0,A1,B0,B1,V):
end:


#2

RandomBell := proc(K, N) local i, total, num_g2, bell:
  total := 0:
  num_g2 := 0:
  for i from 1 to N do:
    bell := Re(RandomBell1(K)):
    total := simplify(total + bell):
    if evalf(bell) > 2 then:
      num_g2++:
    fi:
  od:
  evalf(total / N), num_g2:
end:

#RandomBell(10, 1000) outputted 0.03887227500, 5