MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_001/sol3.py:13–60  ·  view source on GitHub ↗

This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3. Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 >>> solution(4) 3 >>> solution(10) 23 >>> solution(600) 83700

(n: int = 1000)

Source from the content-addressed store, hash-verified

11
12
13def solution(n: int = 1000) -> int:
14 """
15 This solution is based on the pattern that the successive numbers in the
16 series follow: 0+3,+2,+1,+3,+1,+2,+3.
17 Returns the sum of all the multiples of 3 or 5 below n.
18
19 >>> solution(3)
20 0
21 >>> solution(4)
22 3
23 >>> solution(10)
24 23
25 >>> solution(600)
26 83700
27 """
28
29 total = 0
30 num = 0
31 while 1:
32 num += 3
33 if num >= n:
34 break
35 total += num
36 num += 2
37 if num >= n:
38 break
39 total += num
40 num += 1
41 if num >= n:
42 break
43 total += num
44 num += 3
45 if num >= n:
46 break
47 total += num
48 num += 1
49 if num >= n:
50 break
51 total += num
52 num += 2
53 if num >= n:
54 break
55 total += num
56 num += 3
57 if num >= n:
58 break
59 total += num
60 return total
61
62
63if __name__ == "__main__":

Callers 1

sol3.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected