星海's Blog

老头初学编程
合理的有理数实现 代码

最大公约数与Fibnacci的递归和循环实现

星海 posted @ 2010年10月18日 01:51 in C代码 , 1646 阅读

 

/*
 * ==========================================================================
 *
 *       Filename:  euclid.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2010年10月17日 16时27分21秒
 *
 * ==========================================================================
 */
#include <stdio.h>
int k = 1;
int euclid(int a, int b)
{
    if (a % b == 0)
        return b;
    else
        ++k;
        return euclid(b, a % b);
}

int fib(int n)
{
    if (n == 0 || n == 1)
        return 1;
    else
        return fib(n-1)+fib(n-2);
}
int main(void)
{
    printf ("最大公约数为%d\n",euclid(1111186,256));
    printf ("lame~定理 256大于%d",fib(k));

    /* 
     *如果Euclid算法需要k步来计算两个数的GCD,那么这两个数之中较小的一个必然
     *大于等于Fibonacci数列的第K项
     */
    return 0;
}


 

#include <stdio.h>
int euclid(int a,int b)
{
    int temp;
    while (a % b != 0) {
        temp = a % b;
        a = b;
        b = temp;
    }
    return b;
}

int fib(int n)
{
    int x = 2;
    int result;
    int first = 1;
    int second = 1;
    if (n == 1 || n == 0)
        result = 1;
    while (x <= n) {
        ++x;
        result = first + second;
        first = second;
        second = result;
    }
    return result;
}
Avatar_small
DolaA.M 说:
2010年10月18日 04:31

这是Python ???
怎么跑到 Python学习 这个分类来了...

Avatar_small
星海 说:
2010年10月19日 06:48

晕,弄错分类了

已经改过来啦 哈哈~

Avatar_small
Kflayca 说:
2010年11月14日 04:27

int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}

int fibnacci(unsigned int n)
{
return n <= 1 ? 1 : fibnacci(n - 1) + fibnacci(n - 2);
}

Avatar_small
星海 说:
2010年11月14日 21:41

很简洁啊!!!!!!!

unsigned int n,然后n <=1,有意思。。。。。

Avatar_small
CBSE 10th Question P 说:
2021年10月26日 15:17

You must be aware that, beginning in 2022, vocational subjects examinations will begin in February and main subjects examinations will begin in the first week of March. CBSE 10th Question Paper 2022 As a result, it is critical that you finish your syllabus by mid-November and then commit your whole attention to your revision work in order to achieve higher outcomes.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter