Decomposition of rational numbers-number theory

Source: Internet
Author: User
Tags greatest common divisor

Title Description Description

The rational number in any [0,1] p/q (p, q are natural numbers) must be decomposed into 1/r1+1/r2+1/r3+...+1/rk, and R1<r2<r3<...<rk. Of course such decomposition is not unique, such as 5/6=1/2+1/3=1/2+1/5+1/8+1/120, the second item in the first decomposition is larger than the second in the second decomposition, so we can define the first decomposition to be larger than the second decomposition type.

The program requires finding the maximum decomposition of the p/q.

input/output format input/output Input Format:
Keyboard input P, q,1≤p≤q≤50
output Format:
From small to large in the output of each denominator in the decomposition, a line output a number input and Output sample sample Input/output sample Test point # #

Input Sample:

5 6
Sample output:

2 3

Ideas:

Assuming that 1/r is the true fraction of the largest molecule that can be decomposed from the p/q 1, then 1/r≤p/q<1/(r-1) ①

And because P/q-1/r= (PXR-Q)/(QXR) ②

According to ①, PX (r-1) <q, so pxr-p

After the input p, q to simplify the score.

The code is as follows:

1#include <stdio.h>2 intgcdintAintb) //Beg Greatest common divisor 3 {4     intr=a%b;5      while(r>0)6     {7A=b;8b=R;9r=a%b;Ten     } One     returnb; A } - intMain () - { the     intp,q; -     intcm//current Greatest common divisor -     intR; -scanf"%d%d",&p,&q); +      while(p>0) -     { +cm=gcd (p,q); A         if(cm>0) at         { -             /*===========*///Reduction of fractions -p=p/cm; -q=q/cm; -             /*===========*/ -         } in         if((q%p) >0)//if not decomposed into the final 1/rk -         { tor=q/p+1; +         } -         Else the         { *r=q/p; $         }Panax Notoginsengprintf"%d\n", R); -P=p*r-q;//lose theq=q*R; +     } A     return 0; the}

Decomposition of rational numbers-number theory

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.