Skip to content

Instantly share code, notes, and snippets.

View andrewkowalik's full-sized avatar
👋

Andrew Kowalik andrewkowalik

👋
View GitHub Profile
@andrewkowalik
andrewkowalik / gist:5612602
Created May 20, 2013 14:33
Calculate powers using ruby recursively
def powers_rec(num,power)
return num if power == 1
total = num * powers_rec(num,power-1)
end
@andrewkowalik
andrewkowalik / gist:5612545
Created May 20, 2013 14:21
Ruby implementation of quick_sort.
def quick_sort(nums)
return nums if nums.length == 1
mid = nums.length / 2
left = []
right = []
0.upto(nums.length-1) do |i|
if nums[i] <= nums[mid]
left << nums[i]
@andrewkowalik
andrewkowalik / gist:5610034
Created May 20, 2013 02:09
Generates an array of prime numbers
def prime(num)
return [] if num < 2
primes = []
2.upto(num) { |x| primes[x] = x}
2.upto(Math.sqrt(num)) { |x| primes[x] ? (x*x).step(num,x) { |x2| primes[x2] = nil } : next}
primes.compact
end
@andrewkowalik
andrewkowalik / gist:5608882
Created May 19, 2013 20:36
Recursive range between two numbers passed as parameters
def range(start_num,end_num)
return [end_num] if start_num == end_num
nums = []
if start_num > end_num
nums += [start_num] + range(start_num-1,end_num)
else
nums += [start_num] + range(start_num+1,end_num)
end
nums
end