Skip to content

Instantly share code, notes, and snippets.

View edalorzo's full-sized avatar

Edwin Dalorzo edalorzo

View GitHub Profile
@edalorzo
edalorzo / HaskellEuler1.hs
Created November 24, 2012 04:49
Project Euler - Problem #1
module Euler.Problem1 where
-- | Given a maximum integer m and a factor n calculates the sum
-- of all divisors of the given factor up to m.
sumDivisible :: Int -> Int -> Int
sumDivisible m n = n * (p * (p + 1)) `div` 2 where p = m `div` n
result = (sumDivisible 999 3) + (sumDivisible 999 5) - (sumDivisible 999 15)
solve::String
@edalorzo
edalorzo / ScalaEuler1.scala
Created November 24, 2012 05:32
Project Euler-Problem 1
/**
* Given a maximum integer m and a factor n calculates the sum
* of all divisors of the given factor up to m.
*
* @param m maximum integer
* @param n given factor (i.e. 3 or 5)
* @return the sum of all divisors.
*/
def sumDivisible(m: Int, n:Int): Int = {
@edalorzo
edalorzo / PythonEuler1.py
Created November 24, 2012 05:46
Project Euler-Problem 1
def sumDivisible(m,n):
"""
Given a maximum integer m and a factor n, calculates the sum
of all divisors of n up to m.
"""
p = m // n
return n * (p * (p + 1)) // 2
def solve():
@edalorzo
edalorzo / RubyEuler1.rb
Created November 24, 2012 06:16
Project Euler-Problem 1
# Given a maximum integer m and a factor n, calculates the sum
# of all divisors of n up to m.
def sumDivisible(m,n)
p = m / n
n * (p * (p + 1)) / 2
end
def solve
result = sumDivisible(999,3) + sumDivisible(999,5) - sumDivisible(999, 15)
@edalorzo
edalorzo / PythonEuler4.py
Created November 25, 2012 02:52
Project Euler-Problem #4
def is_palindrome(n):
text = str(n)
return text == text[::-1]
def palindromes():
for j in range(100,1000):
for k in range(100,1000):
product = j * k
@edalorzo
edalorzo / PythonEuler8.py
Created November 25, 2012 03:10
Project Euler-Problem #8
import StringIO
def get_big_number():
number = """
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
@edalorzo
edalorzo / PythonEuler13.py
Created November 25, 2012 03:11
Project Euler-Problem #13
import StringIO
def get_big_numbers():
number = """
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
@edalorzo
edalorzo / PythonEuler14.py
Created November 25, 2012 03:14
Project Euler-Problem #14
def collatz(start=1):
n = start
while n!= 1:
n = n / 2 if not n % 2 else 3 * n + 1
yield n
def count(limit=13):
n = 2
while n < limit:
@edalorzo
edalorzo / PythonEuler16.py
Created November 25, 2012 03:16
Project Euler-Problem #16
if __name__ == '__main__':
number = sum( [int(n) for n in str(2 ** 1000)] )
print "The sum of the digits of the number 2^1000 is: %d" % number
@edalorzo
edalorzo / PythonEuler48.py
Created November 25, 2012 03:18
Project Euler-Problem #48
def next_number(stop=1000):
n = 1
while n <= stop:
yield n ** n
n += 1
if __name__ == '__main__':
result = str(sum(next_number()))[-10:]
print "The last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000 is: %s" % result