![]() ![]() ![]() ![]() Thus it's O(log N) complexity where N is the higher of the two numbers. The answer will be 1 but if this is the nth and (n+1)th Fibonacci number it will take n iterations to unravel. The complexity of this algorithm: well the worst case scenario is actually 2 consecutive Fibonacci numbers. ![]() int gcd( int x, int y )įor a simple example, gcd of 9 x takes the greater value so: printf ('GCD of d and d is: d', a, b, gcd) In the last step, we print the GCD of two numbers which is stored in the gcd variable using printf () function. For simplistic terms you can use them if they are there and prompt if not.įor an implementation of the function, it can be a simple free-function. Your main can look at its argument list and possibly support having the parameters in there. You can create a function that does the same thing.Ĭout << "Greatest Common Divisor: " << gcd(num1,num2) << endl įirstly, as this is meant to be for code reviews, I will immediately point out that you are putting everything into main and you really should put the algorithm into its own function. This is how I would implement Euclid's algorithm. Have your program change the input to positive. Negative number again? You need to use a while loop here, or just It seems like you don't want users inputting negative numbers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |