Cho một hàm GCD [A, B] để tìm GCD [ước số chung lớn nhất] của hai số. Người ta cũng biết rằng GCD của ba yếu tố có thể được tìm thấy bởi GCD [A, GCD [B, C]], tương tự đối với bốn phần tử, nó có thể tìm thấy GCD bởi GCD [A, GCD [B, GCD [C, D]] ]. Cho một số nguyên dương n. Nhiệm vụ là in công thức để tìm GCD của số nguyên n bằng hàm gcd [] đã cho. & Nbsp;gcd[a, b] to find GCD [Greatest Common Divisor] of two number. It is also known that GCD of three elements can be found by gcd[a, gcd[b, c]], similarly for four element it can find the GCD by gcd[a, gcd[b, gcd[c, d]]]. Given a positive integer n. The task is to print the formula to find the GCD of n integer using given gcd[] function.
Examples: :
Đầu vào: n = 3Output: GCD [int, gcd [int, int]]n = 3
Output : gcd[int, gcd[int, int]]Đầu vào: n = 5Output: GCD [int, gcd [int, gcd [int, gcd [int, int]]]]]n = 5
Output : gcd[int, gcd[int, gcd[int, gcd[int, int]]]]
Cách tiếp cận: Ý tưởng là sử dụng đệ quy để in lệnh dòng đơn. Bây giờ, để viết một hàm đệ quy, giả sử respursivefun [n], chuỗi yêu cầu bao gồm GCD [int, + respursivefun [n - 1] +]. Điều này có nghĩa là RecursiveFun [n] sẽ trả về một chuỗi chứa một cuộc gọi đến chính nó và để đánh giá giá trị đó, chức năng đệ quy sẽ bắt đầu lại cho n - 1. Điều này sẽ lần lượt trả lại một chuỗi khác với một cuộc gọi đến n - 1 và vì vậy cho đến khi n == 1 và hàm đệ quy thay vào đó trả về chuỗi trực tuyến Int Int.The idea is to use recursion to print the single line command. Now, to write a recursive function, say recursiveFun[n], the required string is composed of gcd[int, + recursiveFun[n – 1] + ]. This means that the recursiveFun[n] should return a string that contains a call to itself and in
order to evaluate that value, the recursive function will begin again for n – 1. This will, in turn, return another string with a call to n – 1 and so until n == 1 and the recursive function instead returns the string “int”.
Below is implementation of the above approach:
C++
#include
using
namespace
std;
string recursiveFun[
int
n]
{
____10 #include
1
#include
2#include
3 #include
4#include
5
#include
3 #include
8 #include
9using
0#include
5
using
2
int
using
4
{
int
using
8
namespace
0
#include
3 namespace
3
using
2
Java
namespace
5 namespace
6
{
namespace
8 namespace
9int
n]
{
____10 std;
5std;
6std;
7
#include
2#include
3 #include
4#include
5
#include
3 #include
8 #include
9using
0#include
5
string recursiveFun[
6string recursiveFun[
7std;
6string recursiveFun[
9using
0#include
5
using
2
int
using
4
{
int
using
8
n]
4
using
2
using
2
Python3
#include
3 namespace
3
Java
namespace
5 namespace
6
namespace
8 namespace
9int
n]
____10 std;
5std;
6std;
7
#include
3 #include
8 string recursiveFun[
5
#include
10#include
11
C#
int
3 namespace
8 int
5 int
6
namespace
5 namespace
6
{
namespace
8 namespace
9int
n]
{
____10 #include
1
#include
2#include
3 #include
4#include
5
#include
3 #include
8 #include
9using
0#include
5
string recursiveFun[
6#include
34using
0#include
5
using
2
int
using
4
{
int
using
8
#include
47
using
2
using
2
#include
3 namespace
3
#include
50
Java
{
namespace
5 namespace
6
namespace
8 namespace
9int
n]
____10 std;
5std;
6std;
7
using
2
#include
3 #include
8 string recursiveFun[
5
#include
2#include
75
#include
76
Output:
gcd[int, gcd[int, gcd[int, gcd[int, int]]]]
int
3 namespace
8 int
5 int
6 O[N], where N is the given number.
Auxiliary Space: O[N] for recursive stack space.