-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhile_programs.py
126 lines (109 loc) · 2.27 KB
/
while_programs.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
126
#-------------------------------------------------------------------------------
# Name: while_programs
# Purpose: Python program to test several while programs which
# perform arithmetic operations
#
# Author: carla
#
# Created: 09/12/2015
# Updated: 18/12/2015
#-------------------------------------------------------------------------------
def main():
pass
if __name__ == '__main__':
main()
#----------------------FUNCTIONS--------------------#
def sum(x1, x2):
x3 = 0
while x2 != x3:
x1+=1
x2-=1
return x1
def substract(x1, x2):
x3 = 0
while x2 != x3:
x1-=1
x2-=1
return x1
def multiply(x1, x2):
x3 = x1
while x2 != 0:
x1= sum(x1,x3)
x2-= 1
x1 -= x3
return x1
def divide(x1,x2):
x1 += 1
x3 = 0
x4 = 0
while x1 > x3:
x4 += 1
x1 = substract(x1,x2)
x1 = x4 - 1
return x1
def mod(x1,x2):
x1 += 1
x3 = 0
x4 = 0
while x1 > x3:
x4 = x1
x1 = substract(x1,x2)
x1 = x4-1
return x1
def pow2(x1):
x2 = 1 # current number
x3 = 0 # current power
while x3 != x1:
x3 += 1
x2 = sum(x2, x2)
x1 = x2
return x1
def factorial(x1):
x2 = 1
x3 = 0
while x1 != x3:
x3 += 1
x2 = multiply(x2, x3)
x1 = x2
return x1
def log(x1):
x2 = 0
x3 = 0
while x1 != x3:
x1 = x1 / 10
x2 += 1
x1 = x2 - 1
return x1
def sum_factorial(x1):
x1 += 1
x2 = 0
x4 = 0
while x1 != x2:
x3 = factorial(x2)
x4 = x4 + x3
x2 += 1
x1 = x4
return x1
def divMod(x1,x2):
'''This function performs the operation:
y / (x % y)'''
x3 = 0
x4 = 0
x6 = x2
while x6 > x3:
x5 = mod(x1,x2)
x6 = substract(x6,x5)
x4 += 1
x1 = x4
return x1
def divInteger(x1, x2):
'''This function performs the operation:
div(y,x) = max{z >= 0 / z*x <= y} '''
x3 = 0
x4 = 0
while x4 <= x1:
x3 += 1
x4 = x3 * x2
x1 = x3-1
return x1
#------------------END OF FUNCTIONS--------------------#