Pi

本文介绍了一种通过分析随机整数对的最大公约数来估算圆周率π的方法,并提供了一个简单的C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Time Limit: 1 Second      Memory Limit: 32768 KB

Professor Robert A. J. Matthews of the Applied Mathematics and Computer Science Department at the University of Aston in Birmingham, England has recently described how the positions of stars across the night sky may be used to deduce a surprisingly accurate value of Pi. This result followed from the application of certain theorems in number theory.

Here, we don't have the night sky, but can use the same theoretical basis to form an estimate for Pi:

Given any pair of whole numbers chosen from a large, random collection of numbers, the probability that the two numbers have no common factor other than one (1) is 6/Pi^2

For example, using the small collection of numbers: 2, 3, 4, 5, 6; there are 10 pairs that can be formed: (2,3), (2,4), etc. Six of the 10 pairs: (2,3), (2,5), (3,4), (3,5), (4,5) and (5,6) have no common factor other than one. Using the ratio of the counts as the probability we have:

6/Pi^2 = 6/10

Pi = 3.162

In this problem, you'll receive a series of data sets. Each data set contains a set of pseudo-random positive integers. For each data set, find the portion of the pairs which may be formed that have no common factor other than one (1), and use the method illustrated above to obtain an estimate for Pi. Report this estimate for each data set.


Input

The input consists of a series of data sets.

The first line of each data set contains a positive integer value, N, greater than one (1) and less than 50.

There is one positive integer per line for the next N lines that constitute the set for which the pairs are to be examined. These integers are each greater than 0 and less than 32768.

Each integer of the input stream has its first digit as the first character on the input line.

The set size designator, N, will be zero to indicate the end of data.


Output

A line with a single real value is to be emitted for each input data set encountered. This value is the estimate for Pi for the data set. An output format like the sample below should be used. Answers must be rounded to six digits after the decimal point.

For some data sets, it may be impossible to estimate a value for Pi. This occurs when there are no pairs without common factors. In these cases, emit the single-line message:

 

No estimate for this data set.

 

exactly, starting with the first character, "N", as the first character on the line.


Sample Input

5
2
3
4
5
6
2
13
39
0


Sample Output

3.162278
No estimate for this data set.

 

 

问题比较简单:

 

05-24
### 关于 `math.pi` 的编程相关内容 在编程领域,尤其是涉及数学计算时,`math.pi` 是一个非常重要的常量。它代表了圆周率 π 的值,通常用于各种几何和三角函数的计算。 #### Python 中的 `math.pi` 在 Python 编程语言中,可以通过导入 `math` 模块来访问 `math.pi` 常量。该常量提供了高精度的 π 值,其数值为 3.141592653589793[^1]。以下是使用 `math.pi` 进行基本计算的一个例子: ```python import math print("Value of pi:", math.pi) print("Circumference of circle with radius 1:", 2 * math.pi * 1) print("Area of circle with radius 1:", math.pi * (1 ** 2)) ``` 这段代码展示了如何利用 `math.pi` 来计算半径为 1 的圆的周长和面积。 #### Java 中的 `Math.PI` 除了 Python,在其他编程语言如 Java 中也有类似的定义。Java 提供了一个名为 `Math.PI` 的静态常量,同样表示圆周率 π 的近似值[^3]。下面是一段简单的 Java 示例代码: ```java public class Main { public static void main(String[] args) { System.out.println("Value of PI: " + Math.PI); double circumference = 2 * Math.PI * 1; double area = Math.PI * Math.pow(1, 2); System.out.println("Circumference of circle with radius 1: " + circumference); System.out.println("Area of circle with radius 1: " + area); } } ``` 此程序实现了与前面提到的 Python 版本相同的功能——基于单位半径的圆的周长和面积计算。 #### 数学运算中的应用实例 通过结合 `math.pi` 和其他数学操作符或函数可以完成更复杂的科学计算任务。例如求解球体体积或者椭圆形轨迹长度等问题都离不开这个基础参数的支持[^2]。 另外需要注意的是不同平台下实现可能会存在细微差异比如某些特定场景下的精度损失情况等[^4][^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值