• Source
    1. Module 2
    2. This HW deals primarily with loops and functions. It will introduce you to some interesting number theory as well. You have probably heard of prime numbers and composite numbers before, but have you ever heard of abundant numbers, or narcissistic numbers? This assignment will ask you to write code to identify different kinds of number properties.
    3.  
    4. We also want you to learn how to reuse code in this assignment, by writing and using functions. This is a basic strategy for reducing complexity in a program. Why? Because things change, and if you have the same thing in several places, when you change one, you have to change all the others. And you can’t always find them all! In order to reuse code, you must take common pieces of code and put them into functions. Failure to do so will result in loss of points.
    5.  
    6. We also want you to add comments to your code and docstrings to your functions.
    7.  
    8. You can assume that all inputs in this assignment will be positive integers.
    9.  
    10. Each function has been defined for you, but without the code. See the docstring in each function (in the starter code) for more details on what the function is supposed to do and how to write the code. It should be clear enough. In some cases, we have provided hints and examples to get you started.
    11.  
    12. ###########################################################
    13. ### EXECUTE THIS CELL BEFORE YOU TO TEST YOUR SOLUTIONS ###
    14. ###########################################################
    15. ​
    16. import unittest
    17. from nose.tools import assert_equal, assert_true
    18. def getFactors(x):
    19. """Returns a list of factors of the given number x.
    20. Basically, finds the numbers between 1 and the given integer that divide the number evenly.
    21. ​
    22. For example:
    23. - If we call getFactors(2), we'll get [1, 2] in return
    24. - If we call getFactors(12), we'll get [1, 2, 3, 4, 6, 12] in return
    25. """
    26.  
    27. List=[]
    28. for i in range (1,x+1):
    29. if(x % i == 0):
    30. List.append(i)
    31.  
    32. return List
    33.  
    34. ##########################
    35. ### TEST YOUR SOLUTION ###
    36. ##########################
    37. ​
    38. num = 2
    39. factors_test = [1, 2]
    40. factors = getFactors(num)
    41. assert_equal(factors_test, factors, str(factors) + ' are not the factors of ' + str(num))
    42. ​
    43. num = 12
    44. factors_test = [1, 2, 3, 4, 6, 12]
    45. factors = getFactors(num)
    46. assert_equal(factors_test, factors, str(factors) + ' are not the factors of ' + str(num))
    47. ​
    48. num = 13
    49. factors_test = [1, 13]
    50. factors = getFactors(num)
    51. assert_equal(factors_test, factors, str(factors) + ' are not the factors of ' + str(num))
    52. ​
    53. # test existence of docstring
    54. assert_true(len(getFactors.__doc__) > 1, "there is no docstring for getFactors")
    55. print("Success!")
    56. Success!
    57. def isPrime(x):
    58. """Returns whether or not the given number x is prime.
    59. ​
    60. A prime number is a natural number greater than 1 that cannot be formed
    61. by multiplying two smaller natural numbers.
    62. ​
    63. For example:
    64. - Calling isPrime(11) will return True
    65. - Calling isPrime(71) will return True
    66. - Calling isPrime(12) will return False
    67. - Calling isPrime(76) will return False
    68. """
    69. if x==1:
    70. return False
    71. if x==2:
    72. return True
    73. f= True
    74. for i in range(2,x):
    75. if(x%i==0):
    76. f=False
    77. break
    78. return f
    79.  
    80. ##########################
    81. ### TEST YOUR SOLUTION ###
    82. ##########################
    83. ​
    84. prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23,
    85. 29, 31, 37, 41, 43, 47, 53, 59,
    86. 61, 67, 71, 73, 79, 83, 89, 97,
    87. 101, 103, 107, 109, 113, 127,
    88. 131, 137, 139, 149, 151, 157,
    89. 163, 167, 173, 179, 181]
    90. ​
    91. for i in prime_numbers:
    92. assert_true(isPrime(i), str(i) + ' is prime')
    93. ​
    94. not_prime_numbers = [1, 8, 12, 18, 20, 27, 28, 30,
    95. 42, 44, 45, 50, 52, 63, 66,
    96. 68, 70, 75, 76, 78, 92, 98,
    97. 99, 102, 138, 148, 150, 156, 158]
    98. ​
    99. for i in not_prime_numbers:
    100. assert_true(not(isPrime(i)), str(i) + ' is not prime')
    101. ​
    102. #test existence of docstring
    103. assert_true(len(isPrime.__doc__) > 1, "there is no docstring for isPrime")
    104. print("Success!")
    105. Success!
    106. def isComposite(x):
    107. """Returns whether or not the given number x is composite.
    108. ​
    109. A composite number has more than 2 factors.
    110. A natural number greater than 1 that is not prime is called a composite number.
    111. Note, the number 1 is neither prime nor composite.
    112. ​
    113. For example:
    114. - Calling isComposite(9) will return True
    115. - Calling isComposite(22) will return True
    116. - Calling isComposite(3) will return False
    117. - Calling isComposite(41) will return False
    118. """
    119.  
    120. f=False
    121. for i in range(2,x):
    122. if(x%i==0):
    123. f=True
    124. break
    125. return f
    126.  
    127. ##########################
    128. ### TEST YOUR SOLUTION ###
    129. ##########################
    130. ​
    131. composite_numbers = [4, 6, 8, 9, 10, 12, 14, 15, 16,
    132. 18, 20, 21, 22, 24, 25, 26, 27,
    133. 28, 30, 32, 33, 34, 35, 36,
    134. 38, 39, 40, 42, 44, 45, 46, 48,
    135. 49, 50, 51, 52, 54, 55, 56, 57,
    136. 58, 60, 62, 63, 64, 65, 66, 91, 93]
    137. ​
    138. for i in composite_numbers:
    139. assert_true(isComposite(i), str(i) + ' is composite')
    140. ​
    141. not_composite_numbers = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
    142. 29, 31, 37, 41, 43, 47, 53, 59,
    143. 61, 67, 71, 73, 79, 83, 89]
    144. ​
    145. for i in not_composite_numbers:
    146. assert_true(not(isComposite(i)), str(i) + ' is not composite')
    147. ​
    148. #test existence of docstring
    149. assert_true(len(isComposite.__doc__) > 1, "there is no docstring for isComposite")
    150. print("Success!")
    151. Success!
    152. def isPerfect(x):
    153. """Returns whether or not the given number x is perfect.
    154. ​
    155. A number is said to be perfect if it is equal to the sum of all its
    156. factors (for obvious reasons the list of factors being considered does
    157. not include the number itself).
    158. ​
    159. Example: 6 = 3 + 2 + 1, hence 6 is perfect.
    160. Example: 28 is another example since 1 + 2 + 4 + 7 + 14 is 28.
    161. Note, the number 1 is not a perfect number.
    162. """
    163.  
    164. s=0
    165. for i in range (1,x):
    166. if(x % i == 0):
    167. s=s+i
    168.  
    169. f=False
    170. if(s==x):
    171. f= True
    172.  
    173. return f
    174.  
    175. perfect_numbers = [6, 28, 496, 8128, 33550336]
    176.  
    177. for i in perfect_numbers:
    178. assert_true(isPerfect(i), str(i) + ' is perfect')
    179. ​
    180. not_perfect_numbers = [2, 3, 4, 5, 7, 8, 9, 10,
    181. 495, 8127, 8129,
    182. 33550335]
    183. ​
    184. for i in not_perfect_numbers:
    185. assert_true(not(isPerfect(i)), str(i) + ' is not perfect')
    186. ​
    187. #test existence of docstring
    188. assert_true(len(isPerfect.__doc__) > 1, "there is no docstring for isPerfect")
    189. print("Success!")
    190. Success!
    191. def isAbundant(x):
    192. """Returns whether or not the given number x is abundant.
    193. ​
    194. A number is considered to be abundant if the sum of its factors
    195. (aside from the number) is greater than the number itself.
    196. ​
    197. Example: 12 is abundant since 1+2+3+4+6 = 16 > 12.
    198. However, a number like 15, where the sum of the factors.
    199. is 1 + 3 + 5 = 9 is not abundant.
    200. """
    201.  
    202. s=0
    203. for i in range (1,x):
    204. if(x % i == 0):
    205. s=s+i
    206.  
    207. f=False
    208. if(s>x):
    209. f = True
    210. return f
    211.  
    212. abundant_numbers = [12, 18, 20, 24, 30, 36, 40, 42, 48,
    213. 54, 56, 60, 66, 70, 72, 78, 80, 84,
    214. 88, 90, 96, 100, 102, 104, 108, 112,
    215. 114, 120]
    216. ​
    217. for i in abundant_numbers:
    218. assert_true(isAbundant(i), str(i) + ' is abundant')
    219. ​
    220. not_abundant_numbers = [1, 2, 3, 4, 5, 6,
    221. 7, 8, 9, 10, 11, 13,
    222. 14, 15, 16, 17, 19,
    223. 21, 22, 23, 25, 26, 27, 28, 29,
    224. 91, 92, 93, 94, 95, 119]
    225. ​
    226. for i in not_abundant_numbers:
    227. assert_true(not(isAbundant(i)), str(i) + ' is not abundant')
    228. ​
    229. #test existence of docstring
    230. assert_true(len(isAbundant.__doc__) > 1, "there is no docstring for isAbundant")
    231. print("Success!")
    232. Success!
    233. def isTriangular(x):
    234. """Returns whether or not a given number x is triangular.
    235.  
    236. The triangular number Tn is a number that can be represented in the form of a triangular
    237. grid of points where the first row contains a single element and each subsequent row contains
    238. one more element than the previous one.
    239.  
    240. We can just use the fact that the nth triangular number can be found by using a formula: Tn = n(n + 1) / 2.
    241.  
    242. Example: 3 is triangular since 3 = 2(3) / 2
    243. 3 --> 2nd position: (2 * 3 / 2)
    244.  
    245. Example: 15 is triangular since 15 = 5(6) / 2
    246. 15 --> 5th position: (5 * 6 / 2)
    247. """
    248.  
    249. if(x==1):
    250. return True
    251. f=False
    252. for i in range(1,x):
    253. s=(i*(i+1))/2
    254. if(s==x):
    255. f = True
    256. break
    257. return f
    258.  
    259. triangular_numbers = [1, 3, 6, 10, 15, 21, 28, 36, 45, 55,
    260. 66, 78, 91, 105, 120, 136, 153, 171,
    261. 190, 210, 231]
    262. ​
    263. for i in triangular_numbers:
    264. assert_true(isTriangular(i), str(i) + ' is triangular')
    265. ​
    266. not_triangular_numbers = [2, 4, 5, 7, 8, 9, 11, 12, 13, 14,
    267. 16, 17, 18, 19, 20,
    268. 22, 23, 24, 25, 26, 27,
    269. 29, 30, 31, 32, 33, 34, 35,
    270. 37, 40, 41, 42, 43, 44,
    271. 54, 56, 189, 191, 209, 211, 230, 232]
    272. ​
    273. for i in not_triangular_numbers:
    274. assert_true(not(isTriangular(i)), str(i) + ' is not triangular')
    275. ​
    276. #test existence of docstring
    277. assert_true(len(isTriangular.__doc__) > 1, "there is no docstring for isTriangular")
    278. print("Success!")
    279. Success!
    280. def isNarcissistic(x):
    281. """Returns whether or not a given number is Narcissistic.
    282. ​
    283. A positive integer is called a narcissistic number if it
    284. is equal to the sum of its own digits each raised to the
    285. power of the number of digits.
    286. ​
    287. Example: 153 is narcissistic because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
    288. Note that by this definition all single digit numbers are narcissistic.
    289. """
    290.  
    291. num = str(x)
    292. power = len(num)
    293. sum = 0
    294. for number in num:
    295. sum += pow(int(number), power)
    296. if sum == x:
    297. return True
    298. return False
    299.  
    300. narcissistic_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
    301. ​
    302. for i in narcissistic_numbers:
    303. assert_true(isNarcissistic(i), str(i) + ' is narcissistic')
    304. ​
    305. non_narcissistic_numbers = [10, 11, 12, 13, 152, 154, 369, 372, 406]
    306. ​
    307. for i in non_narcissistic_numbers:
    308. assert_true(not(isNarcissistic(i)), str(i) + ' is not narcissistic')
    309. ​
    310. #test existence of docstring
    311. assert_true(len(isNarcissistic.__doc__) > 1, "there is no docstring for isNarcissistic")
    312. print("Success!")
    313. Success!
    314. def main():
    315. ​
    316. playing = True
    317. while playing == True:
    318. ​
    319. num_input = input('Give me a number from 1 to 10000. Type -1 to exit. ')
    320. ​
    321. try:
    322. num = int(num_input)
    323. ​
    324. if (num == -1):
    325. playing = False
    326. continue
    327. ​
    328. if (num <= 0 or num > 10000):
    329. continue
    330. ​
    331. factors = getFactors(num)
    332. print("The factors of", num, "are", factors)
    333. ​
    334. if isPrime(num):
    335. print(str(num) + ' is prime')
    336. if isComposite(num):
    337. print(str(num) + ' is composite')
    338. if isPerfect(num):
    339. print(str(num) + ' is perfect')
    340. if isAbundant(num):
    341. print(str(num) + ' is abundant')
    342. if isTriangular(num):
    343. print(str(num) + ' is triangular')
    344. if isNarcissistic(num):
    345. print(str(num) + ' is narcissistic')
    346. ​
    347. except ValueError:
    348. print('Sorry, the input is not an int. Please try again.')
    349.  
    350. #This will automatically run the main function in your program
    351. #Don't change this
    352. if __name__ == '__main__':
    353. main()