Created
December 12, 2011 00:48
-
-
Save anonymous/1463860 to your computer and use it in GitHub Desktop.
Euler Project 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
#!/usr/bin/python | |
# Find the sum of all the multiples of 3 or 5 below 1000. | |
import unittest | |
from multiprocessing import Pool | |
class main_test(unittest.TestCase): | |
def test_multisum(self): | |
self.assertEqual(multisum(3, 10), 18) # 3,6,9 | |
self.assertEqual(multisum(5, 10), 5) # 5 | |
self.assertEqual((multisum(3, 10) + | |
multisum(5, 10)), 23) # 3,5,6,9 | |
# the solver | |
def multisum(multiple, limit): | |
value = 0 | |
result = [ ] | |
while (value + multiple) < limit: # preflight check | |
value += multiple | |
result.append(value) | |
return result | |
if __name__ == "__main__": | |
#unittest.main() | |
pool = Pool(processes=2) | |
# Setting up the processes and process pool. | |
threes = pool.apply_async(multisum, (3, 1000)) | |
fives = pool.apply_async(multisum, (5, 1000)) | |
# because each process is running without a shared state, it's | |
# required to find a union of the two results. "multiples of 3 OR | |
# 5 below 1000" | |
result_set = set.union(set(threes.get()), set(fives.get())) | |
print sum(result_set) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment