Guest User

Untitled

a guest
Oct 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. /*
  2. * Calculate PI using the sequence method with the given precision.
  3. */
  4.  
  5. function calculatePi(precision) {
  6. let estimate = 0
  7. for(let i = 0; i < precision; i++) {
  8. const divisor = i * 2 + 1
  9. const isEven = i % 2 == 0
  10.  
  11. if (isEven) {
  12. estimate = estimate + 4/divisor
  13. } else {
  14. estimate = estimate - 4/divisor
  15. }
  16. }
  17. return estimate
  18. }
  19.  
  20. function calculatePiCryptic(p) {
  21. let e = 0
  22. for(let i = 0; i < p; i++) {
  23. e = i % 2 == 0 ? e + 4 / (i * 2 + 1) : e - 4 / (i * 2 + 1)
  24. }
  25. return e
  26. }
  27.  
  28. const pi = calculatePi(10000)
  29. const piCryptic = calculatePiCryptic(10000)
  30.  
  31. console.log(pi)
  32. console.log(piCryptic)
Add Comment
Please, Sign In to add comment