Statistics

Problem Statement for "Archimedes"

Problem Statement

The Greek mathematician Archimedes of Syracuse (287-212 B.C.) was most likely the first in finding a way to systematically (iteratively) approximate the value of Pi, the ratio between the perimeter and the diameter of a circle.

He inscribed and circumscribed regular polygons in/around a circle and calculated their perimeter. These are lower (inscribed polygon) and upper (circumscribed polygon) bounds for the perimeter of the circle, and therefore can be used to calculate lower and upper bounds for Pi. He then found a formula for directly calculating the perimeter of inscribed and circumscribed polygons with 2*n sides only using the values previously calculated for polygons with n sides.

You now have a slightly simpler task, considering only inscribed polygons. You are given an int numSides, the number of sides of a regular polygon that is inscribed in a circle. Return a double, the approximated value for Pi when the perimeter of that polygon is used as an approximation for the perimeter of the circle.

[Image showing a circle with an 8-sided regular polygon inscribed]

Definition

Class:
Archimedes
Method:
approximatePi
Parameters:
int
Returns:
double
Method signature:
double approximatePi(int numSides)
(be sure your method is public)

Notes

  • Perimeter of a circle: 2 * PI * radius
  • Perimeter of a n-sided regular polygon: n * sidelength
  • Reminder how doubles are evaluated:If your result is within 1e-9 of the expected result, your solution will be evaluated as correct.If your result is between (1+1e-9) * expected and (1-1e-9) * expected, it will be evaluated as correct.

Constraints

  • numSides will be between 3 and 100000, inclusive.

Examples

  1. 3

    Returns: 2.598076211353316

  2. 8

    Returns: 3.0614674589207183

  3. 17280

    Returns: 3.1415926362832276

  4. 4

    Returns: 2.82842712474619

  5. 5

    Returns: 2.938926261462366

  6. 6

    Returns: 2.9999999999999996

  7. 7

    Returns: 3.037186173822907

  8. 8

    Returns: 3.0614674589207183

  9. 99999

    Returns: 3.1415926530730114

  10. 100000

    Returns: 3.1415926530730216

  11. 48

    Returns: 3.1393502030468667

  12. 11

    Returns: 3.0990581252557265

  13. 83

    Returns: 3.1408425675666343

  14. 80

    Returns: 3.140785260725489

  15. 68

    Returns: 3.140475187910292

  16. 349

    Returns: 3.1415502262546897

  17. 308

    Returns: 3.141538178911329

  18. 558

    Returns: 3.141576056601735

  19. 700

    Returns: 3.141582107247802

  20. 129

    Returns: 3.141282121798652

  21. 3523

    Returns: 3.141592237225897

  22. 1162

    Returns: 3.141588826347538

  23. 1540

    Returns: 3.141590474591773

  24. 9361

    Returns: 3.1415925946167045

  25. 7638

    Returns: 3.1415925650091037

  26. 57303

    Returns: 3.1415926520160147

  27. 15275

    Returns: 3.141592631441721

  28. 80110

    Returns: 3.1415926527845537

  29. 21281

    Returns: 3.14159264217904

  30. 19060

    Returns: 3.141592639364783

  31. 100000

    Returns: 3.1415926530730216

  32. 100000

    Returns: 3.1415926530730216


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: