Convert real numbers into scores

Source: Internet
Author: User

Convert a real number to a fractional real number that contains rational and irrational numbers. Any rational number can be expressed as P/Q (p, q is an integer, Q! = 0). If the denominator of a score cannot exceed a certain value, a score cannot be accurately expressed for a general rational number or irrational number. We will mainly discuss how to find a pair of scores P1/Q1 and P2/Q2 so that Q1 and Q2 are less than the given value n, p1/Q1 and P2/Q2 are as close as possible to a given real number .. For ease of description, we use the C ++ language format to define the problem: function interface void search (Int & P1, Int & Q1, Int & p2, Int & Q2, int N, double F) function: Find a pair of non-approx P1/Q1 and P2/q2, so that the denominator of these two scores is not greater than N, and P1/q1 <= F <= P2/q2, and these two scores are as close as possible to F. Before solving this problem, we should first introduce some preparation knowledge. Definition 1: The simplest score (also known as an approximate or non-approximate score ). If the maximum common divisor of p and q is 1, the score P/Q is the simplest score. Note: the numerator and denominator of the score mentioned below are non-negative integers. Definition 2: True score. If p and q are positive integers, 0 <p/q <1, we say P/Q is the true score Theorem 1: score a/B, c/D is the simplest true score (or 0/1 or 1/1) and a/B <C/D, there is 1) number (A + C)/(B + D) is the simplest score, 2) a/B <(A + C)/(B + d) <B/d. here we will only prove the second conclusion. We define R1 = A/B, R2 = C/d, then (A + C)/(B + d)-A/B = (AB + CB-AB-AD) /(BB + bd) = (CB-AD)/(BB + bd) = (R2 * bd-r1bd)/(BB + bd) = (r2-r1) * bd/(BB + bd)> 0 solid (A + C)/(B + d)> A/B. Likewise, it can be proved (A + C)/(B + d) <C/D Definition 3: a given natural number N of the farrex series, sort the non-approximate real scores with the denominator less than or equal to N in ascending order, add 0/1 before the first score, and add 1/1 after the last score. This sequence is called the n-level farelite series, in FN format. For example, F5 is 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5. structure of the faray series: the construction of the faray series can adopt a two-way method, that is, if a/B, c/d (a/B <C/D) it is two elements in the n-level array, and B + D <= N, you can insert a score (A + B) in the center of a/B, c/d) /(C + D ). The following describes the detailed process by taking the five-level array of mines as an example. Step 1: Prepare two numbers of 0/1, 1/1 as the first element and the last element of the entire array of mines 0/1, 1/1step2: Insert a number of 1/2 in the middle of the two numbers to 0/1, 1/2, 1/1 Step 3: insert one number into each pair of adjacent two numbers to 0/1, 1/3, 1/2,
2/3, 1/1step4: insert one number into each pair of adjacent two numbers to 0/1, 1/4, 1/3,
2/5, 1/2, 3/5, 2/3,
3/4, 1/1step5: Between 0/1 and 1/4, and between 3/4 and 1/1, one number can still be inserted, so that the number of inserted denominator is not greater than 5 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1 so far, this sequence contains the simplest true scores of all denominator not greater than 5, and each score is arranged in ascending order. The nature of the faray series: 1. except for the Level 1 Referer series, all the Referer series have an odd number of elements. The element that occupies the center must be 1/2. 2. when N tends to be positive and infinite, the number of elements in the n-level record series tends to 3/(π * π) * N2 ≈ 0.30396355 * n2.3. N-level record series, if two adjacent elements are A/B and C/D (a/B <C/D), the difference between the two numbers is 1/Bd, the minimum value of the difference is 1/(n * (n-1), and the maximum value is 1/n. The first element (0/1) in the faray series is followed by the last element (1/1) the difference between the element and the precursor is obtained to the maximum value, while the difference between the element 1/2 in the center is obtained from the difference between the precursor and the successor element.
1/(n * 2 ). the real-number score method can be expressed as 0/1 for the rational number 0. For the rational number x <0, the total number can be expressed as-(P/q), where P> 0, q> 0; for all positive rational numbers greater than or equal to 1, the total number can be expressed as n + p/Q (N, P, Q is a non-negative integer, Q! = 1, P <q), expressed as (NQ + p)/Q in the form of scores. Therefore, converting a positive rational number less than 1 to a score is the basic problem of converting a real number to a score. Because the irrational number cannot be accurately expressed by a score, we can use two scores a/B and c/d to approximate this real number, make the irrational number F> = A/B and F <= C/D. A/B is called the lower bound of the real number f, and C/D is called the upper bound of the real number F, finding this lower bound and the upper bound is actually to find two adjacent elements in an N-level array. The following is an algorithm that converts positive numbers to scores smaller than 1. Step 1: Set the lower bound of the real number F to A/B = 0/1, and the upper bound to C/D = 1/1. Step 2: calculate the number of lower bound and Upper Bound P/q = (a + C)/(B + d) Step 3: If q> N (the denominator is greater than the specified value ), computing aborted. If ABS (P/Q-F) is less than the specified value =, the calculation is aborted. If P/q> F, set lower bound a/B to P/Q. If P/q <F, set upper bound C/D to P/Q step4, repeat Step 2-3. When the calculation ends, a/B is the lower bound of the real number, and C/D is the upper bound of the real number.

The upper and lower bounds of the method are wide. We can use the following method to directly obtain a more precise upper and lower bounds.

Case 1: F <1/2, calculate 1/F and get the integer c down. Then, the lower bound and upper bound of the real number F are 1/(C + 1) and 1/C.

Case 2: F> 1/2, calculate 1/(1-F) and get the integer c down to get the integer c. Then, the lower and upper bounds of the real number F are (c-1) /C and C/(C + 1 ).

Error analysis, according to the nature of the faray Series 3, we know that the adjacent two elements in the n-level faray series can represent a range [A/B, C/D], the first element Q/B is the lower bound of the interval, and the last element C/D is the upper bound of the interval. The width of the interval H = C/D-A/B, 1/n <= H <1/(n * N-1 ). If you are lucky, a real number falls in a range of 1/N (n-1, the lower bound or upper bound of this interval and the difference between this real number cannot exceed ABS (1/(n * (n-1 ))). If luck is poor, a real number is exactly less than the 2nd elements or the last element in the faray sequence. Then the lower and upper bounds of this element and the difference between this real number cannot exceed 1/N. The following is an example code. This code can calculate the scores of SQRT (2), SQRT (3), SQRT (5), and irrational π and E.
# Include "math. H "# include" stdlib. H "# include" stdio. H "typedef struct _ frac {unsigned long numerator; unsigned long denominator;} frac; double getvalue (frac f) {double T = (double) F. numerator/(double) F. denominator; return t;} // F must be a positive number void searchfrac (frac * plow, frac * phigh, int N, double F) {frac low; frac high; frac mid; int C; int K = (INT) F; F-= (double) K; // obtain the fractional part of F if (F <= 0.5) {c = (INT) (1.0/F); low. numerator = 1; low. denominator = C + 1; high. numerator = 1; high. denominator = C;} else {c = (INT) (1.0/(1.0-f); low. numerator = C-1; low. denominator = C; high. numerator = C; high. denominator = C + 1;} mid. numerator = low. numerator + high. numerator; mid. denominator = low. denominator + high. denominator; while (MID. denominator <n & FABS (F-getvalue (MID)> 1e-15) {# ifdef _ debuuplintf ("F = %. 16f \ tmid = % u/% u = %. 16f \ n ", F, mid. numerator, mid. denominator, getvalue (MID); # endifif (getvalue (MID)> F) {high = mid;} else {LOW = mid;} mid. numerator = low. numerator + high. numerator; mid. denominator = low. denominator + high. denominator;} If (k> 0) {low. numerator + = K * low. denominator; high. numerator + = K * High. denominator;} * phigh = high; * plow = low;} int main (INT argc, char * argv []) {frac low, high; Double T; double f [] = {3.1415926535897932384626433832795, 2.7182818284590452353602874713527,}; For (INT I = 0; I <sizeof (f)/sizeof (double); I ++) {if (I <4) t = SQRT (F [I]); elset = f [I]; searchfrac (& low,& high, 10000, t ); printf ("\ NF = %. 16f, low = % u/% u = %. 16f high = % u/% u = %. 16f \ n ", T, low. numerator, low. denominator, getvalue (low), high. numerator, high. denominator, getvalue (high);} return 0 ;}

Copyright liangbch@263.net, reprint or reference please indicate the source. References: 36th programming competitions 1st questions-programmer Forum: http://www.programfan.com/club/post-185318.html2. farey sequence -- from Wolfram mathworld: http://mathworld.wolfram.com/FareySequence.html

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.