diff --git a/libpy2php/libpy2php.php b/libpy2php/libpy2php.php index ab19f7f9c..7be90eca9 100644 --- a/libpy2php/libpy2php.php +++ b/libpy2php/libpy2php.php @@ -135,8 +135,12 @@ function pyjslib_range($start, $stop = null, $step = 1) return array_reverse($arr, false); } - $arr = range($start, $stop, $step); - array_pop($arr); + if($step > 1 && $step > ($start - $stop)) { + $arr = [ $start ]; + } else { + $arr = range($start, $stop, $step); + array_pop($arr); + } return $arr; } diff --git a/prime.php b/prime.php index 8482e2be1..8840fc694 100644 --- a/prime.php +++ b/prime.php @@ -12,22 +12,22 @@ class PrimeModule $this->smallprimes = $this->primesbelow(10000); } /* -def primesbelow(N): - # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 - #""" Input N>=6, Returns a list of primes, 2 <= p < N """ - correction = N % 6 > 1 - N = {0:N, 1:N-1, 2:N+4, 3:N+3, 4:N+2, 5:N+1}[N%6] - sieve = [True] * (N // 3) - sieve[0] = False - for i in range(int(N ** .5) // 3 + 1): - if sieve[i]: - k = (3 * i + 1) | 1 - print(len(sieve[k*k // 3::2*k])) - print(((N // 6 - (k*k)//6 - 1)//k +1)) - sieve[k*k // 3::2*k] = [False] * ((N//6 - (k*k)//6 - 1)//k + 1) - sieve[(k*k + 4*k - 2*k*(i%2)) // 3::2*k] = [False] * ((N // 6 - (k*k + 4*k - 2*k*(i%2))//6 - 1) // k + 1) +def primesbelow(N): + # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 + #""" Input N>=6, Returns a list of primes, 2 <= p < N """ + correction = N % 6 > 1 + N = {0:N, 1:N-1, 2:N+4, 3:N+3, 4:N+2, 5:N+1}[N%6] + sieve = [True] * (N // 3) + sieve[0] = False + for i in range(int(N ** .5) // 3 + 1): + if sieve[i]: + k = (3 * i + 1) | 1 + print(len(sieve[k*k // 3::2*k])) + print(((N // 6 - (k*k)//6 - 1)//k +1)) + sieve[k*k // 3::2*k] = [False] * ((N//6 - (k*k)//6 - 1)//k + 1) + sieve[(k*k + 4*k - 2*k*(i%2)) // 3::2*k] = [False] * ((N // 6 - (k*k + 4*k - 2*k*(i%2))//6 - 1) // k + 1) return [2, 3] + [(3 * i + 1) | 1 for i in range(1, N//3 - correction) if sieve[i]] - */ + */ public function primesbelowtwo($N) { $correction = posmod($N, 6) > 1; $N = [$N, $N - 1, $N + 4, $N + 3, $N + 2, $N + 1][$N%6];