Merge pull request #623 from shhivam/master

Drastically simplified and corrected primes() in src/kindlekey.py
pull/145/merge
Apprentice Harper 6 years ago committed by GitHub
commit 49b064efa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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…
Cancel
Save