Skip to content

Instantly share code, notes, and snippets.

View joegiralt's full-sized avatar
💣
:p

Joseph Giralt joegiralt

💣
:p
View GitHub Profile
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 / 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
@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 / 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 / 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 / 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)
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 / summation_of_primes.rb
Created August 15, 2015 16:37
Project Euler 10
def summation_of_primes(up_to_num)
puts (1..up_to_num).map{|num| num if Prime.prime?(num)}.compact!.inject(:+)
end
@joegiralt
joegiralt / clue.rb
Last active August 29, 2015 14:27
hint for puzzle for yuriy
grid = [[1, 2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30, 31, 32],
[33, 34, 35, 36, 37, 38, 39, 40],
[41, 42, 43, 44, 45, 46, 47, 48],
[49, 50, 51, 52, 53, 54, 55, 56],
[57, 58, 59, 60, 61, 62, 63, 64]]
grid.each do |elem_arr|
puts "#{elem_arr[0]}, #{elem_arr[1]}"
@joegiralt
joegiralt / tictactoe_oop.rb
Created August 22, 2015 20:01
OOP implementation of tic tac toe
class Board
attr_accessor :pos_1, :pos_2, :pos_3, :pos_4, :pos_5, :pos_6, :pos_7, :pos_8, :pos_9
def initialize(pos_1, pos_2, pos_3, pos_4, pos_5, pos_6, pos_7, pos_8, pos_9)
@pos_1 = pos_1
@pos_2 = pos_2
@pos_3 = pos_3
@pos_4 = pos_4
@pos_5 = pos_5
@pos_6 = pos_6