Skip to content

Commit 3929d16

Browse files
Added matrix exponentiation approach for finding fibonacci number. (TheAlgorithms#1042)
* Added matrix exponentiation approach for finding fibonacci number. * Implemented the way of finding nth fibonacci. * Complexity is about O(log(n)*8) * Updated the matrix exponentiation approach of finding nth fibonacci. - Removed some extra spaces - Added the complexity of bruteforce algorithm - Removed unused function called zerro() - Added some docktest based on request * Updated the matrix exponentiation approach of finding nth fibonacci. - Removed some extra spaces - Added the complexity of bruteforce algorithm - Removed unused function called zerro() - Added some docktest based on request * Tighten up main() and add comments on performance Signed-off-by: Deus-Absconditus <[email protected]>
1 parent 9fcfe6a commit 3929d16

22 files changed

+615
-139
lines changed

.gitpod.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
image: gitpod/workspace-full-vnc
2+
3+
tasks:
4+
- init: pip install -r ./requirements.txt

.travis.yml

+1-6
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ script:
1212
- pytest . --doctest-modules
1313
--ignore=data_structures/stacks/balanced_parentheses.py
1414
--ignore=data_structures/stacks/infix_to_postfix_conversion.py
15-
--ignore=file_transfer_protocol/ftp_send_receive.py
16-
--ignore=file_transfer_protocol/ftp_client_server.py
1715
--ignore=machine_learning/linear_regression.py
1816
--ignore=machine_learning/perceptron.py
1917
--ignore=machine_learning/random_forest_classification/random_forest_classification.py
2018
--ignore=machine_learning/random_forest_regression/random_forest_regression.py
21-
--ignore=maths/abs_min.py
22-
--ignore=maths/binary_exponentiation.py
23-
--ignore=maths/lucas_series.py
24-
--ignore=maths/sieve_of_eratosthenes.py
19+
--ignore=virtualenv # do not test our dependencies
2520
after_success:
2621
- python scripts/build_directory_md.py
2722
- cat DIRECTORY.md

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ We want your work to be readable by others; therefore, we encourage you to note
6969
"""
7070
This function sums two integers a and b
7171
Return: a + b
72-
"""
72+
"""
7373
return a + b
7474
```
7575

DIRECTORY.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [newton method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_method.py)
66
* [newton raphson method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_raphson_method.py)
77
## Backtracking
8+
* [all combinations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_combinations.py)
89
* [all permutations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_permutations.py)
910
* [all subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py)
1011
* [minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py)
@@ -27,7 +28,6 @@
2728
* [morse code implementation](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code_implementation.py)
2829
* [onepad cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py)
2930
* [playfair cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
30-
* [prehistoric men](https://github.com/TheAlgorithms/Python/blob/master/ciphers/prehistoric_men.txt)
3131
* [rabin miller](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rabin_miller.py)
3232
* [rot13](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rot13.py)
3333
* [rsa cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rsa_cipher.py)
@@ -39,6 +39,7 @@
3939
* [vigenere cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/vigenere_cipher.py)
4040
* [xor cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/xor_cipher.py)
4141
## Compression
42+
* [burrows wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py)
4243
* [huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py)
4344
* [peak signal to noise ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py)
4445
* Image Data
@@ -100,8 +101,10 @@
100101
## Dynamic Programming
101102
* [abbreviation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/abbreviation.py)
102103
* [bitmask](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/bitmask.py)
104+
* [climbing stairs](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/climbing_stairs.py)
103105
* [coin change](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/coin_change.py)
104106
* [edit distance](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/edit_distance.py)
107+
* [factorial](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/factorial.py)
105108
* [fast fibonacci](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/fast_fibonacci.py)
106109
* [fibonacci](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/fibonacci.py)
107110
* [floyd warshall](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/floyd_warshall.py)
@@ -142,9 +145,9 @@
142145
* [eulerian path and circuit for undirected graph](https://github.com/TheAlgorithms/Python/blob/master/graphs/eulerian_path_and_circuit_for_undirected_graph.py)
143146
* [even tree](https://github.com/TheAlgorithms/Python/blob/master/graphs/even_tree.py)
144147
* [finding bridges](https://github.com/TheAlgorithms/Python/blob/master/graphs/finding_bridges.py)
145-
* [floyd warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/floyd_warshall.py)
146148
* [graph list](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_list.py)
147149
* [graph matrix](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_matrix.py)
150+
* [graphs floyd warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/graphs_floyd_warshall.py)
148151
* [kahns algorithm long](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_long.py)
149152
* [kahns algorithm topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
150153
* [minimum spanning tree kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
@@ -170,13 +173,17 @@
170173
* [knn sklearn](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/knn_sklearn.py)
171174
* [linear regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_regression.py)
172175
* [logistic regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/logistic_regression.py)
176+
* [NaiveBayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/NaiveBayes.ipynb)
173177
* [perceptron](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/perceptron.py)
178+
* [reuters one vs rest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/reuters_one_vs_rest_classifier.ipynb)
174179
* [scoring functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py)
175180
* Random Forest Classification
176181
* [random forest classification](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/random_forest_classification.py)
182+
* [random forest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/random_forest_classifier.ipynb)
177183
* [Social Network Ads](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/Social_Network_Ads.csv)
178184
* Random Forest Regression
179185
* [Position Salaries](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/Position_Salaries.csv)
186+
* [random forest regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/random_forest_regression.ipynb)
180187
* [random forest regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/random_forest_regression.py)
181188
## Maths
182189
* [3n+1](https://github.com/TheAlgorithms/Python/blob/master/maths/3n+1.py)
@@ -197,7 +204,7 @@
197204
* [find max](https://github.com/TheAlgorithms/Python/blob/master/maths/find_max.py)
198205
* [find min](https://github.com/TheAlgorithms/Python/blob/master/maths/find_min.py)
199206
* [greater common divisor](https://github.com/TheAlgorithms/Python/blob/master/maths/greater_common_divisor.py)
200-
* [lucas series](https://github.com/TheAlgorithms/Python/blob/master/maths/lucas%20series.py)
207+
* [lucas series](https://github.com/TheAlgorithms/Python/blob/master/maths/lucas_series.py)
201208
* [modular exponential](https://github.com/TheAlgorithms/Python/blob/master/maths/modular_exponential.py)
202209
* [newton raphson](https://github.com/TheAlgorithms/Python/blob/master/maths/newton_raphson.py)
203210
* [prime check](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_check.py)
@@ -208,6 +215,8 @@
208215
* [volume](https://github.com/TheAlgorithms/Python/blob/master/maths/volume.py)
209216
## Matrix
210217
* [matrix operation](https://github.com/TheAlgorithms/Python/blob/master/matrix/matrix_operation.py)
218+
* [nth fibonacci using matrix exponentiation](https://github.com/TheAlgorithms/Python/blob/master/matrix/nth_fibonacci_using_matrix_exponentiation.py)
219+
* [rotate matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/rotate_matrix.py)
211220
* [searching in sorted matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/searching_in_sorted_matrix.py)
212221
* [spiral print](https://github.com/TheAlgorithms/Python/blob/master/matrix/spiral_print.py)
213222
## Networking Flow
@@ -216,16 +225,17 @@
216225
## Neural Network
217226
* [back propagation neural network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/back_propagation_neural_network.py)
218227
* [convolution neural network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/convolution_neural_network.py)
228+
* [fully connected neural network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/fully_connected_neural_network.ipynb)
219229
* [perceptron](https://github.com/TheAlgorithms/Python/blob/master/neural_network/perceptron.py)
220230
## Other
221231
* [anagrams](https://github.com/TheAlgorithms/Python/blob/master/other/anagrams.py)
222232
* [binary exponentiation](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation.py)
223233
* [binary exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py)
224234
* [detecting english programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py)
225-
* [dictionary](https://github.com/TheAlgorithms/Python/blob/master/other/dictionary.txt)
226235
* [euclidean gcd](https://github.com/TheAlgorithms/Python/blob/master/other/euclidean_gcd.py)
227236
* [finding primes](https://github.com/TheAlgorithms/Python/blob/master/other/finding_primes.py)
228237
* [fischer yates shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
238+
* [Food wastage analysis from 1961-2013 (FAO)](https://github.com/TheAlgorithms/Python/blob/master/other/Food%20wastage%20analysis%20from%201961-2013%20(FAO).ipynb)
229239
* [frequency finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
230240
* [game of life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
231241
* [linear congruential generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py)
@@ -238,8 +248,6 @@
238248
* [two sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py)
239249
* [word patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py)
240250
* [words](https://github.com/TheAlgorithms/Python/blob/master/other/words)
241-
* Pycache
242-
* [password generator.cpython-37](https://github.com/TheAlgorithms/Python/blob/master/other/__pycache__/password_generator.cpython-37.pyc)
243251
## Project Euler
244252
* Problem 01
245253
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol1.py)
@@ -281,16 +289,13 @@
281289
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_10/sol1.py)
282290
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_10/sol2.py)
283291
* Problem 11
284-
* [grid](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/grid.txt)
285292
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol1.py)
286293
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol2.py)
287294
* Problem 12
288295
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py)
289296
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol2.py)
290297
* Problem 13
291-
* [num](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/num.txt)
292298
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol1.py)
293-
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol2.py)
294299
* Problem 14
295300
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_14/sol1.py)
296301
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_14/sol2.py)
@@ -309,7 +314,6 @@
309314
* Problem 21
310315
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_21/sol1.py)
311316
* Problem 22
312-
* [p022 names](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/p022_names.txt)
313317
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/sol1.py)
314318
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/sol2.py)
315319
* Problem 234
@@ -337,8 +341,6 @@
337341
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_53/sol1.py)
338342
* Problem 76
339343
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py)
340-
## Scripts
341-
* [build directory md](https://github.com/TheAlgorithms/Python/blob/master/scripts/build_directory_md.py)
342344
## Searches
343345
* [binary search](https://github.com/TheAlgorithms/Python/blob/master/searches/binary_search.py)
344346
* [interpolation search](https://github.com/TheAlgorithms/Python/blob/master/searches/interpolation_search.py)
@@ -347,7 +349,6 @@
347349
* [quick select](https://github.com/TheAlgorithms/Python/blob/master/searches/quick_select.py)
348350
* [sentinel linear search](https://github.com/TheAlgorithms/Python/blob/master/searches/sentinel_linear_search.py)
349351
* [tabu search](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_search.py)
350-
* [tabu test data](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_test_data.txt)
351352
* [ternary search](https://github.com/TheAlgorithms/Python/blob/master/searches/ternary_search.py)
352353
## Sorts
353354
* [bitonic sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bitonic_sort.py)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/Python)
2+
13
# The Algorithms - Python <!-- [![Build Status](https://travis-ci.org/TheAlgorithms/Python.svg)](https://travis-ci.org/TheAlgorithms/Python) -->
24

35
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/TheAlgorithms/100) &nbsp;

arithmetic_analysis/newton_raphson_method.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Implementing Newton Raphson method in Python
2-
# Author: Haseeb
2+
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
33

44
from sympy import diff
55
from decimal import Decimal
@@ -30,7 +30,3 @@ def NewtonRaphson(func, a):
3030

3131
# Exponential Roots
3232
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
33-
34-
35-
36-

file_transfer_protocol/ftp_client_server.py

+49-50
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,55 @@
22

33
import socket # Import socket module
44

5-
port = 60000 # Reserve a port for your service.
6-
s = socket.socket() # Create a socket object
7-
host = socket.gethostname() # Get local machine name
8-
s.bind((host, port)) # Bind to the port
9-
s.listen(5) # Now wait for client connection.
10-
11-
print('Server listening....')
12-
13-
while True:
14-
conn, addr = s.accept() # Establish connection with client.
15-
print('Got connection from', addr)
16-
data = conn.recv(1024)
17-
print('Server received', repr(data))
18-
19-
filename = 'mytext.txt'
20-
with open(filename, 'rb') as f:
21-
in_data = f.read(1024)
22-
while in_data:
23-
conn.send(in_data)
24-
print('Sent ', repr(in_data))
25-
in_data = f.read(1024)
26-
27-
print('Done sending')
28-
conn.send('Thank you for connecting')
29-
conn.close()
30-
5+
if __name__ == '__main__':
6+
port = 60000 # Reserve a port for your service.
7+
s = socket.socket() # Create a socket object
8+
host = socket.gethostname() # Get local machine name
9+
s.bind((host, port)) # Bind to the port
10+
s.listen(5) # Now wait for client connection.
3111

32-
# client side server
33-
34-
import socket # Import socket module
12+
print('Server listening....')
3513

36-
s = socket.socket() # Create a socket object
37-
host = socket.gethostname() # Get local machine name
38-
port = 60000 # Reserve a port for your service.
39-
40-
s.connect((host, port))
41-
s.send("Hello server!")
42-
43-
with open('received_file', 'wb') as f:
44-
print('file opened')
4514
while True:
46-
print('receiving data...')
47-
data = s.recv(1024)
48-
print('data=%s', (data))
49-
if not data:
50-
break
51-
# write data to a file
52-
f.write(data)
53-
54-
f.close()
55-
print('Successfully get the file')
56-
s.close()
57-
print('connection closed')
15+
conn, addr = s.accept() # Establish connection with client.
16+
print('Got connection from', addr)
17+
data = conn.recv(1024)
18+
print('Server received', repr(data))
19+
20+
filename = 'mytext.txt'
21+
with open(filename, 'rb') as f:
22+
in_data = f.read(1024)
23+
while in_data:
24+
conn.send(in_data)
25+
print('Sent ', repr(in_data))
26+
in_data = f.read(1024)
27+
28+
print('Done sending')
29+
conn.send('Thank you for connecting')
30+
conn.close()
31+
32+
33+
# client side server
34+
35+
s = socket.socket() # Create a socket object
36+
host = socket.gethostname() # Get local machine name
37+
port = 60000 # Reserve a port for your service.
38+
39+
s.connect((host, port))
40+
s.send("Hello server!")
41+
42+
with open('received_file', 'wb') as f:
43+
print('file opened')
44+
while True:
45+
print('receiving data...')
46+
data = s.recv(1024)
47+
print('data=%s', (data))
48+
if not data:
49+
break
50+
# write data to a file
51+
f.write(data)
52+
53+
f.close()
54+
print('Successfully get the file')
55+
s.close()
56+
print('connection closed')

file_transfer_protocol/ftp_send_receive.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
"""
1010

1111
from ftplib import FTP
12-
ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here
13-
ftp.login(user='username', passwd='password')
14-
ftp.cwd('/Enter the directory here/')
12+
13+
14+
if __name__ == '__main__':
15+
ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here
16+
ftp.login(user='username', passwd='password')
17+
ftp.cwd('/Enter the directory here/')
1518

1619
"""
1720
The file which will be received via the FTP server

graphs/dijkstra.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def dijkstra(graph, start, end):
7171
"F": [["C", 3], ["E", 3]],
7272
}
7373

74-
"""
74+
r"""
7575
Layout of G2:
7676
7777
E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F
@@ -87,7 +87,7 @@ def dijkstra(graph, start, end):
8787
"F": [],
8888
}
8989

90-
"""
90+
r"""
9191
Layout of G3:
9292
9393
E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F

maths/abs_min.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from abs import abs_val
1+
from .abs import abs_val
2+
23

34
def absMin(x):
45
"""
5-
# >>>absMin([0,5,1,11])
6+
>>> absMin([0,5,1,11])
67
0
7-
# >>absMin([3,-10,-2])
8+
>>> absMin([3,-10,-2])
89
-2
910
"""
1011
j = x[0]
@@ -13,9 +14,11 @@ def absMin(x):
1314
j = i
1415
return j
1516

17+
1618
def main():
1719
a = [-3,-1,2,-11]
1820
print(absMin(a)) # = -1
1921

22+
2023
if __name__ == '__main__':
2124
main()

maths/binary_exponentiation.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ def binary_exponentiation(a, n):
1717
return b * b
1818

1919

20-
try:
21-
BASE = int(input('Enter Base : '))
22-
POWER = int(input("Enter Power : "))
23-
except ValueError:
24-
print("Invalid literal for integer")
25-
26-
RESULT = binary_exponentiation(BASE, POWER)
27-
print("{}^({}) : {}".format(BASE, POWER, RESULT))
20+
if __name__ == "__main__":
21+
try:
22+
BASE = int(input("Enter Base : ").strip())
23+
POWER = int(input("Enter Power : ").strip())
24+
except ValueError:
25+
print("Invalid literal for integer")
26+
27+
RESULT = binary_exponentiation(BASE, POWER)
28+
print("{}^({}) : {}".format(BASE, POWER, RESULT))

0 commit comments

Comments
 (0)