UVa 412 Pi: Simple GCD

Source: Internet
Author: User
Tags gcd integer time limit

412-pi

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=353

Professor Robert J. Matthews of the Applied Mathematics and Computer science Department at the University of Aston in B Irmingham, England has recently described howthe positions of stars across the night Skymay is used to deduce a surprising Ly accurate value of

. This result followed to the application of certain theorems in number theory.

Here, we don ' Thave the night sky and but can use the same theoretical basis to form a estimate for:

Given any pair of whole numbers chosen-a large, random collection of numbers, the probability that twonumbers e no common factor than one (1) is

For example, using the small collection of Numbers:2, 3, 4, 5, 6; There are pairs can be formed: (2,3), (2,4), etc. Six of the 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:

In the 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 formed" have nocommon other factor one (1), and use The method illustrated above to obtain a estimate for

. The estimate for each data set.

Input

The input consists of a series of data sets.

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

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45362.htm

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

Each integer to the input stream has its digit as the "the" "the" the "the" the "the" input line.

The set size designator, N, 'll is zero to indicate the end of data.

Output

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

For some data sets, the It may is impossible to estimate a value for. This occurs when there are no pairs without common. In this cases, emit the Single-line message:

No estimate for this data set.

Exactly, starting with the "N", as the "the" and "the", "the", "the", "the", ""

Sample Input

5
2
3
4
5
6
2
km
0

Sample output (Your output for the float/real, using Your chosen language, May is default-formatted differently).

3.162278
No estimate for this data set.

Complete code:

/*0.109s*/
    
#include <cstdio>  
#include <cmath>  
    
int a[55];  
    
int gcd (int a, int b)  
{return  
    b gcd (b, a% B): A;  
}  
    
int main ()  
{  
    int n, Count, I, J;  
    while (scanf ("%d", &n), N)  
    {  
        count = 0;  
        for (i = 0; i < n; ++i)  
            scanf ("%d", &a[i]);  
        for (i = 0; i < n-1. ++i) for  
            (j = i + 1; j < n; ++j)  
                if (gcd (a[i), a[j]) = = 1) ++count;  
        if (count) printf ("%.6f\n", sqrt (3.0 * n * (n-1)/count);  
        Else puts ("No estimate for this data set.");  
    }  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.