This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def powers_rec(num,power) | |
return num if power == 1 | |
total = num * powers_rec(num,power-1) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder