From 78d44a86c71257dc5b170a933797993e0bc94434 Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Tue, 23 Aug 2016 21:42:18 +0800 Subject: [PATCH 1/4] URGENT: BUG-FIX --- seriously/SeriouslyCommands.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index c9bac32..0ff0f52 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -209,6 +209,19 @@ def nPr(n, k): return 1 return math.factorial(n)/math.factorial(n-k) +def isqrt(n): + lo=1 + hi=n + while lox: break - n = primes[-1]+2 - while n*n<=x: - if x%n==0: + for test in range(primes[-1]+2,isqrt(x)): + if x%test==0: return 0 return 1 From 05514a69a6e8529910cb0ee85718e44f0d5d9bc1 Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Wed, 24 Aug 2016 00:30:47 +0800 Subject: [PATCH 2/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 57 +++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index 0ff0f52..3396053 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -96,8 +96,6 @@ def gcd(a,b): def gcd_list(*vals): return reduce(gcd,vals or [1]) -primes = [2,3] - class MathSelector(object): def __init__(self, fn): self.fn = fn @@ -209,6 +207,9 @@ def nPr(n, k): return 1 return math.factorial(n)/math.factorial(n-k) +primes = [2,3] +max_tested = 4 + def isqrt(n): lo=1 hi=n @@ -238,23 +239,33 @@ def is_prime(x): return 0 return 1 -def init_next_prime(n): - global primes - if n == -1: - n = primes[-1] - if primes[-1] > n: - return - x = primes[-1]+2 - while True: - if is_prime(x): - primes.append(x) - return - x+=2 +def init_n_primes(n): + global primes, max_tested + while len(primes)<=n: + temp=[1]*max_tested + for p in primes: + for q in range((p-max_tested)%p,max_tested,p): + temp[q] = 0 + primes += [x+max_tested for x in range(max_tested) if temp[x]] + max_tested *= 2 + +def init_primes_up_to(n): + global primes, max_tested + if max_tested1: + init_primes_up_to(isqrt(n)) + for p in primes: a=0 while n%p==0: a+=1 n//=p if a: res.append([p,a]) - init_next_prime(p) - index += 1 - p = primes[index] + if n==1: + break + if n>1: + # n is a prime at this point, but please don't add + # it to the prime list as it would mess up the prime + # list since the prime list would not be continuous + res.append([n,1]) return res def factor(n): From f7728ed19f452a45ebd3c2bb82310b952b86120b Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Wed, 24 Aug 2016 01:21:32 +0800 Subject: [PATCH 3/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index 3396053..ef22158 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -253,6 +253,7 @@ def init_primes_up_to(n): global primes, max_tested if max_tested= n >= primes[-1]: + return len(primes) + #binary search + lo=0;hi=len(primes)-1 + while lo Date: Wed, 24 Aug 2016 01:50:12 +0800 Subject: [PATCH 4/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index ef22158..4508491 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -215,12 +215,10 @@ def isqrt(n): hi=n while lo= n >= primes[-1]: return len(primes) #binary search - lo=0;hi=len(primes)-1 + lo=0 + hi=len(primes)-1 while lo