Skip to content

Instantly share code, notes, and snippets.

View abdollar's full-sized avatar

Abdul Chaudhry abdollar

View GitHub Profile
@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 / 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 / Ruby
Created December 21, 2011 05:40
10001st Prime
require 'prime'
Prime.take(10001).last
@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 / 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 / Bench
Created January 8, 2012 18:42 — forked from igrigorik/Bench
# ruby getimage.rb -e production -c config/getimage.rb
@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 / 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:1665105
Created January 23, 2012 19:32
Octal permissions
ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'
@abdollar
abdollar / gist:1665112
Created January 23, 2012 19:33
tcpdump dns queries
sudo tcpdump -vi eth0 port 53