注册 登录
编程论坛 VFP论坛

求两个数的最大公约数

头脑风暴 发布于 2018-09-23 16:13, 2168 次点击
请问用循环如何求两个整数的最大公约数?
6 回复
#2
头脑风暴2018-09-23 16:13
顶~~~~~
#3
sdta2018-09-23 16:29
学生不思进取可不行啊
#4
红星二锅头2018-09-23 17:33
* 求最小公倍数
m=3
N=4  && m n是被求的两个数
F=m*N
Do While .T.
    If Mod(Max(m,N),Min(m,N))=0
        r=Min(m,N)  && r值最后就是最大公约数
        Exit
    Else
        r=Mod(Max(m,N),Min(m,N))
        m=Min(m,N)
        N=r
        r=Min(m,N)
    Endif
Enddo
T=F/r  && T就是两者得最小公倍数
? T
#5
红星二锅头2018-09-23 17:45
LCD / GCD 是基本算法,尽量自己做,家长赚钱不容易的,,,

Local x
Local Y
Local g

x = 122   && Read Numerator
Y = 990   && Read Denominator
*!*    g = 1
*!* Display the given fraction
Messagebox("Original Fraction: " + Transform(x) + " / " + Transform(Y))
*!* Calculate GCD
g = gcd(x, Y)
*!* Reduce the fraction
X = X / g
Y = Y / g
*!* Display result
Messagebox("Reduced Fraction: " + Transform(x) + " / " + Transform(Y))



*!***************************************
*!* Name: GCD - Reduce a Fraction
*!* Description:The proper way to reduce a fraction to simplest terms. (For example, 12/32 reduces to 3/8) Divide both the numerator and the denominator by the

greatest common divisor of the numerator and denominator. Most programmers are familiar with this GCD algorithm. If this is new to you, take a moment to understand

what this does and how it can be used. It comes in handy!
*!* By: korejwa
*!*
*!*This code is copyrighted and has&& limited warranties.Please see http://www.

details.&&**************************************

Function gcd(X, Y )
    gcd = 0
    If X < 0
        X = -x  && Euclid's Algorithm
    Endif
    If Y < 0
        Y = -Y  && Returns Greatest Common Divisor of x and y
    Endif
    If X = 0 Or Y = 0
        ? "Math Error in GCD"  &&If both x and y are zero ...
    Endif
    gcd = Y
    Do While X > 0
        gcd = X
        x = Y % X
        Y = gcd
    Enddo
    Return gcd
Endfunc
#6
红星二锅头2018-09-23 17:53
GCD = 最大公约数
LCM = 最小公倍数
#7
红星二锅头2018-09-23 18:02
中秋节是团圆的节日,楼主可以分别用:递归、丢潘图、欧拉等算法做,这是对父母最大的孝心,楼主,你行,你最棒,,,
1