-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion.py
128 lines (89 loc) · 3.23 KB
/
recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# ############################################################
# DAY 1
# Recursion Algorithm
# just an example how it works
# ############################################################
# ############################################################
# ############################################################
def fun1():
fun2()
print('This is first function')
def fun2():
fun3()
print('this is second function ')
def fun3():
fun4()
print('this is 3rd function')
def fun4():
print('this is fourth function')
# fun1()
# ############################################################
# ############################################################
# ############################################################
def recursiveMethod(n):
if n<1:
print(f'{n} is less then 1')
else:
recursiveMethod(n-1)
print(n)
# recursiveMethod(5)
# ############################################################
# ############################################################
# Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) 6*5*4*3*2*1
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
#############################
"""Setting assert rule , so that thet conditions are set and it will not use all the stack memory, before using assert ,
recrusion method will accept non onteger numbers and negative number
and it will give error """
assert x >= 0 and type(x) == int, 'The number must be positive integers only'
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 4
final = factorial(num)
# print("The factorial of", num, "is", final )
# ############################################################
# ############################################################
# write a recursive function to find fibonacci series
# 0 , 1 , 1 , 2,3,5,8,13,21
# f(n) = f(n-1) + f(n-2)
def fibonacci(n):
assert n >= 0 and int(n) == n, 'The number must be positive integers only'
if n in [0,1]:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
n = 5
# print(fibonacci(6))
# ############################################################
# ############################################################
# write a recursive function to find sum of digits of a positive integer
#
def sum(n):
"""Assert will check the number is inserted is positive or not and is integer only
Then if loop will check that if n is equals to 0 then it will stop recursive funtion
otherwise it will check remainder"""
assert n >= 0 and type(n) == int, 'The number must be positive integers only'
if n == 0:
return 0
else:
return int(n%10) + sum(int(n/10))
# print(sum(52))
# HACKER RANK
# Find the number of ways that a given integer,X , can be expressed as the sum of the Nth powers of unique, natural numbers.
import random
def powerSum(x,n):
N = random.randint(0,100)
N2 = random.randint(0,100)
totalSum = (N**n) + (N2**n)
# print(totalSum)
# print(x)
if(x != totalSum):
return powerSum(x,n)
else:
print(N , N2)
print(totalSum)
powerSum(5,2)