-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.py
executable file
·64 lines (54 loc) · 1.34 KB
/
Common.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
64
# Common or useful functions
def isPrime(num):
num = abs(num)
if num < 2:
return False
if num == 2:
return True
if num % 2 == 0:
return False
for i in range(3, int(num ** .5) + 1, 2):
if num % i == 0:
return False
return True
def primeSieve(bound):
sieve = [True] * bound
sieve[0] = sieve[1] = False
for (i, prime) in enumerate(sieve):
if prime:
yield i
for n in range(i * i, bound, i):
sieve[n] = False
def gcd(x, y):
if ((x % y) == 0):
return y;
else:
return gcd(y, x % y)
def lcm(x, y):
return (x // gcd(x, y)) * y
def prod(factors):
p = 1
for n in factors:
p *= n
return p
def divisors(n):
fact = set()
for i in range(1, int(n ** .5 + 1), 1):
if n % i == 0:
fact.add(i)
fact.add(n // i)
return fact
def trianglenum(n):
return int(0.5 * n * (n + 1))
def pentagonnum(n):
return int(0.5 * n * (3 * n - 1))
def hexagonnum(n):
return n * (2 * n - 1)
def isPandigital(n):
s = str(n)
length = len(s)
for i in range(length):
if s.find(str(length - i)) == -1:
return False
s = s.replace(str(length - i), '', 1)
return len(s) == 0