python計(jì)算微信ID-(兩個(gè)質(zhì)數(shù),1開始到這個(gè)數(shù)的奇數(shù)序列里有多少個(gè)3)

朋友圈看到的一張圖,于是用python3寫了個(gè)算法程序算了一下~~~~

-----------------------------------------------------------------------------------------------------------------------------------------------------

下面是coding:

#coding:utf-8

#python3!

#1.ID:NYAB(AB均為質(zhì)數(shù)):A>B & A*B=707829217

#2.1-AB的奇數(shù)序列里,一共出現(xiàn)了多少個(gè)3

'''

n = 12345,設(shè)n在某一位的數(shù)字為current,當(dāng)前位的因素為factor

(百位的factor為100),高位數(shù)字為higher(百位的higher為12),

低位數(shù)字為lower(百位的lower為45),當(dāng)前位出現(xiàn)x的次數(shù)為bit_sum。current = x時(shí):bit_sum = higher * factor + lower + 1;

current < x時(shí):bit_sum = higher * factor;

current > x時(shí):bit_sum = (higher + 1) * factor;

因?yàn)槠鏀?shù)序列中數(shù)字的所有個(gè)位都是奇數(shù),所以:

當(dāng)前位是個(gè)位,且x是偶數(shù)時(shí),bit_sum = 0;

奇數(shù)序列長度 約等于 自然數(shù)序列長度 的 50%;current != x,當(dāng)前位是個(gè)位時(shí),bit_sum不變;current != x,當(dāng)前位非個(gè)位時(shí),bit_sum會減少50%;current = x,且當(dāng)前位是個(gè)位時(shí),bit_sum = higher * factor + 1;

current = x,且當(dāng)前位非個(gè)位時(shí),bit_sum = higher * factor * 0.5 + ceil(lower * 0.5)(進(jìn)一法);

'''

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的奇數(shù)序列中,x出現(xiàn)的次數(shù)

? ? ssum =0? ? #當(dāng)前出現(xiàn)x的次數(shù)

? ? factor =1? #當(dāng)前位的因素(百位的因素是100)

? ? higher =0? #當(dāng)前位的高位,N=12345,X=3,當(dāng)前位在百位時(shí),higher是12

? ? current =0? #當(dāng)前位

? ? lower =0? ? #當(dāng)前位的低位,N=12345,X=3,當(dāng)前位在百位時(shí),lower是45

? ? time =1? ? #自然數(shù)序列的長度為1,其中奇數(shù)序列約為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:#奇數(shù)序列,除了個(gè)位數(shù)以外,其他位數(shù)包含x的數(shù)量,會少一半

? ? ? ? ? ? time =0.5

? ? ? ? else:

if x %2 ==0:#奇數(shù)序列,當(dāng)x為偶數(shù)時(shí), 個(gè)位數(shù)一個(gè)都沒有, 所以跳過個(gè)位數(shù)的運(yùn)算

? ? ? ? ? ? ? ? continue

? ? ? ? if x ==0:#當(dāng)x=0時(shí), 高位相對要減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)

最后計(jì)算的結(jié)果:

wechat ID is: NY866278171

the number 3 is:? 441684627

--------------------------------------------------------------------------------------------------------------------------------------------------------

計(jì)算奇數(shù)序列里有多少個(gè)3是從網(wǎng)上搜的思路,原文是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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容