예제 #1
0
""" This problem is here to demonstrate the structure our code will
be following. If you can run this example, you should be fine as you
go through the rest of the problems.

This problem is based off of the Project Euler problem #6. In order
to show a little more Numpy functionality, the sizes have been increased.

Links:
http://projecteuler.net/problem=6
"""


def naive():
    x = 0
    y = 0
    for i in range(1, 1001):
        x += pow(i, 2)
        y += i
    return pow(y, 2) - x

if __name__ == '__main__':
    import eulersix_test
    eulersix_test.evaluate(naive)
예제 #2
0
""" This solution uses numpy to apply (x ** 2) to each
element in the array, instead of iteration over a range
of numbers
"""
import numpy as np


def better():
    x = np.arange(1001, dtype='int64')
    sumsq = np.sum(x ** 2)
    sqsum = x.sum() ** 2

    return sqsum - sumsq

if __name__ == '__main__':
    import eulersix_test
    eulersix_test.evaluate(better)