Skip to content

Instantly share code, notes, and snippets.

@photomattmills
photomattmills / NaNoWriter.rb
Created November 3, 2010 17:55
A simple ruby script for writing NaNoWriMo entries.
words = 0
puts "How many words today?"
nwords = gets.chomp.to_i
word_string = ""
todays = File.new("NaNoWriMo" + Time.now.strftime('%b-%d'), "w" )
while words < nwords do
word_string << gets
words = word_string.scan(/\w+/).length
todays.write(word_string)
end
@photomattmills
photomattmills / primenumber.rb
Created November 27, 2010 18:07
class for testing primes.
class PrimeNumber
def self.is_prime(n)
(2..Math.sqrt(n).to_i).each { |test| return false if n.modulo(test) == 0 }
return true
end
end
@photomattmills
photomattmills / gist:743471
Created December 16, 2010 14:50
packages on my slicehost
adduser install
apt install
apt-utils install
aptitude install
base-files install
base-passwd install
bash install
binutils install
bsd-mailx install
bsdmainutils install
@photomattmills
photomattmills / walk_multidimensional_array.rb
Created December 18, 2010 00:16
This method will go through all the children in a multidimensional array.
array = [:first,:first,[:second,:second,[:third,:third,[:fourth,:fourth]]]]
def walk(array)
if array.class == Array
array.each do |item|
if item.class == Array
item.each { |i| walk(i) }
else
puts item
end
@photomattmills
photomattmills / importer.rb
Created February 23, 2011 22:29
simple script to convert an RSS feed to jekyll files.
# from the standard library :
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'time'
# gem install to_slug if you don't have it already.
require 'to_slug'
source = "" # put your feed url here
@photomattmills
photomattmills / chkrootkit.sh
Created March 15, 2011 21:36
chkrootkit shell script
curl http://www.reznor.com/tools/chkrootkit.tar.gz > chkrootkit.tar.gz
if test $(openssl md5 chkrootkit.tar.gz | cut -d" " -f2) == $(curl http://www.reznor.com/tools/chkrootkit.md5 | cut -d" " -f1);
then
tar -xvf chkrootkit.tar.gz
cd chkrootkit-*
sudo ./chkrootkit
fi
shipment = Shipment.create(:order_id => order.id) #assume we have the order loaded already
shipment.save #this was actually unnecesary
shipment = Shipment.create(:order_id => order.id) #assume we have the order loaded already
shipment = Shipment.find(shipment.id)
shipment.save
irb(main):001:0> def a; return "a"; end;
irb(main):002:0* "Match".match /#{a}/
=> #<MatchData "a">

So this is a fast way to test primality of a number, called a sieve. The PRIMES constant is a list of all the prime numbers below sqrt(1000000000). Computing the values in the sieve actually takes a relatively long time, on the order of seconds; someone cleverer than me could probably make it recursive, such that it would construct itself on the fly, but all of my attempts to do this took much longer than a constant.

Also, protip, don't do expensive calculations like sqrt(n) more than once if you can avoid it. That actually was a huge performance gain, the reason this is an order of magnitude (or more) faster and not just 70% faster.