Skip to content

Commit 155bd0b

Browse files
vipinkarthicpre-commit-ci[bot]github-actionscclausstianyizheng02
authored andcommitted
Fixes TheAlgorithms#9943 Added Doctests to binary_exponentiation_3.py (TheAlgorithms#10121)
* Python mirror_formulae.py is added to the repository * Changes done after reading readme.md * Changes for running doctest on all platforms * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Change 2 for Doctests * Changes for doctest 2 * updating DIRECTORY.md * Doctest whitespace error rectification to mirror_formulae.py * updating DIRECTORY.md * Adding Thermodynamic Work Done Formulae * Work done on/by body in a thermodynamic setting * updating DIRECTORY.md * updating DIRECTORY.md * Doctest adiition to binary_exponentiation_3.py * Change 1 * updating DIRECTORY.md * Rename binary_exponentiation_3.py to binary_exponentiation_2.py * updating DIRECTORY.md * updating DIRECTORY.md * Formatting --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent 5090e86 commit 155bd0b

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

DIRECTORY.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@
5454
* [Largest Pow Of Two Le Num](bit_manipulation/largest_pow_of_two_le_num.py)
5555
* [Missing Number](bit_manipulation/missing_number.py)
5656
* [Numbers Different Signs](bit_manipulation/numbers_different_signs.py)
57+
* [Power Of 4](bit_manipulation/power_of_4.py)
5758
* [Reverse Bits](bit_manipulation/reverse_bits.py)
5859
* [Single Bit Manipulation Operations](bit_manipulation/single_bit_manipulation_operations.py)
5960

6061
## Blockchain
61-
* [Chinese Remainder Theorem](blockchain/chinese_remainder_theorem.py)
6262
* [Diophantine Equation](blockchain/diophantine_equation.py)
63-
* [Modular Division](blockchain/modular_division.py)
6463

6564
## Boolean Algebra
6665
* [And Gate](boolean_algebra/and_gate.py)
@@ -101,11 +100,13 @@
101100
* [Diffie Hellman](ciphers/diffie_hellman.py)
102101
* [Elgamal Key Generator](ciphers/elgamal_key_generator.py)
103102
* [Enigma Machine2](ciphers/enigma_machine2.py)
103+
* [Fractionated Morse Cipher](ciphers/fractionated_morse_cipher.py)
104104
* [Hill Cipher](ciphers/hill_cipher.py)
105105
* [Mixed Keyword Cypher](ciphers/mixed_keyword_cypher.py)
106106
* [Mono Alphabetic Ciphers](ciphers/mono_alphabetic_ciphers.py)
107107
* [Morse Code](ciphers/morse_code.py)
108108
* [Onepad Cipher](ciphers/onepad_cipher.py)
109+
* [Permutation Cipher](ciphers/permutation_cipher.py)
109110
* [Playfair Cipher](ciphers/playfair_cipher.py)
110111
* [Polybius](ciphers/polybius.py)
111112
* [Porta Cipher](ciphers/porta_cipher.py)
@@ -172,6 +173,7 @@
172173

173174
## Data Structures
174175
* Arrays
176+
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
175177
* [Median Two Array](data_structures/arrays/median_two_array.py)
176178
* [Permutations](data_structures/arrays/permutations.py)
177179
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
@@ -352,6 +354,7 @@
352354
* [Smith Waterman](dynamic_programming/smith_waterman.py)
353355
* [Subset Generation](dynamic_programming/subset_generation.py)
354356
* [Sum Of Subset](dynamic_programming/sum_of_subset.py)
357+
* [Trapped Water](dynamic_programming/trapped_water.py)
355358
* [Tribonacci](dynamic_programming/tribonacci.py)
356359
* [Viterbi](dynamic_programming/viterbi.py)
357360
* [Word Break](dynamic_programming/word_break.py)
@@ -360,6 +363,7 @@
360363
* [Apparent Power](electronics/apparent_power.py)
361364
* [Builtin Voltage](electronics/builtin_voltage.py)
362365
* [Carrier Concentration](electronics/carrier_concentration.py)
366+
* [Charging Capacitor](electronics/charging_capacitor.py)
363367
* [Circular Convolution](electronics/circular_convolution.py)
364368
* [Coulombs Law](electronics/coulombs_law.py)
365369
* [Electric Conductivity](electronics/electric_conductivity.py)
@@ -466,6 +470,8 @@
466470
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)
467471

