mirror of
https://github.com/noDRM/DeDRM_tools.git
synced 2025-10-23 23:07:47 -04:00
Merge pull request #623 from shhivam/master
Drastically simplified and corrected primes() in src/kindlekey.py
This commit is contained in:
commit
49b064efa4
1 changed files with 20 additions and 18 deletions
|
|
@ -124,25 +124,27 @@ def SHA256(message):
|
||||||
return ctx.digest()
|
return ctx.digest()
|
||||||
|
|
||||||
# For K4M/PC 1.6.X and later
|
# For K4M/PC 1.6.X and later
|
||||||
# generate table of prime number less than or equal to int n
|
|
||||||
def primes(n):
|
def primes(n):
|
||||||
if n==2: return [2]
|
"""
|
||||||
elif n<2: return []
|
Return a list of prime integers smaller than or equal to n
|
||||||
s=range(3,n+1,2)
|
:param n: int
|
||||||
mroot = n ** 0.5
|
:return: list->int
|
||||||
half=(n+1)/2-1
|
"""
|
||||||
i=0
|
if n == 2:
|
||||||
m=3
|
return [2]
|
||||||
while m <= mroot:
|
elif n < 2:
|
||||||
if s[i]:
|
return []
|
||||||
j=(m*m-3)/2
|
primeList = [2]
|
||||||
s[j]=0
|
|
||||||
while j<half:
|
for potentialPrime in range(3, n + 1, 2):
|
||||||
s[j]=0
|
isItPrime = True
|
||||||
j+=m
|
for prime in primeList:
|
||||||
i=i+1
|
if potentialPrime % prime == 0:
|
||||||
m=2*i+3
|
isItPrime = False
|
||||||
return [2]+[x for x in s if x]
|
if isItPrime is True:
|
||||||
|
primeList.append(potentialPrime)
|
||||||
|
|
||||||
|
return primeList
|
||||||
|
|
||||||
# Encode the bytes in data with the characters in map
|
# Encode the bytes in data with the characters in map
|
||||||
def encode(data, map):
|
def encode(data, map):
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue