This post contains the solutions to the problems 2.7, 2.8 and 2.9 of I.E. Irodov’s “Problems in General Physics”. If you don’t really care about physics, you probably want to ignore this post. Read the rest of this entry »
Irodov Solutions (2.7-2.9)
January 18th, 2010Ruby: Watch a file for changes
December 28th, 2009I was writing a script that does numerical differentiation, which required a lot of tweaking. I thought it would be quite cool to be able to see the results of the script automatically, whenever I made a change to the script, without having to run it again.
So I quickly wrote this up:
require 'digest/md5' $md5 = '' def watch(file, timeout, &cb) $md5 = Digest::MD5.hexdigest(File.read(file)) loop do sleep timeout if ( temp = Digest::MD5.hexdigest(File.read(file)) ) != $md5 $md5 = temp cb.call(file) end end end
That is the main function. It will wait for a fixed amount of time, get a hash of the file and compare it with a previous version. If it has changed, then it executes the callback which you have specified.
Here is an example of how it can be used:
$num = 0 watch '../num_diff.rb', 1 do |file| puts "\e[31m MODIFIED: \e[0m \e[34m #{file} \e[0m (#{$num}, #{Time.now})" puts "\e[32m -- EXECUTING -- \e[0m" puts puts `ruby #{file}` puts puts "\e[32m -- FINISHED EXECUTION -- \e[0m" puts $num += 1 end
Implementation of Octave’s diag in Ruby
December 13th, 2009GNU Octave has a diag function which behaves like this:
octave> diag([1,2,3]) ans = Diagonal Matrix 1 0 0 0 2 0 0 0 3
To be more clear,
Here is an implementation of the diag function in Ruby:
# Create a diagonal matrix given a vector (or an array), # placing the entries of the vector on the diagonal of # the matrix. # For example, diag([1,2,3]) would give: # [ 1 0 0 ] # [ 0 2 0 ] # [ 0 0 3 ] # That would actually be a 2D matrix like this # [[1, 0, 0], [0, 2, 0], [0, 0, 3]] # The array returned has to be converted to a matrix by doing # Matrix[ *diag(vector) ] def diag(vector) n = vector.size matrix = [] n.times do |i| matrix << Array.new(n) { |x| (x == i) ? vector[x] : 0 } end matrix end
You can also get the code in this gist; use it as you wish.
Irodov Solutions (1.1-1.3)
November 29th, 2009This post contains the solutions to the first 3 problems of I.E. Irodov’s “Problems in General Physics”. If you don’t really care about physics, you probably want to ignore this post. Read the rest of this entry »
Finding the remainder left behind when dividing a large number
November 23rd, 2009Project Euler Problem 40
October 29th, 2009In my opinion, this was the easiest problem so far. The brute-force method is easy to implement and runs very fast.
This is the code:
$fraction = '' 1.upto(1000000) do |i| $fraction << i.to_s end def d_(n) $fraction[n-1].to_i end puts d_(1) * d_(10) * d_(100) * d_(1000) * d_(10000) * d_(100000) * d_(1000000)
It runs in slightly lesser than one second.
The last line isn’t very Ruby-ish because I copied it from the problem description page, because I’m too lazy to write it myself. That’s also why I have a d_(n) function.
Project Euler Problem 21 (Ruby)
September 24th, 2009If you intend to solve the problem on your own, I wouldn’t recommend reading any further.
Let
be defined as the sum of proper divisors of
(numbers less than
which divide evenly into
).
Ifand
, where
, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore
. The proper divisors of 284 are 1, 2, 4, 71 and 142; so
.
Evaluate the sum of all the amicable numbers under 10000.
def d(n) total = 0 1.upto n/2 do |i| if (n % i) == 0 total += i end end total end amicable_sum = 0 10000.times do |a| b = d(a) if d(b) == a && b > a amicable_sum += a + b end end puts amicable_sum
My version of isn’t as efficient as the one in the PDF you get after solving the problem, but it gets the job done.
Running it takes around 8.5 seconds on my machine. That’s not too bad for ~20 lines of code.
Fajan’s Rules
August 15th, 2009Fajan’s Rules are a set of empirical rules that are used to predict whether an inorganic compound will be ionic or covalent. Read the rest of this entry »
Velox – Random Latin Words
June 16th, 2009I’ve always had a hard time naming the photographs I post on dA. Eventually, I started using a random Latin word. But going through a huge list of words looking for something that sounds cool was still tedious. So I did what anyone else in my situation would – write a web app to make my life easy. It’s called Velox (which was one of the words it came up with while testing it). Here’s a link to it- Velox – Random Latin Words. Read the rest of this entry »
Automatically Resize Large Images with Javascript
May 24th, 2009When you allow users to submit content, and allow that content to include images, there’s always a chance that some user will submit a really large image that will break your layout. One way to solve that is to check the content for large images and resize them using a server-side program. However, sometimes, that is simply not possible. In those cases, we use what’s left- Javascript. Read the rest of this entry »
