From the "simplest proper fraction of the number of" talk

      The simplest is called a proper fraction of a molecular fraction is less than the denominator and numerator and denominator no common factor.
      2010 Hubei primary Mathematical Olympiad (sixth grade group) there is such a questions: the 2010 is the simplest denominator proper fraction of how many?
      This study Primary School Mathematics Olympiad questions that students and contains a collection of inclusion and exclusion mastery of knowledge.
      Since 2010 = 2 * 3 * 5 * 67 (decomposition quality factor), the denominator 2010 therefore proper fractions the simplest molecule must be not less than 2010 divided by 3, 5 or 67.
      Children calculation to solve this problem as follows:
      In 2010 a total of 1 to 2010 in number,
      can have 2 divisible 2010 ÷ 2 = 1005
      can have a number divisible by 3 ÷ 3 = 670 is 2010
      . 5 can have a divisible 2010 ÷ 5 = 402
      can have a divisible 67 2010 ÷ 67 = 30
      can be simultaneously 2 and 3 has divisible 2010 ÷ (2 × 3) = 335
      can have 2 and 5 are simultaneously divisible 2010 ÷ (2 × 5) = 201
      can be simultaneously 2 and 67 divisible has 2010 ÷ (2 × 67) = 15
      can simultaneously have 3 and 5 divisible 2010 ÷ (3 × 5) = 134
      can simultaneously be 3 and 67 divisible there are a number of 2010 ÷ (3 × 67) = 10
      can be simultaneously divisible 5 and 67 with a 2010 ÷ (5 × 67) = 6
      can be simultaneously 2, 3 and 5 there is divisible 2010 ÷ (2 × 3 × 5) = 67
      can have 2, 3 and 67 are simultaneously divisible 2010 ÷ (2 × 3 × 67 ) = 5
      While being able to have 2010 ÷ (2 × 5 × 67 ) = 2,5 divisible 3 and 67
      can be simultaneously divisible 3,5 and 67 there 2010 ÷ (3 × 5 × 67 ) = 2
      can be simultaneously 2 3, 5 and 67 there is divisible 2010 ÷ (2 × 3 × 5 × 67) = 1
      Thus, in can be 1 to 3 or 2 or 5 or 67 with a divisible 2010
        (1005 + 670 + 402 + 30 ) - (335 + 201 + 15 + 134 + 10 + 6) + (67 + 5 + 3 + 2) -1
       = 77-1 = 1482 + 2107-701
      Accordingly, 1 to 2010 neither divisible by 2, and is not divisible by 3, can not be divisible by 5, 67 can not be divisible has 2010-1482 = 528.
      That is to 2010 as the denominator of the simplest proper fraction has 528.

      We can see that the above calculation is more complicated, requiring carefully.
      After learning through programming, you can write a simple loop program to solve this problem.
      Cnt with a variable to hold the number of proper fractions the simplest, the initial value is 0.
      For each of 1 ~ num in 2010, judgment, which is a cyclic, written
             for (num = 1; num < = 2010; num ++)
      determination method in the loop body is: neither divisible by 2 if num, also is not divisible by 3, can not be divisible by 5, can not be divisible by 67, the count. Write
       IF (! NUM = 2%. 3% NUM = 0 && && 0. 5% NUM = NUM% 67 = 0 && 0!!!)
              CNT ++;
      Finally, the output cnt. A simple program, get answers to your questions.
 source prepared as follows:
       #include <stdio.h>
       int main ()
       {
             int CNT, NUM;
             CNT = 0;
             for (NUM =. 1; NUM <= 2010; NUM ++)
                  ! IF (NUM% 2 = 0 && num% 3! = 0 && num % 5! = 0 && num% 67!

             printf("%d\n",cnt);
             return 0;
       }

      It should be noted that, when the true problem of the competition is: all to 2010 as the denominator of the simplest proper fractions and how much?

      Look, as college students like children, like you can do out of it?

      Of course, you learn programming, the above procedure is simple rewrite it, you can quickly get an answer. Why kids like it as hard thinking and operations.

#include <stdio.h>
int main()
{
     int num;
     double sum;
     sum=0;
     for (num=1; num<=2010;num++)
         if (num%2!=0 && num%3!=0 && num%5!=0 && num %67!=0)
               sum+=1.0*num/2010;
     printf("%lf\n",sum);
     return 0;
}

      After the program runs, the output 264.000000. That is all to 2010 as the denominator of the most simple and proper fraction is 264.

      Kids are not as hard as the program count. 1/7 + 2010/2010 + 11/2010 + ... + 2099/2010 = 264.

      There are smart children's children, 1/2010 is the most simple proper fraction, then 2099/2010 must be the simplest proper fraction.

      i / 2010 is the most simple proper fraction, then (2010-i) / 2010 must be the simplest proper fraction.

      1/2010 + 2099/2010=1         i/2010 +(2010-i)/2010=1。

      2010 for the children to know the simplest denominator proper fraction has 528, and therefore their is 528/2 = 264.

      Since 2010 after decomposition of the quality factor, factor 3, 5 and 67 have four for study inclusion and exclusion collection contains a slightly larger amount of calculation but can be done, it can be a good contest questions.

     On the basis of the questions on this road, we see such a problem.

[Example 1] simplest proper fraction.

      Enter any positive integer n, where n is the denominator in order to find the most proper fraction Jane How many?

     (1) 1 programming ideas.

      N as a denominator input, an exhaustive molecular i (1≤i≤n-1). Thus, the first program may be written in the following cycle:

         for (I =. 1; I <= n--. 1; I ++)
        {
               For each fraction i / n, to detect the presence or absence of the common factor. The decision whether the count result of the detection;
        }
      needed for each fraction i / n In the above loop, detecting the presence or absence of the common factor. If the molecule i n the presence of the denominator common factor greater than k 1, indicating i / n is not the most simple proper fraction, not counting. How to detect it?
      Well as factor k in the range of [2, i], thus setting u k exhaustive cycle in [2, i], when the condition is satisfied
            i% k == 0 && n% k == 0
      Description numerator and denominator exists common factor k, t = after 1 marker exit.
      Before exhaustion of the factor k is circulated, may set a flag t = 0. After exiting the loop factor to be exhaustive, if t = 1, the numerator and denominator present description common factor; if remains of the original t = 0, no common factor of the numerator and denominator described, the number of statistics.

      (2) a source.

#include <stdio.h>
int main ()
{
      int n-, I, K, T, CNT;
      the while (! Scanf ( "% D", n-&) = 0 && n-)
      {
           CNT = 0;
           for (I =. 1 ; i <= n-1; i ++) // exhaustive molecule
           {
                T = 0;
                for (K = 2; K <= I; K ++) // exhaustive factor
                    if (i% k == 0 && n% k 0 ==)
                    {
                         T =. 1;
                         BREAK; // common factor of the numerator and denominator have discarded
                    }
               IF (T == 0)
                     CNT ++; // count the number of the most simple proper fraction
          }
          the printf ( "% D \ n-", CNT );
      }
      return 0;
}

      Will be submitted to the above source POJ 2407 "Relatives" , determined to Time Limit Exceeded. 2407 POJ meaning of the title is: enter a positive integer N, is less than or equal to find N ([1, N]) , and the number of positive integers with N mutually quality (including 1). This is entirely consistent with the proper fraction for the most simple means.

      The above process is simple and direct source, but for larger values ​​of N, it will timeout. Therefore, we should find a quick method for finding. In number theory, Euler function on a good solution to this problem.

      In number theory, for positive integer n, it is a function of the Euler number of the number of positive integers less than or equal to n with n coprime. This function Euler named its first investigator, usually abbreviated as φ function. For example, φ (8) = 4, and 8 as 1,3,5,7 are coprime.

      In general, the positive integer N is provided after the decomposition of the quality factor, N = P1 ^ q1 * P2 ^ q2 * ... * Pn ^ qn.

      Then φ (N) = N * (1-1 / P1) * (1-1 / P2) * ... * (1-1 / Pn).

      For example, 10 = 2 * 5 φ (10) = 10 × (1-1 / 2) × (1-1 / 5) = 4; 4 this number is 1, 3, 7, 9.

         30 = 2 * 3 * 5 φ (30) = 30 × (1-1 / 2) × (1-1 / 3) × (1-1 / 5) = 8; this number is 7, 11 8 , 13, 17, 19, 23, 29.

 

Guess you like

Origin www.cnblogs.com/cs-whut/p/11515994.html