Skip to content

Instantly share code, notes, and snippets.

@ncaq
Last active December 2, 2016 13:59
Show Gist options
  • Select an option

  • Save ncaq/d85b9b9133d8ab4ea683965510867a62 to your computer and use it in GitHub Desktop.

Select an option

Save ncaq/d85b9b9133d8ab4ea683965510867a62 to your computer and use it in GitHub Desktop.
2015-07に記述.prologによる格子点の個数からの円周率の近似値の計算
piExp(R, Pi) :- dimension(circle, R, Cs), dimension(box, R, Bs), Pi is Cs / Bs.
dimension( box, R, Ps) :- Ps is R * R.
dimension(circle, R, Ps) :- dimensionRec(circle, 0, R, Psr), Ps is Psr * 4.
dimensionRec( _, X, R, Ps) :- X > R, Ps is 0.
dimensionRec(circle, X, R, Ps) :- X =< R, line(circle, X, R, P), dimensionRec(circle, X + 1, R, NP), Ps is P + NP.
line(circle, X, R, P) :- hypot(X, Y, R), P is Y.
hypot(X, Y, R) :- nonvar(X), nonvar(Y), sqrt(X * X + Y * Y, R).
hypot(X, Y, R) :- nonvar(X), nonvar(R), sqrt(R * R - X * X, Y).
hypot(X, Y, R) :- nonvar(Y), nonvar(R), sqrt(R * R - Y * Y, X).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment