Skip to content

Instantly share code, notes, and snippets.

@reednj
reednj / mult.md
Last active September 25, 2022 06:23

Multiplication from Scratch

Imagine you need to implement (integer) mulitplication in code. Maybe you are on a system which doesn't have it or something. How to do this, and what is the minimal set of operators that are required?

Repeated addition

The most obvious way to do multiplition is through repeated addition. To get the answer to 56 x 67 you add 56 to itself 67 times (or 67, 56 times - the order doesn't matter).

This is simple to implement if we assume for the moment that both a and b are positive (we will deal with negative integers later)

aback
abase
abate
abbey
abbot
abhor
abide
abled
abode
abort
@reednj
reednj / screens.rb
Last active December 8, 2016 23:42
#!/usr/bin/env ruby
#
# screens.rb
# @reednj, 2016-12-06
#
# This script will take a screenshot of the desktop on up to two monitors, and save it in the
# given location (usually Dropbox). It will only take a screenshot if the user is active.
#
# If there is already a screenshot that exists for that day, it will _sometimes_ overwrite it
# and make a new one. This is an attempt to get a screenshot at a random time each day.
@reednj
reednj / is_a!.rb
Created April 1, 2016 02:30
is_a! extension method for ruby
class Object
def is_a!(t, name = nil)
if !is_a? t
if name.nil?
raise "expected #{t} but got #{self.class}"
else
raise "#{name} requires #{t} but got #{self.class}"
end
end
end
#!/bin/sh
# create a gem templete
bundle gem gem_name_goes_here
# run the unit tests
bundle exec rake test
# upload the gem to rubygems
bundle exec rake release
@reednj
reednj / benchmark.rb
Last active March 11, 2016 05:48
a small ruby method to benchmark a block of code
def benchmark(count = 1, &block)
start = Time.now
count.times do |i|
yield
end
duration = Time.now - start
per_sec = count / duration
@reednj
reednj / worker.rb
Last active March 17, 2016 02:13
A ruby background work with a monitoring thread to kill the worker after a timeout
class WorkerThread
def initialize
end
def start(options = nil)
raise 'background_task needs a block' unless block_given?
options ||= {}
@reednj
reednj / paint-colors.md
Created October 16, 2015 10:55
colors used in paint on windows 7

colors used in paint on windows 7

row one

  • #000000
  • #7F7F7F
  • #880015
  • #ED1C24
  • #FF7F27
  • #FFF200
@reednj
reednj / class.js
Last active December 31, 2015 02:38
Basic javascript Class object. No inheritance or anything like that but allows the nice encapsulation of properties and methods.
// The MIT License (MIT)
//
// Copyright (c) 2013, Nathan Reed
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
using System;
using System.Collections.Generic;
using System.Text;
namespace Temp
{
class Program
{
static void Main(string[] args)
{