This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMathHack.py
213 lines (179 loc) · 6.43 KB
/
MathHack.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
#! /usr/bin/env python3
import math # for trig func. , degrees(),radians() && gcd()
import sys #for cli args
def err(): #function for input error
print("Invalid Input, Sorry.")
def trcalc(func,angle): #function for TCAL
if func == "sin":
print('Result =',round((math.sin(angle)),4))
elif func == "cos":
print('Result =',round(math.cos(angle),4))
elif func == "tan":
print('Result =',round(math.tan(angle),4))
elif func == "cosec":
print('Result =',round(1/(math.sin(angle)),4))
elif func == "sec":
print('Result =',round(1/(math.cos(angle)),4))
elif func == "cot":
print('Result =',round(1/(math.tan(angle)),4))
else: err()
def factorial(x):
f = 1
i = x
while i >= 1:
f = f*i
i -= 1
print('Factorial of ',x,' = ',"{:e}".format(f))
frequency = len(sys.argv)
arguments = sys.argv
#-----Begin 'Main'-----
if frequency >= 3:
choice = arguments[1]
if choice == "cal" and frequency >= 5:
num1, op, num2 = float(arguments[2]), arguments[3] , float(arguments[4])
if op == '+':
print("Result = ",num1+num2)
elif op == '-':
print("Result = ",num1-num2)
elif op == 'x' or op == '*':
print("Result = ",num1*num2)
elif op == '/':
print("Result = ",num1/num2)
elif op == '^':
print("Result = ",num1**num2)
elif op == '%':
print("Result = ",num1%num2)
elif op == '|' or op == '\\' :
print("Result = ",(num1)**(1/num2))
else:
err() #invokes error message
elif choice == 'fact':
num = int(arguments[2])
if num >= 2:
print("Factors of ",num," are: ","1, ", end = "") # 1 is factor of every num
factor = 2 # sets counter to begin @ 2
while factor <= ((num)/2):
if num%factor == 0:
print(factor,end=", ")
factor += 1
print(num) # num is factor of itself
else:
err()
elif choice == 'avg' and frequency >= 4:
import statistics
lst =[]
count = 0;
for argument in arguments :
if count >= 2:
lst.append(int(argument))
count += 1
print('The average is',statistics.mean(lst))
elif choice == 'prime':
flag = False # flag is false by default
n = int(arguments[2])
i = 2
while i <= (n**0.5):
if (n%i) == 0:
flag = True
break
i += 1
if n == 1:
print('1 is neither prime nor composite')
elif flag == False :
print(n,' is a prime number.')
elif flag == True :
print(n,' is not a prime number.')
elif choice == 'pmfact': #prints only unique prime factors
num = int(arguments[2])
print('Prime factors of ',num,' are : ', end='')
i = 2
while i <= num:
if num%i == 0:
isPrime = 1
j = 2
while j <= (i**(0.5)):
if i%j == 0:
isPrime = 0
break
j += 1
if isPrime == 1:
print(i,end =' ')
i += 1
print('')
elif choice == 'tab':
num = float(arguments[2])
for i in range (2,20):
print(num,' x ',i,' = ',round((num*i),4))
elif choice == 'fctrl':
num = int(arguments[2])
factorial(num)
elif choice == 'eq' and frequency >= 5:
a = float(arguments[2])
b = float(arguments[3])
c = float(arguments[4])
dscrmnt = (b**2) - (4*a*c)
print('Discriminant evaluates to ',dscrmnt)
if dscrmnt > 0:
r1= (-b + (dscrmnt**0.5))/ (2*a);
r2= (-b - (dscrmnt**0.5))/ (2*a);
print('Root 1 = ',round(r1,4))
print('Root 1 = ',round(r2,4))
elif dscrmnt == 0:
r = -b/(2*a)
print('Root 1 = Root 2 = ',round(r,4))
else:
print('Roots are not real.')
real = -b/(2*a)
ima = ((-dscrmnt)**0.5) / (2*a)
print('Root 1 = ',round(real,4),'+',round(ima,4),'i')
print('Root 2 = ',round(real,4),'-',round(ima,4),'i')
elif choice == 'hcf' and frequency >= 4:
a = int(arguments[2])
b = int(arguments[3])
print('HCF of ',a,' and ',b,' = ',math.gcd(a,b)) #uses gcd() from math module
elif choice == 'smp' and frequency >= 4:
nu = int(arguments[2])
den = int(arguments[3])
gcd = math.gcd(nu,den)
print('Simplified fraction is',int(nu/gcd),'/',int(den/gcd))
elif choice == 'tcal' and frequency >= 5:
unit = arguments[2]
func = arguments[3]
if unit == 'r' :
rad = float(arguments[4])
trcalc(func,rad)
elif unit == 'd' :
deg = float(arguments[4])
rad = math.radians(deg) # converts degrees to rad
trcalc(func,rad)
else:
err()
elif choice == 'ttab' and frequency >= 4:
what = arguments[2]
if what == 'd' or what == 'D':
dig = float(arguments[3])
this = math.radians(dig) # converts to radians
print('Angle in radian =',round(this,4))
print('\nSin of',dig,'=',round(math.sin(this),4))
print('Cos of',dig,'=',round(math.cos(this),4))
print('Tan of',dig,'=',round(math.tan(this),4))
print('Cosec of',dig,'=',round(1/(math.sin(this)),4))
print('Sec of',dig,'=',round(1/(math.cos(this)),4))
print('Cot of',dig,'=',round(1/(math.tan(this)),4))
elif what == 'r' or what == 'R' :
this = float(arguments[3])
dig = math.degrees(this) # converts to degrees
print('Angle in degree =',round(dig,4))
print('\nSin of',this,'=',round(math.sin(this),4))
print('Cos of',this,'=',round(math.cos(this),4))
print('Tan of',this,'=',round(math.tan(this),4))
print('Cosec of',this,'=',round(1/(math.sin(this)),4))
print('Sec of',this,'=',round(1/(math.cos(this)),4))
print('Cot of',this,'=',round(1/(math.tan(this)),4))
else:
err()
else :
print("Insufficient or Incorrect Input.")
else :
print("Too few arguments.")
#---------End 'Main'----------