Browsing the archives for the ruby tag.

Russian Peasant Multiplication in Ruby

programming

Russian Peasant Multiplication: a very simple and elegant way to multiple.

Read more: School-boy example of how and why it works and about Ancient Egyptian Multiplication on wikipedia

So here’s my simple implementation: (or as a gist on github)

module RussianPeasantMultiplication
  def russian_peasant_multiply(b)
    numbers_to_add = []
    a, b = [self, b].sort #So we have the smaller number as the first

    negative_operands = [a, b].select { |n| n < 0 }
    result_should_be_negative = negative_operands.size.odd? # or negative_operands.size == 1

    # Now get the absoultes
    a, b = [a, b].map { |n| n.abs }

    while( a > 1 )
      a = a >> 1 # halv it
      b = b < < 1 # double it
      if a.odd? # or (a % 2 == 0 )
        numbers_to_add << b
      else
      end
    end
    result = numbers_to_add.inject(0) { |sum, n| sum += n }
    result = result * -1 if result_should_be_negative
    result
  end
end

class Integer
  include RussianPeasantMultiplication
end

# Tests
require "test/unit"

class TestInteger < Test::Unit::TestCase
  def test_russian_peasant_multiply
    assert_equal(22 * 70, 22.russian_peasant_multiply(70))
  end

  def test_russian_peasant_multiply_for_negative_numbers
    assert_equal(-22 * 70, -22.russian_peasant_multiply(70))
  end

  def test_russian_peasant_multiply_for_negative_arguments
    assert_equal(22 * -70, 22.russian_peasant_multiply(-70))
  end

  def test_russian_peasant_multiply_for_negative_numbers_and_arguments
    assert_equal(-22 * -70, -22.russian_peasant_multiply(-70))
  end

  def test_russian_peasant_multiply_for_zero
    assert_equal(0 * -70, 0.russian_peasant_multiply(-70))
  end

  def test_russian_peasant_multiply_for_zero_arguments
    assert_equal(-22 * 0, -22.russian_peasant_multiply(0))
  end

  def test_russian_peasant_multiply_for_zero_numbers_and_arguments
    assert_equal(0 * 0, 0.russian_peasant_multiply(0))
  end
end
Add the first comment

Rails: Table join with specified fields in select

programming

Figured this out after a lot of monkeying-around (I mean script/console).

Situation:

  • I have two tables (Revisions has_many Inputs)
  • I can load Revisions and then for each I can find Inputs, but quickly found out that for my situation, it leads to a lot of queries. So I want to load the required fields from both tables together

:joins is the only way to do this, using :includes does NOT respect the select clause. Here is the gist I created:

Admittedly, this is hacky, too hacky for my comfort. Comment/suggest a better/cleaner solution?

Note: Found out that there is a gem to do this: ar-select-with-include

Add the first comment

Rails Guides on my Kindle DX!!! (or any webpage for that matter)

tech

I got a Amazon Kindle DX for a birthday gift! Thanks Ujwala!

And so far I am loving it. It’s better to read on compared to Kindle 1 (which, BTW, Ujwala had gifted to me last year) – and the native PDF support (and the search-anywhere) is awesome! The experimental browser that comes with the DX is much much improved compared to Kindle 1st Gen.

So I was reading more about Rails Routing in the rails-guides. And while I was in the bus today morning I wanted to read that on the Kindle (coz it is a pleasure to read on it). So I fired up the guides page on the experimental browser – it works, but reading a PDF or a Kindle-formatted book is so much better.

So I got this simple idea. Print the web page as a PDF and e-mail it to your Kindle. Here’s what I did:

  1. Go to the site
  2. Print – (PDF format “saved to file” instead of sending it to a printer). I was on Linux – Mac also has the “Print to PDF/PS support by default, for Windows you’ll need to get CutePDF installed)
  3. e-mail it to your Kindle address (I also uploaded it to my dropbox – so I can get it later on) and you are done!

(BTW, dropbox is an amazing thing – if you use multiple computers/OS, you have to try this thing. If you are going to try it out (for free), help me out – use my Dropbox referral link)

So there you go – another easy way to get web-content for free on your Kindle. Go on enjoy the book now.

Blog-Notes

Add the first comment
« Older Posts