Created
October 29, 2012 01:12
-
-
Save arn-e/3970833 to your computer and use it in GitHub Desktop.
un abridged code.rb
This file contains hidden or 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 binary_search(item,array,min = 0,max = array.length, half = (min + max) / 2) | |
return min if item == array[min] | |
return -1 if (item < array[0] || item > array[array.length-1]) || (min == max - 1) | |
item < array[half] ? binary_search(item,array,min,half) : binary_search(item,array,half,array.length) | |
end | |
def prime_factors(n, factors = [], f = 2) | |
return factors.push(f) if n == f | |
n % f == 0 ? prime_factors(n / f, factors.push(f)) : prime_factors(n, factors, f + 1) | |
end | |
def is_fibonacci?(i) | |
a,b=0,1 | |
until b >= i | |
a,b=b,a+b | |
return true if b == i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment