Skip to content

Instantly share code, notes, and snippets.

@r3
r3 / euler6.py
Created May 18, 2012 05:27
Project Euler 6
"""I took the liberty of changing two variable names that happen to shadow
builtin Python functions (sum and str), but the code is otherwise
unedited from what the crew produced in the meeting today.
"""
def squareOfSums(numbers):
#Bob
n = sum(numbers)
return n ** 2
@r3
r3 / Euler7.py
Created May 18, 2012 05:21
Project Euler Challenge 7
"""You'll have to forgive me some of the ugly,
I wrote this when I was new to Python, but it looks passable.
-r3
"""
def is_prime(num):
"""Checks argument for primality"""
# Optimization to determine if even
if num % 2 == 0:
@r3
r3 / euler5.py
Created May 16, 2012 04:13
Project Euler 5 solution and explanation.
"""Project Euler: Problem 5
Requires: Python 2
2520 is the smallest number that can be divided by each of the numbers from
1 to 10 without any remainder. What is the smallest positive number that is
evenly divisible by all of the numbers from 1 to 20?
This result takes a different approach to factoring than we used in class.
The code that we built is in another gist linked in the email that I sent
out. I didn't feel the need to generate prime numbers if I could let a prime
"""Project Euler: Problem 3"""
def largest_prime_factor(num):
"""Determines the largest prime factor of the given number"""
def divide_out_multiples(ix, num):
"""Removes swathes of multiples at a time. If two is a multiple,
we remove any even factors and return the result.
"""
while num % ix == 0:
num /= ix
def find_multiples(factor, end):
"""Creates a list of multiples less than a given end
value from the given factors.
"""
return range(factor, end, factor)
@r3
r3 / gist:2590147
Created May 3, 2012 22:52
Project Euler #1 (prior to refactor)
# The name doesn't well reflect what the function does (as Ilya pointed
# out), but after the refactor, this will be fixed
def find_multiples(factors, end=1000, func=sum):
"""Creates a list of multiples less than a given end
value (1000 by default) from the given factors and
applies a function to them (sum by default).
@r3
r3 / poker.py
Created April 25, 2012 00:12
Udacity code up to #16
def poker(hands):
"""Return the best hand: poker([hand,...]) => hand"""
# Recall that providing a key is essentially calling
# that function on 'hands' and sorting the objects by
# the results.
return max(hands, key=hand_rank)
def card_ranks(cards):
"""Return hand sorted by card rank"""
# Dictionary, maps between one object and another