Skip to content

Instantly share code, notes, and snippets.

@amyhenning
amyhenning / collatz.rb
Last active September 1, 2018 00:57
Created a method to return the Collatz Sequence for a given n, then created a method to find the number with the longest Collatz Sqeuence between 1 and n
def collatz(n)
# set an empty arry to contain the values of the sequence
ary = []
# set initial index value to 0
index = 0
# start by pushing n into the array
ary.push(n)
# loop until the array includes the number 1, i.e. until the sequence is complete
until ary.include?(1)
@amyhenning
amyhenning / fibonacci.rb
Last active September 1, 2018 00:56
Built recursive and iterative methods to return the Fibonacci sequence up to a given n, then ran benchmarks to test performance
def recursive_fib(n)
# sets the first two values (0 and 1) by default
if n < 2
return n
# if n is greater than or equal to 2, the nth number in the fibonacci
# is the sum of the function called on n-1 and the function called on
# n-2
else
return recursive_fib(n-1) + recursive_fib(n-2)
end
@amyhenning
amyhenning / map.js
Last active September 1, 2018 00:55
Built the map method in JS
"use strict";
var _ = {
// Implements:
// https://lodash.com/docs#map
map: (array, callback) => {
return callback(array);
}
}