Skip to content

Instantly share code, notes, and snippets.

@georgepradhan
georgepradhan / gist:5810047
Last active December 18, 2015 16:18
Project Euler #11
# Problem taken from: https://projecteuler.net/problem=11
# In the 2020 grid below, four numbers along a diagonal line have been marked in red.
# The product of these numbers is 26 63 78 14 = 1788696.
# What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 2020 grid?
n = '08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
@georgepradhan
georgepradhan / gist:5862931
Created June 25, 2013 22:11
Trying (not yet succeeding) to make a prime factorization method.
# Goal: Create a method that takes an input number and creates a hash of its prime factors and their exponents.
# e.g. prime_factorization(18) => { 2=>1, 3=>2 } #i.e. 2 * 3^2
# Logic: Take a number and divide by each prime number, and divide the resulting quotient by prime numbers, etc. to get the total factorization.
# e.g. 100 = 50 * 2.
# 50 = 25 * 2.
# 25 = 5 * 5.
# Prime factorization of 100 = 2*2*5*5 or 2^2*5^2.
factors = Hash.new(0)
@georgepradhan
georgepradhan / gist:5863394
Created June 25, 2013 23:26
Project Euler #12 - attempt 1
# The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
# 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
# Let us list the factors of the first seven triangle numbers:
# 1: 1
# 3: 1,3
# 6: 1,2,3,6
# 10: 1,2,5,10
@georgepradhan
georgepradhan / gist:5863403
Created June 25, 2013 23:28
Project Euler #12 - attempt 2
# See my earlier Gist, Project Euler #12 - attempt 1, for context.
puts Time.now
def is_prime?(n)
return true if n == 2
for i in 2..((n**0.5).ceil)
# puts "Checking #{i}... "
if n % i == 0
return false
@georgepradhan
georgepradhan / gist:5867679
Created June 26, 2013 14:12
Project Euler #12 - attempt 3 Finally works. Solution takes about 15 seconds so could be optimized further, but not bad.
def tri_num(n)
(n)*(n+1)/2 #formula for the nth triangle number. http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/runsums/triNbProof.html
end
def num_factors(n) #brute force method..
factors = 0
for i in 1..n
factors += 1 if n % i == 0
end
factors
@georgepradhan
georgepradhan / gist:5877812
Created June 27, 2013 16:13
Recursive method to take an array that potentially contains other arrays and return an array of only individual elements.
@result = []
def flatten(array)
array.each do |element|
if element.respond_to?(:each)
flatten(element)
else
@result.push(element)
end
end
@georgepradhan
georgepradhan / gist:6025666
Created July 18, 2013 00:04
numbers in words - first try
## Pseudo-code ##
# Initialize constant (lookup hash) with digits as keys,
# words as values. e.g. { 1 => one, 2 => two }. This is for 1-9
# Initialize constant (lookup hash) with numbers as keys,
# words as values. e.g. { 10 => ten, 11 => eleven }. This is for 10-19.
# Initialize constant (lookup hash) with 10's place digit as keys,
# prefixes as values. e.g. { 2 => twenty, 3 => thirty }. This is for 20-99.
@georgepradhan
georgepradhan / gist:6025681
Last active December 19, 2015 22:09
numbers in words - 2nd try
NUMBER_WORDS =
{
90 => 'ninety' ,
80 => 'eighty' ,
70 => 'seventy' ,
60 => 'sixty' ,
50 => 'fifty' ,
40 => 'forty' ,
30 => 'thirty' ,
20 => 'twenty' ,
# Part 1 tests
car = Car.new({:color => 'red'})
p car.drive == :driving # => true
p car.brake == :stopped # => true
p car.instance_variables.sort == [:@color, :@wheels, :@status].sort
p (car.needs_gas? == true || car.needs_gas? == false) #this sometimes yields false, strangely..
p [true, false].include?(car.needs_gas?) #seems to work
car.needs_gas? #seems to always return true or false, so i'm not sure what's going on above..
# Part 1 tests
@georgepradhan
georgepradhan / index.html
Last active December 21, 2015 03:39 — forked from dbc-challenges/index.html
DBC Phase 2 Practice Assessment Part 3
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.jsdelivr.net/normalize/2.1.0/normalize.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lato:100,900">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.0.2/css/font-awesome.min.css">
</head>