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

Function solution

project_euler/problem_005/sol1.py:17–66  ·  view source on GitHub ↗

Returns the smallest positive number that is evenly divisible (divisible with no remainder) by all of the numbers from 1 to n. >>> solution(10) 2520 >>> solution(15) 360360 >>> solution(22) 232792560 >>> solution(3.4) 6 >>> solution(0) Traceback (mos

(n: int = 20)

Source from the content-addressed store, hash-verified

15
16
17def solution(n: int = 20) -> int:
18 """
19 Returns the smallest positive number that is evenly divisible (divisible
20 with no remainder) by all of the numbers from 1 to n.
21
22 >>> solution(10)
23 2520
24 >>> solution(15)
25 360360
26 >>> solution(22)
27 232792560
28 >>> solution(3.4)
29 6
30 >>> solution(0)
31 Traceback (most recent call last):
32 ...
33 ValueError: Parameter n must be greater than or equal to one.
34 >>> solution(-17)
35 Traceback (most recent call last):
36 ...
37 ValueError: Parameter n must be greater than or equal to one.
38 >>> solution([])
39 Traceback (most recent call last):
40 ...
41 TypeError: Parameter n must be int or castable to int.
42 >>> solution("asd")
43 Traceback (most recent call last):
44 ...
45 TypeError: Parameter n must be int or castable to int.
46 """
47
48 try:
49 n = int(n)
50 except TypeError, ValueError:
51 raise TypeError("Parameter n must be int or castable to int.")
52 if n <= 0:
53 raise ValueError("Parameter n must be greater than or equal to one.")
54 i = 0
55 while 1:
56 i += n * (n - 1)
57 nfound = 0
58 for j in range(2, n):
59 if i % j != 0:
60 nfound = 1
61 break
62 if nfound == 0:
63 if i == 0:
64 i = 1
65 return i
66 return None
67
68
69if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected