[pypy-issue] Issue #1878: cPython and PyPy produce different results when run on the same code (pypy/pypy)
Ran Gutin
issues-reply at bitbucket.org
Sat Oct 4 18:58:26 CEST 2014
New issue 1878: cPython and PyPy produce different results when run on the same code
https://bitbucket.org/pypy/pypy/issue/1878/cpython-and-pypy-produce-different-results
Ran Gutin:
This is my solution to Project Euler problem 95.
```
#!python
import time
def euler95():
def chainLength(i):
start = i
sofar = set()
length = 0
while i not in sofar and i <= 10**6:
sofar = sofar.union({i})
#print(sofar)
i = sums[i]
length += 1
if i == start:
return length
return 0
sums = [sum(factors) for factors in factorsOfEverythingBelow(10**6+1)]
print("found sums")
return max(range(10**6), key = chainLength)
def factorsOfEverythingBelow(n):
factors = []
for i in range(n): factors += [[1]]
for i in range(2,n):
for j in range(i+i,n,i):
factors[j] += [i]
return factors
def euler(n, f):
begin = time.time()
print("Euler",n,":",f(),"in",time.time()-begin,"seconds")
```
Start the program with:
```
#!python
euler(95,euler95)
```
CPython prints out:
found sums
Euler 95 : 14316 in 19.938631057739258 seconds
PyPy prints out:
found sums
Euler 95 : 1 in 6.321170806884766 seconds
CPython gives the right answer to https://projecteuler.net/problem=62 and PyPy gives the wrong answer.
Given another Project Euler problem:
```
#!python
def period_sqrt(n):
if float(sqrt(n)).is_integer():
return 0
m = 0
d = 1
a = int(n**0.5)
a0 = a
mdset = set()
period = 0
while True:
m = d*a - m
d = (n - m**2)/d
a = int((a0 + m)/d)
if (m,d) in mdset:
return period
mdset = mdset.union({(int(m),int(d))})
period += 1
def euler64():
return sum(1 for i in range(1,10**4+1) if period_sqrt(i) % 2 == 1)
```
Both CPython and PyPy give the right result.
More information about the pypy-issue
mailing list