Skip to content

Instantly share code, notes, and snippets.

View abdollar's full-sized avatar

Abdul Chaudhry abdollar

View GitHub Profile
@abdollar
abdollar / gist:1653897
Created January 21, 2012 20:29
Base62 Ruby
SIXTYTWO = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
def to_s_62(i)
return '0' if i == 0
s = ''
while i > 0
s << SIXTYTWO[i.modulo(62)]
i /= 62
end
s.reverse
@abdollar
abdollar / gist:1590755
Created January 10, 2012 19:44 — forked from jstorimer/hilong.rb
hilong -- A simply utility to show character counts for each line of input and highlight lines longer than 80 characters.
#!/usr/bin/env ruby
# A simply utility to show character counts for each line of input and
# highlight lines longer than 80 characters.
#
# Written as an example for http://jstorimer.com/2011/12/12/writing-ruby-scripts-that-respect-pipelines.html
#
# Examples:
#
# $ hilong Gemfile
@abdollar
abdollar / Bench
Created January 8, 2012 18:42 — forked from igrigorik/Bench
# ruby getimage.rb -e production -c config/getimage.rb
@abdollar
abdollar / fib.rb
Created December 21, 2011 06:11 — forked from peterc/fib.rb
fib=->(n){_=5**0.5;(((1+_)**n-(1-_)**n)/(2**n*_)).to_i}
@abdollar
abdollar / sroot.rb
Created December 21, 2011 06:10 — forked from peterc/sroot.rb
Square root calculation using Newton-Raphson
# Square root calculation using Newton-Raphson
# from Practical Programming (1968)
a = 256
x = (1 + a) / 2.0
loop do
ox = x
x = (x + a.to_f / x) / 2.0
@abdollar
abdollar / Ruby
Created December 21, 2011 05:40
10001st Prime
require 'prime'
Prime.take(10001).last
@abdollar
abdollar / gist:1504697
Created December 21, 2011 05:19 — forked from dgrijalva/gist:1198745
openssl notes
# Convert ssh-rsa key to pem
ssh-keygen -f infile.pub -e -m PKCS8 > outfile.pem
# Encrypt a file using public key pem
openssl rsautl -encrypt -inkey public.pem -pubin -in file.txt -out file.ssl
# Decrypt using private key
openssl rsautl -decrypt -inkey private.pem -in file.ssl -out decrypted.txt
@abdollar
abdollar / gist:1470338
Created December 13, 2011 03:08
Find the difference between the sum of the squares and the square of the sum
#The sum of the squares of the first ten natural numbers is,
#12 + 22 + ... + 102 = 385
#The square of the sum of the first ten natural numbers is,
#(1 + 2 + ... + 10)2 = 552 = 3025
#Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
#Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum
ruby-1.9.2-p180 :004 > (1..10).to_a.reduce(:+)**2 - (1..10).to_a.map { |x| x**2 }.reduce(:+)
=> 2640
ruby-1.9.2-p180 :005 > (1..100).to_a.reduce(:+)**2 - (1..100).to_a.map { |x| x**2 }.reduce(:+)
@abdollar
abdollar / Ruby
Created December 13, 2011 02:47
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
(400..2432902008176640000).step(20).each do |i|
val = (1..20).to_a.select{|x| i%x==0}&(1..20).to_a
if val.count == 20
puts i
exit
end
end
Use the product of the LCM's
@abdollar
abdollar / gist:1463968
Created December 12, 2011 01:09
Find the largest palindrome made from the product of two 3-digit numbers
def palindrome?(str); str.reverse == str; end
max = 0
900.upto(999) { |a|
a.upto(999) { |b|
max = [max, a * b].max if palindrome?((a * b).to_s)
}
}
max