GCD function for C

前端 未结 4 1861
礼貌的吻别
礼貌的吻别 2021-02-15 18:04

Q 1. Problem 5 (evenly divisible) I tried the brute force method but it took time, so I referred few sites and found this code:

#include
int gcd(i         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 18:43

    To your second question: The GCD function uses Euclid's Algorithm. It computes A mod B, then swaps A and B with an XOR swap. A more readable version might look like this:

    int gcd(int a, int b)
    {
        int temp;
        while (b != 0)
        {
            temp = a % b;
    
            a = b;
            b = temp;
        }
        return a;
    }
    

提交回复
热议问题