468472
## Greedy Methods
473+
* [Best Time To Buy And Sell Stock](greedy_methods/best_time_to_buy_and_sell_stock.py)
474+
* [Fractional Cover Problem](greedy_methods/fractional_cover_problem.py)
469475
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py)
470476
* [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py)
471477
* [Gas Station](greedy_methods/gas_station.py)
@@ -524,6 +530,10 @@
524530
* Local Weighted Learning
525531
* [Local Weighted Learning](machine_learning/local_weighted_learning/local_weighted_learning.py)
526532
* [Logistic Regression](machine_learning/logistic_regression.py)
533+
* Loss Functions
534+
* [Binary Cross Entropy](machine_learning/loss_functions/binary_cross_entropy.py)
535+
* [Huber Loss](machine_learning/loss_functions/huber_loss.py)
536+
* [Mean Squared Error](machine_learning/loss_functions/mean_squared_error.py)
527537
* [Mfcc](machine_learning/mfcc.py)
528538
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
529539
* [Polynomial Regression](machine_learning/polynomial_regression.py)
@@ -564,7 +574,9 @@
564574
* [Carmichael Number](maths/carmichael_number.py)
565575
* [Catalan Number](maths/catalan_number.py)
566576
* [Ceil](maths/ceil.py)
577+
* [Chebyshev Distance](maths/chebyshev_distance.py)
567578
* [Check Polygon](maths/check_polygon.py)
579+
* [Chinese Remainder Theorem](maths/chinese_remainder_theorem.py)
568580
* [Chudnovsky Algorithm](maths/chudnovsky_algorithm.py)
569581
* [Collatz Sequence](maths/collatz_sequence.py)
570582
* [Combinations](maths/combinations.py)
@@ -591,6 +603,7 @@
591603
* [Gaussian](maths/gaussian.py)
592604
* [Gaussian Error Linear Unit](maths/gaussian_error_linear_unit.py)
593605
* [Gcd Of N Numbers](maths/gcd_of_n_numbers.py)
606+
* [Germain Primes](maths/germain_primes.py)
594607
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
595608
* [Greedy Coin Change](maths/greedy_coin_change.py)
596609
* [Hamming Numbers](maths/hamming_numbers.py)
@@ -618,7 +631,9 @@
618631
* [Matrix Exponentiation](maths/matrix_exponentiation.py)
619632
* [Max Sum Sliding Window](maths/max_sum_sliding_window.py)
620633
* [Median Of Two Arrays](maths/median_of_two_arrays.py)
634+
* [Minkowski Distance](maths/minkowski_distance.py)
621635
* [Mobius Function](maths/mobius_function.py)
636+
* [Modular Division](maths/modular_division.py)
622637
* [Modular Exponential](maths/modular_exponential.py)
623638
* [Monte Carlo](maths/monte_carlo.py)
624639
* [Monte Carlo Dice](maths/monte_carlo_dice.py)
@@ -720,12 +735,16 @@
720735
## Neural Network
721736
* [2 Hidden Layers Neural Network](neural_network/2_hidden_layers_neural_network.py)
722737
* Activation Functions
738+
* [Binary Step](neural_network/activation_functions/binary_step.py)
723739
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
724740
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.py)
725741
* [Mish](neural_network/activation_functions/mish.py)
726742
* [Rectified Linear Unit](neural_network/activation_functions/rectified_linear_unit.py)
727743
* [Scaled Exponential Linear Unit](neural_network/activation_functions/scaled_exponential_linear_unit.py)
728744
* [Sigmoid Linear Unit](neural_network/activation_functions/sigmoid_linear_unit.py)
745+
* [Soboleva Modified Hyperbolic Tangent](neural_network/activation_functions/soboleva_modified_hyperbolic_tangent.py)
746+
* [Softplus](neural_network/activation_functions/softplus.py)
747+
* [Squareplus](neural_network/activation_functions/squareplus.py)
729748
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
730749
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
731750
* [Perceptron](neural_network/perceptron.py)
@@ -779,6 +798,7 @@
779798
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
780799
* [Photoelectric Effect](physics/photoelectric_effect.py)
781800
* [Potential Energy](physics/potential_energy.py)
801+
* [Reynolds Number](physics/reynolds_number.py)
782802
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
783803
* [Shear Stress](physics/shear_stress.py)
784804
* [Speed Of Sound](physics/speed_of_sound.py)
@@ -1101,6 +1121,7 @@
11011121
* [Interpolation Search](searches/interpolation_search.py)
11021122
* [Jump Search](searches/jump_search.py)
11031123
* [Linear Search](searches/linear_search.py)
1124+
* [Median Of Medians](searches/median_of_medians.py)
11041125
* [Quick Select](searches/quick_select.py)
11051126
* [Sentinel Linear Search](searches/sentinel_linear_search.py)
11061127
* [Simple Binary Search](searches/simple_binary_search.py)
@@ -1201,6 +1222,7 @@
12011222
* [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py)
12021223
* [Split](strings/split.py)
12031224
* [String Switch Case](strings/string_switch_case.py)
1225+
* [Strip](strings/strip.py)
12041226
* [Text Justification](strings/text_justification.py)
12051227
* [Top K Frequent Words](strings/top_k_frequent_words.py)
12061228
* [Upper](strings/upper.py)

