From f688bee0aa18a42fb7a805e3220bfb045702bad3 Mon Sep 17 00:00:00 2001 From: shhivam Date: Sat, 8 Sep 2018 23:30:00 +0530 Subject: [PATCH] optimised and corrected primes func drastically --- src/kindlekey.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/kindlekey.py b/src/kindlekey.py index e20b7c9..06aaddc 100644 --- a/src/kindlekey.py +++ b/src/kindlekey.py @@ -126,23 +126,26 @@ def SHA256(message): # For K4M/PC 1.6.X and later # generate table of prime number less than or equal to int n def primes(n): - if n==2: return [2] - elif n<2: return [] - s=range(3,n+1,2) - mroot = n ** 0.5 - half=(n+1)/2-1 - i=0 - m=3 - while m <= mroot: - if s[i]: - j=(m*m-3)/2 - s[j]=0 - while jint + """ + if n == 2: + return [2] + elif n < 2: + return [] + primeList = [2] + + for potentialPrime in range(3, n + 1, 2): + isItPrime = True + for prime in primeList: + if potentialPrime % prime == 0: + isItPrime = False + if isItPrime is True: + primeList.append(potentialPrime) + + return primeList # Encode the bytes in data with the characters in map def encode(data, map):