Created
December 10, 2012 00:58
-
-
Save ciniglio/4247751 to your computer and use it in GitHub Desktop.
Greplin challenge p2
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 fib(n) | |
@a ||= [1,1] | |
(0..n).each do |i| | |
@a[i] = @a[i-1] + @a[i-2] unless @a[i] | |
end | |
@a[n] | |
end | |
def fib_after(n) | |
f = 0 | |
i = 0 | |
while f <= n do | |
f = fib(i) | |
i += 1 | |
end | |
return f | |
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
# not the most efficient prime detection | |
def prime?(n) | |
2.upto(Math.sqrt(n)) do |d| | |
return false if n % Float(d) == 0 | |
end | |
return true | |
end | |
def prime_fib_after(n) | |
f = fib_after(n) | |
while !prime? f do | |
f = fib_after(f) | |
end | |
return f | |
end | |
def prime_divisors(n) | |
a = [] | |
2.upto(Math.sqrt(n)) do |d| | |
a << d if n % Float(d) == 0 && prime?(d) | |
end | |
return a | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment