发现python处理除法挺快的,就直接偷懒了。。
求大佬解惑,为什么会有非零返回
from functools import reduce
class judge():
def is_divisible_by_2(self,s):
if int(s[-1]) == 0 or int(s[-1])%2 ==0:
return True
else:
return False
def is_divisible_by_3(self,s):
#计算各个数位的数字之和
b = reduce((lambda x, y: int(x) + int(y)), a)
if b%3 == 0:
return True
else:
return False
def is_divisible_by_5(self,s):
#如果末尾是0,则一定能被2整除。故不用再次判断
if s[-1] =='5':
return True
else:
return False
def is_divisible_by_7(self,s):
while int(s) > 7:
s = str(int(s[:-1])-2*int(s[-1]))
if int(s) != 7:
return False
else:
return True
def is_divisible_by_11(self, s):
#分别取出奇数和偶数位的数字
tmp1 = s[::2]
tmp2 = s[1::2]
sum_tmp1 = reduce((lambda x, y: int(x) + int(y)), tmp1)
sum_tmp2 = reduce((lambda x, y: int(x) + int(y)), tmp2)
if int(sum_tmp2-sum_tmp1) ==0 or int(sum_tmp2-sum_tmp1) %11 == 0:
return True
else:
return False
def is_divisible_by_13(self, s):
while len(s) > 2:
s = str(int(s[:-1])+int(s[-1])*4)
if int(s) %13 ==0:
return True
else:
return False
def is_divisible_by_17(self, s):
while len(s) > 2:
s = str(int(s[:-1]) + int(s[-1]) * 5)
if int(s) % 17 == 0:
return True
else:
return False
def is_divisible_by_19(self, s):
while len(s) > 2:
s = str(int(s[:-1]) + int(s[-1]) * 2)
if int(s) % 19 == 0:
return True
else:
return False
def judge_prime(self,num,prime):
if prime < 20:
p = getattr(self,'is_divisible_by_'+str(prime))
return p(num)
else:
if (int(num)%prime) == 0:
return True
else:
return False
if __name__ == '__main__':
l,k = map(int, input().split(" "))
n = input()
# l,k = 20,5
# n = '23654987725541023819'
m = [n[i:i+k] for i in range(l-k+1)]
is_prime = [True] * (len(m))
prime_initial = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71]
#原始
for i in range(len(m)):
tmp = judge()
if tmp.is_divisible_by_2(m[i]):
is_prime[i] = False
# print(is_prime)
else:
a = list(filter(None, list(m[i])))
for j in prime_initial:
if tmp.judge_prime(m[i],j) and is_prime[i]:
is_prime[i] = False
if any(is_prime):
for i in range(len(is_prime)):
if is_prime[i]:
print(m[i],end='')
break
else:
print('404')