1+ def find_sum (n ):
2+ if n == 1 :
3+ return 1
4+ return n + find_sum (n - 1 )
5+
6+ def fib (n ):
7+ # 0,1,1,2,3,5,8 <-- fibonacci numbers
8+ # --------------
9+ # 0,1,2,3,4,5,6 <-- index
10+ if n == 0 or n == 1 :
11+ return n
12+ return fib (n - 1 ) + fib (n - 2 )
13+
14+ # print(find_sum(5))
15+ # print(fib(10))
16+
17+ # 1. Write a Python program to calculate the sum of a list of numbers using recursion.
18+ # Click me to see the sample solution
19+
20+ def find_list_sum (list ):
21+ if len (list ) == 0 :
22+ return 0
23+ if len (list ) == 1 :
24+ return list [0 ]
25+ return list [len (list )- 1 ] + find_list_sum (list [0 :len (list )- 1 ])
26+
27+ # print(find_list_sum([4,2,3,8,13,10,10]))
28+
29+ # 2. Write a Python program to convert an integer to a string in any base using recursion .
30+ # Click me to see the sample solution
31+
32+ #IDK what that means
33+
34+ # 3. Write a Python program to sum recursion lists using recursion.
35+ # Test Data: [1, 2, [3,4], [5,6]]
36+ # Expected Result: 21
37+ # Click me to see the sample solution
38+
39+ def find_list_of_lists_sum (list ):
40+ for item in list :
41+ if type (item ) == int :
42+ return item
43+ elif type (item ) == list :
44+ find_list_of_lists_sum (item )
45+ return
46+
47+ # print(find_list_of_lists_sum([1, 2, [3,4], [5,6]]))
48+
49+ # 4. Write a Python program to get the factorial of a non-negative integer using recursion.
50+ # Click me to see the sample solution
51+
52+ def find_factorial (int ):
53+ if int == 0 or int == 1 :
54+ return 1
55+ return int * find_factorial (int - 1 )
56+
57+ # print(find_factorial(6))
58+
59+ # 5. Write a Python program to solve the Fibonacci sequence using recursion.
60+ # Click me to see the sample solution
61+
62+ def fib2 (n ):
63+ # 0,1,1,2,3,5,8 <-- fibonacci numbers
64+ # --------------
65+ # 0,1,2,3,4,5,6 <-- index
66+ if n == 0 or n == 1 :
67+ return n
68+ return fib2 (n - 1 ) + fib2 (n - 2 )
69+
70+ # print(fib(7))
71+
72+ # 6. Write a Python program to get the sum of a non-negative integer using recursion.
73+ # Test Data:
74+ # sumDigits(345) -> 12
75+ # sumDigits(45) -> 9
76+ # Click me to see the sample solution
77+
78+ def sumDigits (number ):
79+ str_num = str (number )
80+ digits = [int (num ) for num in str_num ]
81+
82+ if len (digits ) == 0 :
83+ return 0
84+ if len (digits ) == 1 :
85+ return int (digits [0 ])
86+
87+ updated_list = digits [:- 1 ]
88+ str_digits = '' .join (str (digit ) for digit in updated_list )
89+ new_number = int (str_digits )
90+ return int (digits [len (digits )- 1 ]) + sumDigits (new_number )
91+
92+ # print(sumDigits(345))
93+
94+
95+ # 7. Write a Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0) using recursion .
96+ # Test Data:
97+ # sum_series(6) -> 12
98+ # sum_series(10) -> 30
99+ # Click me to see the sample solution
100+
101+ def sum_minus_twos (num ):
102+ if num == 0 or num == 1 or num == 2 :
103+ return num
104+ return num + sum_minus_twos (num - 2 )
105+
106+ # print(sum_minus_twos(15))
107+
108+
109+ # 8. Write a Python program to calculate the sum of harmonic series upto n terms.
110+ # Note: The harmonic sum is the sum of reciprocals of the positive integers.
111+ # Example :
112+ # harmonic series
113+ # Click me to see the sample solution
114+
115+ def harmonic_sum (num ):
116+ if num == 0 :
117+ return "n/a"
118+ if num == 1 :
119+ return 1
120+ return (1 / num ) + harmonic_sum (num - 1 )
121+
122+ # print(harmonic_sum(5))
123+
124+ # 9. Write a Python program to calculate the geometric sum up to 'n' terms.
125+ # Note: In mathematics, a geometric series is a series with a constant ratio between successive terms.
126+
127+ # Ex: geometric_sum(3) = 1+ 1/2 + 1/4
128+
129+ def geometric_sum (num ):
130+ if num == 0 :
131+ return 1
132+ return 1 / (pow (2 ,num )) + geometric_sum (num - 1 )
133+
134+ # print(geometric_sum(4))
135+
136+ # Click me to see the sample solution
137+
138+ # 10. Write a Python program to calculate the value of 'a' to the power of 'b' using recursion.
139+ # Test Data :
140+ # (power(3,4) -> 81
141+ # Click me to see the sample solution
142+
143+ def calc_exponent (a ,b ):
144+ if b == 1 :
145+ return a
146+ return a * calc_exponent (a ,b - 1 )
147+
148+ # print(calc_exponent(4,3))
149+
150+ # 11. Write a Python program to find the greatest common divisor (GCD) of two integers using recursion.
151+ # Click me to see the sample solution
152+
153+ def Recurgcd (a , b ):
154+ # Determine the lower and higher values between 'a' and 'b'
155+ low = min (a , b )
156+ high = max (a , b )
157+
158+ # Check if the lower value is 0 (base case for GCD calculation)
159+ if low == 0 :
160+ # If the lower value is 0, return the higher value (GCD is the non-zero value)
161+ return high
162+ # Check if the lower value is 1 (base case for GCD calculation)
163+ elif low == 1 :
164+ # If the lower value is 1, return 1 (GCD of any number with 1 is 1)
165+ return 1
166+ else :
167+ # If neither base case is met, recursively call the Recurgcd function
168+ # with the lower value and the remainder of the higher value divided by the lower value
169+ return Recurgcd (low , high % low )
170+
171+ # Print the result of calling the Recurgcd function with the input values 12 and 14
172+ print (Recurgcd (24 , 64 ))
173+
174+
175+ def find_GCF (a ,b ):
176+ if a % b == 0 : #if b is divisible by a
177+ return b
178+ if b % a == 0 : #if a is divisible by b
179+ return a
180+
181+ factors_a = []
182+ for i in range (1 ,a + 1 ):
183+ if a % i == 0 :
184+ factors_a .append (i )
185+
186+ factors_b = []
187+ for i in range (1 ,b + 1 ):
188+ if b % i == 0 :
189+ factors_b .append (i )
190+
191+ common_factors = []
192+
193+ if len (factors_a ) < len (factors_b ): #use the shorter list to save run time
194+ for factor in factors_a :
195+ if factor in factors_b :
196+ common_factors .append (factor )
197+ else :
198+ for factor in factors_b :
199+ if factor in factors_a :
200+ common_factors .append (factor )
201+
202+ return max (common_factors )
203+
204+ print (find_GCF (24 ,64 ))
0 commit comments