-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmead_maths.py
More file actions
79 lines (65 loc) · 1.6 KB
/
mead_maths.py
File metadata and controls
79 lines (65 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def is_prime(n: int) -> int:
'''
Returns True if the number 'n' is prime
'''
prime = True
for i in range(2, n):
if n % i == 0:
prime = False
break
return prime
def sum_of_digits(n: int) -> int:
'''
Calculates the sum of the digits of a number
'''
tot = 0
for digit in str(n):
tot += int(digit)
return tot
def product_of_digits(n: int) -> int:
'''
Calculates the product of the digits of a number
'''
tot = 1
for digit in str(n):
tot *= int(digit)
return tot
def is_even(num: int) -> bool:
'''
Returns true if integer is even
'''
if num % 2 == 0:
return True
else:
return False
def is_odd(num: int) -> bool:
'''
Returns true if integer is odd
'''
return not is_even(num)
def ceiling(a: float, b: float) -> int:
'''
Ceiling division a/b
TODO: Could also use math.ceiling
TODO: What about floor?
'''
return -(-a // b)
def reverse_digits(old_number: int) -> int:
'''
Reverses the digits of integer n
'''
new_number = 0
n = old_number
while n != 0:
last_digit = n % 10 # Isolate the final digit using modulus
n = n//10 # Floor division to remove the final digit
new_number = new_number*10+last_digit # Construct new number
return new_number
# return int(str(n)[::-1]) # Easy using strings
def remainder_division(a: int, b: int) -> tuple:
'''
Returns the whole division and remainder of a/b
'''
whole = a//b
remainder = a % b
return whole, remainder