Skip to content

Instantly share code, notes, and snippets.

View joegiralt's full-sized avatar
💣
:p

Joseph Giralt joegiralt

💣
:p
View GitHub Profile
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"
@joegiralt
joegiralt / sum_square_difference.rb
Created August 14, 2015 23:48
Project Euler 6 sum_square_difference
def sum_square_difference(num)
((1..num).map{ |elem| elem ** 2 }.inject(:+) - (1..num).inject(:+) ** 2).abs
end
puts sum_square_difference(100)
@joegiralt
joegiralt / 10001st_prime.rb
Last active August 29, 2015 14:27
Project Euler 7
#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"
@joegiralt
joegiralt / count_duplicate_items_in_array.rb
Last active August 29, 2015 14:27
count duplicate items in array ()
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
@joegiralt
joegiralt / sort_and_find_copies.rb
Created August 12, 2015 19:25
algo for pointing out copies (modified insertion sort)
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
@joegiralt
joegiralt / multiple_of_3_and_5.exs
Created August 11, 2015 01:02
Project Euler 1 -- Elixir
# 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
set = []
(1..999).each do |num|
if num % 3 == 0
set.push(num)
end
end
puts set
sum = 0
for num in set
@joegiralt
joegiralt / gist:601d95496c49b4988fa2
Created January 21, 2015 21:38
n choose k Ruby
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
@joegiralt
joegiralt / gist:191f39262ee379ab91da
Created October 8, 2014 21:40
scribbles magic squares
hash = {}
perfect_sqrs = []
(1..300).each do |num|
perfect_sqrs << (num**2)
end
find_partition = lambda do |elts|
p "ELTS: #{elts}"
if elts.empty?
[[]]
@joegiralt
joegiralt / build-erlang-r16b_and_elixir0-12-5.sh
Last active August 29, 2015 13:57 — forked from vanhalt/build-erlang-r16b_and_elixir093.sh
Builds Erlang r16b & Elixir 0.12.5
#!/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