朋友圈看到的一张图,于是用python3写了个算法程序算了一下~~~~
-----------------------------------------------------------------------------------------------------------------------------------------------------
下面是coding:
#coding:utf-8
#python3!
#1.ID:NYAB(AB均为质数):A>B & A*B=707829217
#2.1-AB的奇数序列里,一共出现了多少个3
'''
n = 12345,设n在某一位的数字为current,当前位的因素为factor
(百位的factor为100),高位数字为higher(百位的higher为12),
低位数字为lower(百位的lower为45),当前位出现x的次数为bit_sum。current = x时:bit_sum = higher * factor + lower + 1;
current < x时:bit_sum = higher * factor;
current > x时:bit_sum = (higher + 1) * factor;
因为奇数序列中数字的所有个位都是奇数,所以:
当前位是个位,且x是偶数时,bit_sum = 0;
奇数序列长度 约等于 自然数序列长度 的 50%;current != x,当前位是个位时,bit_sum不变;current != x,当前位非个位时,bit_sum会减少50%;current = x,且当前位是个位时,bit_sum = higher * factor + 1;
current = x,且当前位非个位时,bit_sum = higher * factor * 0.5 + ceil(lower * 0.5)(进一法);
'''
import numpyas np
import math
def product(n):
x =int(n **0.5)
L = np.arange(1,x+1)
L[0] =0
for iin range(2, x+1):
if L[i-1] !=0:
for jin range(i*2, x+1, i):
L[j-1] =0
primes = [xfor xin Lif x !=0]
return primes
ll = []
def decompose(num):
primes = product(num)
iszhishu =True
for iin primes:
if num % i ==0:
ll.append(i)
iszhishu =False
decompose(num/i)
if iszhishu:
ll.append(int(num))
return ll
def merge(result):
c =''
for iin result[::-1]:
c +=str(i)
return int(c)
def juge_three(n, x):#1-n的奇数序列中,x出现的次数
ssum =0 #当前出现x的次数
factor =1 #当前位的因素(百位的因素是100)
higher =0 #当前位的高位,N=12345,X=3,当前位在百位时,higher是12
current =0 #当前位
lower =0 #当前位的低位,N=12345,X=3,当前位在百位时,lower是45
time =1 #自然数序列的长度为1,其中奇数序列约为0.5
while math.floor(n/factor) !=0:
higher = math.floor(n/(factor*10))
current = math.floor(n/factor) %10
lower = n - math.floor(n/factor)*factor
if factor >1:#奇数序列,除了个位数以外,其他位数包含x的数量,会少一半
time =0.5
else:
if x %2 ==0:#奇数序列,当x为偶数时, 个位数一个都没有, 所以跳过个位数的运算
continue
if x ==0:#当x=0时, 高位相对要减1, eg:1-200:11-19、111-119,若x=0,则只有101-109,不会有00-09
higher -=1
if current == x:
t = factor
if factor ==1 :
math.ceil(lower * time)
ssum += higher * factor * time + t
elif current > x:
ssum += (higher +1) * factor * time
elif current < x:
ssum += higher * factor * time
factor *=10
return int(ssum)
if __name__ =='__main__':
num =707829217
x =3
result = decompose(num)
wechat_number = merge(result)
three = juge_three(wechat_number, x)
print('wechat ID is: ' +'NY' +str(wechat_number))
print('the number 3 is: ', three)
最后计算的结果:
wechat ID is: NY866278171
the number 3 is: 441684627
--------------------------------------------------------------------------------------------------------------------------------------------------------
计算奇数序列里有多少个3是从网上搜的思路,原文是JS写的,理解后改成了python,原文地址:
https://github.com/KawayAlpaka/calculation/blob/master/%E6%B1%82%E5%A5%87%E6%95%B0%E5%BA%8F%E5%88%97%E4%B8%ADx%E5%87%BA%E7%8E%B0%E7%9A%84%E6%AC%A1%E6%95%B0/README.md