Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed fibonacci challenge #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion lib/fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@
# e.g. 1st fibonacci number is 1
# ....
# e.g. 6th fibonacci number is 8
# def fibonacci(n)
# if n.nil?
# raise ArgumentError, "Cannot be nil"
# end
#
# if n.to_s.include?("-")
# raise ArgumentError, "Cannot be negative"
# end
# # From 0 to N
# # Each number is the sum of the two numbers before it
# # Generate array of numbers
# # Look at prior to numbers, add them together
# # 0 and 1 do not have prior two entries to add together
# numbers = [*0..n]
# # Start at index 2, manually add in 0 and 1
# result = [0, 1]
# i = 2
# while i < numbers.length
# # First number
# a = result[i - 1]
# # Second number
# b = result[i - 2]
# # Push sum of a and b to result array
# result << (a + b)
# # Increase iterator
# i += 1
# end
# # Return result at n index
# return result[n]
# end

# Linear runtime , O(1) space complexity

# Recursive attempt

def fibonacci(n)
raise NotImplementedError
if n.nil?
raise ArgumentError, "Cannot be nil"
end

if n.to_s.include?("-")
raise ArgumentError, "Cannot be negative"
end

# When n is less than 0 or 1, number is returned
if n < 2
return n
# 0 returns 0, 1 returns 1
end
# Otherwise, returns sum of prior two numbers
# Enters recursion
return fibonacci( n-1 ) + fibonacci( n-2)

end

# Time Complexity
# Recursive - Takes much longer :(