免责声明:课余时间写的,仅仅是很多时候懒得手算,不能100%保证完全应对每种情况,请自行判断和使用,如果有任何bug或者功能修改可以向我反馈,我会尝试修复/添加
算法一览:
1.输入一个数,拿到所有的factor
例:8 = [1, 2, 4, 8]
2.输入一个数,拿到的prime formula
例:8 = 2 * 2 * 2
3.输入x和y,计算gcd(x, y)(greater common devisors)
例: gcd(120, 25) = 5
4.找到比x大的最小质数
例: find_bigger_prime(100000);
5.判断x是否为质数
例: is_prime(100003);
使用本程序请确保您有一定的编程基础
ps:不定期更新
# 1.拿到一个数的所有factor, 注意,输入必须大于1!
def factors_list(x):
result_list = []
for i in range(1, x+1):
if x % i == 0:
result_list.append(i)
return result_list
# 2.输出一个数的prime formula, 注意,输入必须大于1!
def prime_formula(x):
result_list = []
i = 2
while i <= x/i:
while x % i == 0:
result_list.append(i)
x /= i
i += 1
if x > 1:
result_list.append(int(x))
return "*".join(str(i) for i in result_list)
# 3.输入要查询gcd的x和y,例如find_gcd(120, 25), 输出结果5
def find_gcd(x, y):
quotient = x // y
reminder = x - (quotient * y)
print(f'x = {x}\ty = {y}\tq = {quotient}\tr = {reminder}')
while reminder != 0:
x = y
y = reminder
quotient = x // y
reminder = x - (quotient * y)
print(f'x = {x}\ty = {y}\tq = {quotient}\tr = {reminder}')
return y
# 程序启动部分
def main(select_function):
if select_function == 1:
number = int(input("pls enter the number: "))
print(f'factors_list for {number} = {factors_list(number)}')
elif select_function == 2:
number = int(input("pls enter the number: "))
print(f'prime formula for {number} = {prime_formula(number)}')
elif select_function == 3:
x = int(input("pls enter the x: "))
y = int(input("pls enter the y: "))
print(f'gcd({x}, {y}) = {find_gcd(x, y)}')
#
if __name__ == "__main__":
"""
算法一览:
1.输入一个数,拿到所有的factor
例:8 = [1, 2, 4, 8]
2.输入一个数,拿到的prime formula
例:8 = 2*2*2
3.输入x和y,计算gcd(x, y) (greater common devisors)
例: gcd(120, 25) = 5
"""
select_function = int(input("pls enter the function you want to use(input 1/2/3): "))
main(select_function)
//4.找到比当前数大的最小质数
public static void find_bigger_prime(int x) {
for (int i = x; ; i++) {
boolean judge = true;
for (int j = 2; (j * j) <= i; j++){
if (i % j == 0) {
judge = false;
break;
}
}
if (judge) {
System.out.println(i);
break;
}
}
}
//5.判断当前数是否是质数
public static void is_prime(int x) {
boolean judge = true;
for (int j = 2; (j * j) <= x; j++){
if (x % j == 0) {
judge = false;
break;
}
}
if (judge) {System.out.println("Yes");}
else {System.out.println("No");}
}
2022/9/7:对1和2进行优化,降低时间复杂度