forked from alexander-mead/python_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.py
65 lines (57 loc) · 1.28 KB
/
maths.py
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
def is_prime(n):
'''
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):
'''
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):
'''
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):
'''
Returns true if integer is even
'''
if num%2 == 0:
return True
else:
return False
def is_odd(num):
'''
True if integer is odd
'''
return not is_even(num)
def ceiling(a, b):
'''
Ceiling division a/b
TODO: Could also use math.ceiling
'''
return -(-a // b)
def reverse_digits(old_number):
'''
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