正剧开始:
星历2016年05月08日 10:05:37, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[初等数论初步]。
710316能被3整除
710316能被9整除
710316 不能 被11整除
710316 不能 被7整除
#根据规律判整除性
#其实并不实用,只是说明一下规律
def tmp():
number = 710316;
array = numberArray(number);
size = len(array);
#被11整除
array_odd = array[0:size:2];
array_even = array[1:size:2];
#被7整除
array_last3 = number%1000;
array_front = (number-array_last3)//1000;
#判断被3整除
if sum(array)%3==0:
print('{0}能被3整除'.format(number));
else:
print('{0} 不能 被3整除'.format(number));
#判断被9整除
if sum(array)%9==0:
print('{0}能被9整除'.format(number));
else:
print('{0} 不能 被9整除'.format(number));
#判断被11整除
if (sum(array_odd)-sum(array_even))%11 == 0:
print('{0}能被11整除'.format(number));
else:
print('{0} 不能 被11整除'.format(number));
#判断被7整除
if (array_last3 - array_front)%7==0:
print('{0}能被7整除'.format(number));
else:
print('{0} 不能 被7整除'.format(number));
#把一个整数的各位数字放入数组
#由个位往高位排列
def numberArray(number):
array = [];
bit = len(str(number));
for i in range(bit):
array.append(number%10);
number//=10;
#array.reverse();
return array;
#判断一个数是否质数
def prime(num):
if (num < 2):
return False;
sqr = int(math.sqrt(num))+1;
for i in range(2, sqr):
if (num%i==0):
return False;
return True;
#最大公约数
def gcd(m, n):
m, n = max(m, n), min(m, n);
i = 1;
while n:
print('step', i, ': ', m, n);
i += 1;
m, n = n, m % n
return m
#最小公倍数
def lcm(m, n):
return m*n/gcd(m, n);
>>>
2625.0
print(lcm(375, 105));
#分解质因数
def primeFactor(num, lists):
if (num < 2):
lists.append(num);
return lists;
elif (prime(num) == True):
lists.append(num);
return lists;
else:
sqr = int(math.sqrt(num))+1;
i = 2;
while i <= sqr:
if (num % i == 0 and prime(i) == True):
lists.append(i);
num = num//i;
break;
i+=1;
return primeFactor(num, lists);
>>>
720 = 2 * 2 * 2 * 2 * 3 * 3 * 5
152 = 2 * 2 * 2 * 19
8
13680.0
def tmp2():
number = [720, 152];
for j in range(len(number)):
factors = primeFactor(number[j], []);
count = len(factors);
s = str(number[j])+' = ';
for i in range(count):
s += str(factors[i]);
if (i < count-1):
s += ' * ';
print(s);
print(gcd(720,152));
print(lcm(720, 152));
>>>
21的欧拉函数值φ(21) = 12.0
def tmp3(number):
print('{0}的欧拉函数值φ({0}) = {1}'.format(number, round(eulerPhi(number),3)))
#欧拉函数
def eulerPhi(number):
factors = set(primeFactor(number, []));
factors = sorted(factors);
#print(factors);
phi = number;
for i in range(len(factors)):
phi *= (1-1/factors[i]);
return phi;
>>>
25的欧拉函数值φ(25) = 20.0
def tmp4():
for i in range(1, 21):
if (18**i)%25==1:
print(i);
break;
>>>
[4.0, 9.0, 14.0]
def tmp5():
result = [];
mod = 15;
remain = 6;
quotient = 9;
for i in range(1, 20):
x = (mod*i + remain)/quotient;
if abs((int(x))-x)< 1e-6:
result.append(x);
if x > mod:
break;
print(result);
>>>
[9.0]
#例6
def tmp5():
result = [];
mod = 74;
remain = 1;
quotient = 33;
for i in range(1, 20):
x = (mod*i + remain)/quotient;
if abs((int(x))-x)< 1e-6:
result.append(x);
if x > mod:
break;
print(result);
var s = [
'f(a)=e, f(b)=f, f(c)=g',
'a=-1, e=2;',
'b=0, f=3;',
'c=1, g=6;',
'f(x) = 2(x-0)(x-1)/(-1)/(-2)',
' +3(x+1)(x-1)/(1)/(-1)',
' +6(x+1)(x-0)/(2)/(1)',
];
[58, 238, 418, 598, 778, 958, 1138, 1318, 1498]
#孙子定理的变通
def tmp6():
remain = [2, 3, 4];
mod = [4, 5, 9];
count = len(mod);
multi = 1;
for i in range(count):
multi *= mod[i];
#直接遍历这个范围
bound = sum(remain)*multi+1;
result = [];
for i in range(1, bound):
for j in range(count):
if i%mod[j]!=remain[j]:
break;
if j >= count-1:
result.append(i);
print(result);
[282, 975, 1668, 2361, 3054, 3747, 4440, 5133, 5826, 6519, 7212, 7905]
#孙子定理的变通
def tmp6():
#模数
mod = [7, 9, 11];
#余数
remain = [2, 3, 7];
count = len(mod);
multi = 1;
for i in range(count):
multi *= mod[i];
#直接遍历这个范围
bound = sum(remain)*multi+1;
result = [];
for i in range(1, bound):
for j in range(count):
if i%mod[j]!=remain[j]:
break;
if j >= count-1:
result.append(i);
print(result);
[2111, 4421, 6731, 9041, 11351, 13661, 15971, 18281, 20591, 22901, 25211, 27521, 29831, 32141, 34451, 36761, 39071, 41381, 43691, 46001]
#韩信点兵
#孙子定理的变通
def tmp6():
#模数
mod = [5, 6, 7, 11];
#余数
remain = [1, 5, 4, 10];
count = len(mod);
multi = 1;
for i in range(count):
multi *= mod[i];
#直接遍历这个范围
bound = sum(remain)*multi+1;
result = [];
for i in range(1, bound):
for j in range(count):
if i%mod[j]!=remain[j]:
break;
if j >= count-1:
result.append(i);
print(result);
2 30 68
5 25 70
8 20 72
11 15 74
14 10 76
17 5 78
20 0 80
#百马百瓦
def tmp8():
a = 33;
b = 50;
c = 200;
result = [];
for i in range(a+1):
for j in range(b+1):
for k in range(c+1):
if (abs(((i*3 + j*2 + k*0.5) - 100)) < 1e-6) and i+j+k == 100:
print(i, j, k);
#不定方程
def tmp7():
#ax + by = c
#b为大于1的整数
a, b, c = 3, 2, 5;
u, v, i, j = a, b, 0, 1;
r = u%v;
k = j;
while r!=1:
u, v = v, r;
q = u//v;
r = u%v;
j = i-q*j;
i = k;
k = j;
x = c*k;
y = c*(1-a*k)/b;
print(x, y);
>>>
-34 6.0
#不定方程
def tmp7():
#ax + by = c
#b为大于1的整数
a, b, c = 13, 74, 2;
u, v, i, j = a, b, 0, 1;
r = u%v;
k = j;
while r!=1:
u, v = v, r;
q = u//v;
r = u%v;
j = i-q*j;
i = k;
k = j;
x = c*k;
y = c*(1-a*k)/b;
print(x, y);
本节到此结束,欲知后事如何,请看下回分解。