This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def SumMultiplesOfThreeOrFive(number): | |
return sum(i for i in range(1,number) if (i % 3 == 0 or i % 5 == 0)) | |
#for i in range(1,number): | |
# if (i % 3 == 0 or i % 5 == 0): | |
# sum += i | |
#return sum | |
class TestSumMultiplesOfThreeOrFive(unittest.TestCase): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def EvenFibonacciNumbers(limit): | |
#Find all even fibonacci numbers | |
fibonacciSeries = FibonacciGenerator(limit) | |
return sum(i for i in fibonacciSeries if i % 2 == 0) | |
def FibonacciGenerator(limit): | |
a = 0 | |
b = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from math import sqrt | |
def isPrime(i): | |
if (i == 2 or i == 3): | |
return True | |
else: | |
for j in range(3, int(sqrt(i)+1)): | |
if ( i % j == 0): | |
return False | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def LargestIntDividesEleven(numberOfDigits): | |
largestInt = 10**(numberOfDigits)-1 | |
return largestInt - (largestInt % 11) | |
def reverse(number): | |
''' | |
This method does not speed things up, but it does provide a rather | |
interesting way of reversing any integers without converting it to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def LongestZeroSequence(n): | |
''' | |
Input: n is a string of a binary sequence | |
Output: the length of the longest consecutive zero sequence | |
when encounter 1, stop counting and save the max sequence so far. | |
output the max sequence | |
''' | |
maxSequence = 0 | |
seq = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sieve of Eratosthenes | |
# Code by David Eppstein, UC Irvine, 28 Feb 2002 | |
# http://code.activestate.com/recipes/117119/ | |
def PrimesGenerator(): | |
""" | |
Generate an infinite sequence of prime numbers. | |
""" | |
# Maps composites to primes witnessing their compositeness. | |
# This is memory efficient, as the sieve is not "run forward" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def drawdown2(series): | |
MDD = 0 | |
peak = -99999 | |
length = len(series) | |
DD = np.zeros(length) | |
for i in range(length): | |
if (series[i] > peak): # peak will be the maximum value seen so far (0 to i) |