ProjectEuler.net problem #1 “Find the sum of all the multiples of 3 or 5 below 1000.”
The solution below is a brute force method written in python that checks to see if the number is cleanly divisible by three or five and adds that number to an array. then checks the arrays for any duplicates (e.g. 15 is divisible by both 3 and 5) removes any duplicates then totals the array for the final answer.
The following code uses a function found on this site.
def unique(s):
n = len(s)
if n == 0:
return []
u = {}
try:
for x in s:
u[x] = 1
except TypeError:
del u
else:
return u.keys()
try:
t = list(s)
t.sort()
except TypeError:
del t
else:
assert n > 0
last = t[0]
lasti = i = 1
while i < n:
if t[i] != last:
t[lasti] = last = t[i]
lasti += 1
i += 1
return t[:lasti]
u = []
for x in s:
if x not in u:
u.append(x)
return u
Numbers = []
i = 1
while i<1000:
if (i%3==0):
Numbers.append(i)
if (i%5==0):
Numbers.append(i)
i=i+1
print i
Numbers = unique(Numbers)
ans = 0
for j in Numbers:
ans=ans+j
print ans
Link to this post!