Esempio n. 1
0
def main():
    watch = StopWatch()
    
    max_length = 0
    for d in range(1, 1001):
        length = cnt_solutions(d)
        if length > max_length:
            print "%s -> %s" % (d, length)
            max_length = length

    watch.print_time()
Esempio n. 2
0
def test_pool():
    from euler_tools.misc import StopWatch
    watch = StopWatch()

    max_n = 300
    import multiprocessing
    count = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=count)
    answers = pool.map(work, range(max_n))

    answer = 0
    for item in answers:
        answer = lossy_add(answer, item, max_value)

    watch.print_time()
    print answer
Esempio n. 3
0
def main():
    lp = LossyPrinter(1)
    total_watch = StopWatch()

    cpu_count = multiprocessing.cpu_count()
    #~ pool = multiprocessing.Pool(processes=cpu_count)
    #~ work_units = divide_array(range(max_n), cpu_count * 10)
#~
    #~ watch = StopWatch()
    #~ map_results = pool.map(find_abundant_numbers, work_units)
    #~ watch.print_time("Map")
#~
    #~ watch = StopWatch()
    abundant_numbers = []
    for n in xrange(max_n):
        if is_abundant_naive(n):
            abundant_numbers.append(n)

    #~ for item in map_results:
        #~ abundant_numbers.extend(item)
    #~ abundant_numbers = sorted(abundant_numbers)
    #~ watch.print_time("Reduce")

    #~ len(abundant_numbers): 6965
    print "len(abundant_numbers): %s"  % len(abundant_numbers)
    if len(abundant_numbers) < 100:
        print abundant_numbers

    #~ return

    pool = multiprocessing.Pool(processes=cpu_count)
    work_units = [(abundant_numbers, n) for n in divide_array(range(max_n), cpu_count * 2)]
    #~ find_sumands(work_units[0])
    watch = StopWatch()
    map_results = pool.map(find_sumands, work_units)
    watch.print_time("Map")

    sum = reduce(lambda x, y: x + y, map_results)

    #~ sum = 0
    #~ for n in range(max_n):
        #~ lp.try_print("n: %s" % n)
        #~ if not is_sum_of_abundant_numbers(n, abundant_numbers):
           #~ sum += n

    total_watch.print_time("Total")

    print "Anwser: %s" % sum
    # correct answer: 4179871
    if sum != 4179871:
        print "THIS IS INCORRECT!"
Esempio n. 4
0
def fn_d(n):
    """
        >>> fn_d(220)
        284
        >>> fn_d(284)
        220
    """
    retval = 0
    for divisor in range(1, n):
        if n % divisor == 0:
            retval += divisor
    return retval

if __name__ == '__main__':
    import doctest
    doctest.testmod()

    from euler_tools.misc import StopWatch
    watch = StopWatch()

    max_n = 10000
    sum = 0
    for n in range(max_n):
        result = fn_d(n)
        if result != n:
            if fn_d(result) == n:
                sum += n

    watch.print_time()
    print sum