"def isComposite(x):\n",
" \"\"\"Returns whether or not the given number x is composite.\n",
"\n",
" A composite number has more than 2 factors.\n",
" A natural number greater than 1 that is not prime is called a composite number.\n",
" Note, the number 1 is neither prime nor composite.\n",
"\n",
" For example:\n",
" - Calling isComposite(9) will return True\n",
" - Calling isComposite(22) will return True\n",
" - Calling isComposite(3) will return False\n",
" - Calling isComposite(41) will return False\n",
composite_numbers = [4, 6, 8, 9, 10, 12, 14, 15, 16,
18, 20, 21, 22, 24, 25, 26, 27,
28, 30, 32, 33, 34, 35, 36,
38, 39, 40, 42, 44, 45, 46, 48,
49, 50, 51, 52, 54, 55, 56, 57,
58, 60, 62, 63, 64, 65, 66, 91, 93]
for i in composite_numbers:
assert_true(isComposite(i), str(i) + ' is composite')
not_composite_numbers = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89]
for i in not_composite_numbers:
assert_true(not(isComposite(i)), str(i) + ' is not composite')
assert_true(len(isComposite.__doc__) > 1, "there is no docstring for isComposite")
print("Success!")
When i go to to test my solution i get an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-037cd1a71006> in <module>
21
22 #test existence of docstring
---> 23 assert_true(len(isComposite.__doc__) > 1, "there is no docstring for isComposite")
24 print("Success!")
TypeError: object of type 'NoneType' has no len()
What I have tried:
def isComposite(x):
if (x <= 1):
return False
if (x <=3):
return False
if (x % 2 == 0 or x % 3 == 0):
return True
i = 5
while (i * i <= x):
if (x % i == 0 or x % (i + 2) == 0):
return True
i = i + 6
return False
print(isComposite(9))
print(isComposite(22))
print(isComposite (3))
print(isComposite (41))
my output is:
true
true
false
false