maths/binary_exponentiation_2.py

+35-24
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
"""
2-
* Binary Exponentiation for Powers
3-
* This is a method to find a^b in a time complexity of O(log b)
4-
* This is one of the most commonly used methods of finding powers.
5-
* Also useful in cases where solution to (a^b)%c is required,
6-
* where a,b,c can be numbers over the computers calculation limits.
7-
* Done using iteration, can also be done using recursion
8-
9-
* @author chinmoy159
10-
* @version 1.0 dated 10/08/2017
2+
Binary Exponentiation
3+
This is a method to find a^b in O(log b) time complexity
4+
This is one of the most commonly used methods of exponentiation
5+
It's also useful when the solution to (a^b) % c is required because a, b, c may be
6+
over the computer's calculation limits
7+
8+
Let's say you need to calculate a ^ b
9+
- RULE 1 : a ^ b = (a*a) ^ (b/2) ---- example : 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2
10+
- RULE 2 : IF b is odd, then a ^ b = a * (a ^ (b - 1)), where b - 1 is even
11+
Once b is even, repeat the process until b = 1 or b = 0, because a^1 = a and a^0 = 1
12+
13+
For modular exponentiation, we use the fact that (a*b) % c = ((a%c) * (b%c)) % c
14+
Now apply RULE 1 or 2 as required
15+
16+
@author chinmoy159
1117
"""
1218

1319

1420
def b_expo(a: int, b: int) -> int:
21+
"""
22+
>>> b_expo(2, 10)
23+
1024
24+
>>> b_expo(9, 0)
25+
1
26+
>>> b_expo(0, 12)
27+
0
28+
>>> b_expo(4, 12)
29+
16777216
30+
"""
1531
res = 1
1632
while b > 0:
1733
if b & 1:
@@ -24,6 +40,16 @@ def b_expo(a: int, b: int) -> int:
2440

2541

2642
def b_expo_mod(a: int, b: int, c: int) -> int:
43+
"""
44+
>>> b_expo_mod(2, 10, 1000000007)
45+
1024
46+
>>> b_expo_mod(11, 13, 19)
47+
11
48+
>>> b_expo_mod(0, 19, 20)
49+
0
50+
>>> b_expo_mod(15, 5, 4)
51+
3
52+
"""
2753
res = 1
2854
while b > 0:
2955
if b & 1:
@@ -33,18 +59,3 @@ def b_expo_mod(a: int, b: int, c: int) -> int:
3359
b >>= 1
3460

3561
return res
36-
37-
38-
"""
39-
* Wondering how this method works !
40-
* It's pretty simple.
41-
* Let's say you need to calculate a ^ b
42-
* RULE 1 : a ^ b = (a*a) ^ (b/2) ---- example : 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2
43-
* RULE 2 : IF b is ODD, then ---- a ^ b = a * (a ^ (b - 1)) :: where (b - 1) is even.
44-
* Once b is even, repeat the process to get a ^ b
45-
* Repeat the process till b = 1 OR b = 0, because a^1 = a AND a^0 = 1
46-
*
47-
* As far as the modulo is concerned,
48-
* the fact : (a*b) % c = ((a%c) * (b%c)) % c
49-
* Now apply RULE 1 OR 2 whichever is required.
50-
"""

0 commit comments

Comments
 (0)