-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathUtils.py
50 lines (43 loc) · 1.05 KB
/
MathUtils.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
"""
Random class.
"""
from random import randint
from math import gcd
from sympy import isprime
class MathUtils:
"""
Static class for random numbers
and other number operations.
"""
@classmethod
def get_random_prime(cls, bits: int) -> int:
"""
:return: Returns a random prime number.
"""
# Until we find a valid random prime
while True:
# Get a random number with that amount of bits
# [2 ^ (bits - 1) :to: 2 ^ bits - 1]
# For example, for 3 bits:
# [0b100 :to: 0b111]
random_number: int = randint(2 ** (bits - 1), 2 ** bits - 1)
# If it is prime
if cls.is_prime(random_number):
# Return it
return random_number
@classmethod
def is_prime(cls, number: int) -> bool:
"""
Check if a number is prime.
:param number: The value to check
:return: Boolean value; Is the value a prime number?
"""
# Implement later using Rabin-Miller
return isprime(number)
@staticmethod
def lcm(a, b):
"""
Math.lcm implementation (since not available in 3.8).
:return:
"""
return abs(a*b) // gcd(a, b)