Skip to content

Instantly share code, notes, and snippets.

@arn-e
Created October 29, 2012 01:12
Show Gist options
  • Save arn-e/3970833 to your computer and use it in GitHub Desktop.
Save arn-e/3970833 to your computer and use it in GitHub Desktop.
un abridged code.rb
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