-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator_10.py
216 lines (159 loc) · 5.28 KB
/
Calculator_10.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#The ALU has digital circuits for sum, subtraction, multiplication and comparision.
#The challenge here would be to write the code for division, square root, trignometric and other fuctions
#The following python code just needs python install of 30 MB and was written by Dr. Harish Ravi on 24 Nov 2019
for i in range(0,10000):
x='25'; #2 digit number
y='14'; #2 digit number
for k in range(0,10000):
for r in range(0,500):
1
print('ss,PS,<3')
#Doing representation of two digit product
product=int(x[1])*int(y[1])+10*(int(x[0])*int(y[1])+int(x[1])*int(y[0]))+int(x[0])*int(y[0])*100
#Doing representation of digit sum and subtraction
add=int(x[1])+int(y[1])+10*(int(x[0])+int(y[0]))
sub=int(x[1])-int(y[1])+10*(int(x[0])-int(y[0]))
#Showing output,
print('Product of ',x,' and' ,y,' ', product)
print('Sum of',x,' and' ,y,' ',add)
print('Subtraction ',x,' and' ,y,' ',sub)
#Dividing x a two digit number by z a single digit number
z='3'# Single digit number
ha=1;# While loop flag
j=0; # Increasing the quotient in the loop until the product exceeds the divisor
while ha:
r=int(x[1])+10*int(x[0])-j*int(z[0]);
if(r<int(z[0])):
ha=0; #Setting the while loop flag to 0 to come out of the loop
j=j+1; #incrementing the quotient until it divides
j=j-1; # Reducing the quotient as we counted one past
#Getting the decimal point of the quotient
ha=1;
h=0;
while ha:
r2=r*10-h*int(z[0]);
if(r2<int(z[0])):
ha=0;
h=h+1;
h=h-1;
print('division of ',x,' and' ,z,' ',j,'.',h)
#Finding square root by subtracting successively the contribution from most significant digit.
sq='314';
ha=1;
a=0;
while ha:
luv=int(sq[0])*100+int(sq[1])*10+int(sq[2])-100*a*a;
a=a+1;
if luv<0:
ha=0;
a=a-2;
ha=1;
b=0;
while ha:
luv2=int(sq[0])*100+int(sq[1])*10+int(sq[2])-100*a*a-(20*a+b)*b;
b=b+1;
if luv2<0:
ha=0;
b=b-2;
ha=1;
c=0;
while ha:
luv3=100*(int(sq[0])*100+int(sq[1])*10+int(sq[2])-100*a*a-(20*a+b)*b)-c*(200*a+20*b+c);
c=c+1;
if luv3<0:
ha=0;
c=c-2;
print('Square root of ',sq , ' ',10*a+b,'.',c)
#Maclaurin expansion of all trignometric and hyperbolic functions
n=100
def hfactorial(n):
s=1;
for j in range(1,n+1):
s=s*j
return s
def hsin(x):
return x-x*x*x/6+x*x*x*x*x/120-x*x*x*x*x*x*x/5040;
def hcos(x):
return 1-x*x/2+1/24*x*x*x*x-x*x*x*x*x*x/720;
def htan(x):
return x+x*x*x/3+2/15*x*x*x*x*x+17/315*x*x*x*x*x*x*x+62/2035*x*x*x*x*x*x*x*x*x;
def h2cos(x):
s=0.0;
for j in range(n):
s=s+(-1)**j/hfactorial(2*j)*(x**(2*j))
return s
def h2sin(x):
s=0.0;
for j in range(n):
s=s+(-1)**j/hfactorial(2*j+1)*(x**(2*j+1))
return s
def h2sinh(x):
s=0.0;
for j in range(n):
s=s+1/hfactorial(2*j+1)*(x**(2*j+1))
return s
def h2atanh(x):
s=0.0;
for j in range(1,n):
s=s+1/(2*j-1)*(x**(2*j-1))
return s
def h2atan(x):
s=0.0;
for j in range(1,n):
s=s+(-1.0)**(j+1)/(2*j-1)*(x**(2*j-1))
return s
def h2ln1px(x):
s=0.0;
for j in range(1,n):
s=s+(-1)**(j+1)/j*(x**(j))
return s
def h2erf(x):
s=0.0;
for j in range(0,n):
s=s+2/np.sqrt(np.pi)*(-1)**j/(2*j+1)/hfactorial(j)*(x**(2*j+1))
return s
def h2exp(x):
s=0.0;
for j in range(n):
s=s+1.0/hfactorial(j)*(x**(j))
return s
def h2acot(x):
s=0.0;
for j in range(1,n):
s=s+(-1)**j/(2*j+1)*(x**(2*j+1))
return np.pi/2-s
def h2cosh(x):
s=0.0;
for j in range(1,n):
s=s+1/hfactorial(2*j)*(x**(2*j))
return s
n=10000
print('pi',h2atan(1)*4.0)
n=100
print('e',h2exp(1))
print('h2cos(3.14)', h2cos(3.14/2),'h2sin(0.1)' , h2sin(0.1),'h2sinh(0.1)', h2sinh(0.1))
"""
The results are,
Product of 25 and 14 350
Sum of 25 and 14 39
Subtraction 25 and 14 11
division of 25 and 3 8 . 3
Square root of 314 17 . 7
pi 3.1415936535907742
e 2.7182818284590455
h2cos(3.14) 0.0007963267107332883 h2sin(0.1) 0.09983341664682817 h2sinh(0.1) 0.10016675001984403
"""
#The following code works if you have numpy and matplotlib installed
import numpy as np
import matplotlib.pyplot as plt
def h2gamma(n):
if n==1:
return 1;
if n==0.5:
return 1;
else:
return (n-1)*h2gamma(n-1);
x=np.arange(0,1,0.05)
plt.plot(x,h2sin(x),x,h2cos(x),x,h2exp(x),x,h2erf(x),x,h2cosh(x),x,h2acot(x),x,h2ln1px(x),x,h2atan(x),x,h2atanh(x))
plt.legend(['h2sin(x)','h2cos(x)','h2exp(x)','h2erf(x)','h2cosh(x)','h2acot(x)','h2ln1px(x)','h2atan(x)','h2atanh(x)'])
plt.show()