Skip to content

Instantly share code, notes, and snippets.

@jshawl
Last active August 29, 2015 14:10
Show Gist options
  • Save jshawl/d7cb5113cc679d2614da to your computer and use it in GitHub Desktop.
Save jshawl/d7cb5113cc679d2614da to your computer and use it in GitHub Desktop.
def count_to limit, index = 1, nums = []
if nums.empty?
nums = [1,2]
end
if index == limit
return (nums)
else
next_digit = nums[-2] + nums[-1]
if next_digit <= limit
nums << next_digit
end
index += 1
count_to limit, index, nums
end
end
def pluck_even nums, length = -1
if length == -1
length = nums.length
end
if length == 0
return nums
end
length -= 1
if nums[length] % 2 != 0
nums.delete_at(length)
end
pluck_even( nums, length )
end
def sum arr, n = 0, sum = 0
if n == arr.length
return sum
else
sum += arr[n]
end
next_n = n + 1
return sum(arr, next_n, sum)
end
p sum( pluck_even( count_to( 1_000 ) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment