Created
January 9, 2023 16:36
-
-
Save mykhailokrainik/327b80849a97252fa52035d10be40a61 to your computer and use it in GitHub Desktop.
Peasant Multiplication (https://www.cut-the-knot.org/Curriculum/Algebra/PeasantMultiplication.shtml)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def multiplication(a,b) | |
m = {} | |
while a >= 1 | |
m[a] = b | |
a = a / 2 | |
b = b * 2 | |
end | |
m.select { |k, _| k % 2 != 0 }.values.sum | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'RPM' do | |
it 'multiplication 11 * 11' do | |
expect(main(11, 11)).to eq(121) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment