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

Function compute_nums

project_euler/problem_046/sol1.py:67–107  ·  view source on GitHub ↗

Returns a list of first n odd composite numbers which do not follow the conjecture. >>> compute_nums(1) [5777] >>> compute_nums(2) [5777, 5993] >>> compute_nums(0) Traceback (most recent call last): ... ValueError: n must be >= 0 >>> comput

(n: int)

Source from the content-addressed store, hash-verified

65
66
67def compute_nums(n: int) -> list[int]:
68 """
69 Returns a list of first n odd composite numbers which do
70 not follow the conjecture.
71 >>> compute_nums(1)
72 [5777]
73 >>> compute_nums(2)
74 [5777, 5993]
75 >>> compute_nums(0)
76 Traceback (most recent call last):
77 ...
78 ValueError: n must be >= 0
79 >>> compute_nums("a")
80 Traceback (most recent call last):
81 ...
82 ValueError: n must be an integer
83 >>> compute_nums(1.1)
84 Traceback (most recent call last):
85 ...
86 ValueError: n must be an integer
87
88 """
89 if not isinstance(n, int):
90 raise ValueError("n must be an integer")
91 if n <= 0:
92 raise ValueError("n must be >= 0")
93
94 list_nums = []
95 for num in range(len(odd_composites)):
96 i = 0
97 while 2 * i * i <= odd_composites[num]:
98 rem = odd_composites[num] - 2 * i * i
99 if is_prime(rem):
100 break
101 i += 1
102 else:
103 list_nums.append(odd_composites[num])
104 if len(list_nums) == n:
105 return list_nums
106
107 return []
108
109
110def solution() -> int:

Callers 1

solutionFunction · 0.85

Calls 2

is_primeFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected