Created
February 16, 2023 16:05
-
-
Save bannovGB/28be1fc10a054d9feace5207d897b1b5 to your computer and use it in GitHub Desktop.
Python_lesson05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Задача 26: Напишите программу, которая на вход принимает два числа A и B, | |
и возводит число А в целую степень B с помощью рекурсии. | |
A = 3; B = 5 -> 243 (3⁵) | |
A = 2; B = 3 -> 8''' | |
def recApowB(a, b): | |
if b == 0: | |
return 1 | |
return a * recApowB(a, b - 1) | |
a = int(input('Введите число: ')) | |
b = int(input('Введите степень: ')) | |
print(recApowB(a, b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Задача 28: Напишите рекурсивную функцию sum(a, b), | |
возвращающую сумму двух целых неотрицательных чисел. | |
Из всех арифметических операций допускаются только +1 и -1. | |
Также нельзя использовать циклы. | |
2 2 | |
4''' | |
def recsum(a, b): | |
if b == 0: | |
return a | |
return 1 + recsum(a, b - 1) | |
print(recsum(3, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment