This file contains hidden or 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 special_pythagorean_triplet(num) | |
(1..(num/2)).to_a.combination(3).to_a.each do |set| | |
sort_set = set.sort! | |
triplet = (sort_set[0] ** 2 + sort_set[1]**2) == sort_set[2]**2 | |
equals_1000 = (sort_set.inject(:+) == num) | |
if triplet && equals_1000 | |
answer = sort_set.inject(:*) | |
else | |
answer = "no special triplet" |
This file contains hidden or 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 sum_square_difference(num) | |
((1..num).map{ |elem| elem ** 2 }.inject(:+) - (1..num).inject(:+) ** 2).abs | |
end | |
puts sum_square_difference(100) |
This file contains hidden or 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
#Functional | |
require 'prime' | |
def find_10001_prime(array) | |
# array[0] incrementor | |
# array[1] number of time incremetor was prime | |
prime_but_not_10001 = Prime.instance.prime?(array[0]) && array[1] != 10001 | |
prime_10001 = Prime.instance.prime?(array[0]) && array[1] == 10001 | |
if prime_but_not_10001 | |
puts "#{array} prime_but_not_10001" |
This file contains hidden or 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
array = ['jim','jack','joe','larry','larry','larry','larry','larry', 'joe'] | |
def count_items(array) | |
hash = {} | |
array.each do |name| | |
if hash[name] | |
hash[name] = hash[name] + 1 | |
else | |
hash[name] = 1 |
This file contains hidden or 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 sort_and_find_copies(array) | |
array.each_with_index do |element, idx| | |
previous_idx = idx - 1 | |
while previous_idx >= 0 | |
puts "#{array[previous_idx]} is a copy!" if array[previous_idx] == element | |
break if array[previous_idx] <= element | |
array[previous_idx + 1] = array[previous_idx] | |
previous_idx -= 1 | |
end | |
array[previous_idx + 1] = element |
This file contains hidden or 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
# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
# Find the sum of all the multiples of 3 or 5 below 1000. | |
sum = Enum.sum Enum.filter( | |
Enum.to_list(1..999), | |
fn(x) -> | |
rem(x, 3) == 0 or rem(x, 5) == 0 | |
end | |
) | |
IO.puts sum |
This file contains hidden or 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
set = [] | |
(1..999).each do |num| | |
if num % 3 == 0 | |
set.push(num) | |
end | |
end | |
puts set | |
sum = 0 | |
for num in set |
This file contains hidden or 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
class Integer | |
def choose(k) | |
return 0 if (k > self) | |
n = self | |
r = 1 | |
1.upto(k) do |d| | |
r *= n | |
r /= d | |
n -= 1 | |
end |
This file contains hidden or 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
hash = {} | |
perfect_sqrs = [] | |
(1..300).each do |num| | |
perfect_sqrs << (num**2) | |
end | |
find_partition = lambda do |elts| | |
p "ELTS: #{elts}" | |
if elts.empty? | |
[[]] |
This file contains hidden or 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
#!/bin/bash | |
# Pull this file down, make it executable and run it with sudo | |
# wget https://raw.github.com/gist/5487621/build-erlang-r16b.sh | |
# chmod u+x build-erlang-r16b.sh | |
# sudo ./build-erlang-r16b.sh | |
if [ $(id -u) != "0" ]; then | |
echo "You must be the superuser to run this script" >&2 | |
exit 1 | |
fi |