Ask Your Question

KristofferH's profile - activity

2021-07-23 00:02:40 +0100 received badge  Famous Question (source)
2021-07-23 00:02:40 +0100 received badge  Notable Question (source)
2020-09-05 09:15:03 +0100 received badge  Famous Question (source)
2020-09-05 09:15:03 +0100 received badge  Notable Question (source)
2019-07-12 06:57:00 +0100 received badge  Notable Question (source)
2018-03-08 12:42:59 +0100 received badge  Popular Question (source)
2018-01-23 00:29:20 +0100 received badge  Popular Question (source)
2017-06-09 19:45:53 +0100 received badge  Popular Question (source)
2015-11-01 04:19:28 +0100 asked a question Applying Memoized to Recursive Function

Does anyone know how to apply memoized function to a recursive function? specifically the def f(xn) function in the following function?

I am trying to improve the following code to be able to factor a numbers. It seems to work well for values like 7331116 and 7331118 but 7331117 results in a recursion depth error and I can't figure out how to improve the code. Someone suggested to me to memoize the def f(xn) function but all can find online is that you add @CachedFunction right before the function is declared but it doesn't seem to help.

def pollard_Rho(n):

def f(xn):
    # This calculates f(x) = x^2+1 based on x0=2.
        return (1 + f(xn-1)**2)%n if xn else 2
    i=0    # Counting variable
    x=f(i)    # calculating x of the pollard rho method.
    y=f(f(i))    # calculating y of the pollard rho method.
    d=gcd(abs(x-y),n)    # calculating gcd to construct loop.
    while d==1:    # A loop looking for a non 1 interesting gcd.
        i = i + 1    # counting iterations 
        d=gcd(abs(x-y),n)
        print d # Printing d=gcd for debugging purposes.
    root1=d # Yes! found a factor, now we can find the other one.
    root2=n/d # Hey! Here is the other factor!
    print i + 1    # debugging print out.
    return (root1,root2) # Kick those factors out into the world.

print pollard_Rho(7331118)

Fixed indentation.

2015-10-12 08:05:02 +0100 received badge  Student (source)
2015-10-12 07:06:50 +0100 asked a question Can't Figure out how to Fix IndexError Based on Len()

Can anyone help fix an error happening on the line "for i in range(1,len(sums)-1):"? I'm relevantly new to sage and more use to python but the few differences sage has I think have been taken into account but I can't figure out what is causing this IndexError..

def Ramanujan(t):
    cubes = [x**3 for x in range(1,t/10)];
    crev = [] # Calculating Cube Roots;
    for x,x3 in enumerate(cubes):
        crev[x3] = x + 1;
        sums = sorted(x + y for x in cubes for y in cubes if y < x) # Organizing Data
        for i in range(1,len(sums)-1):
            if sums[i-1] != sums[i] and sums[i] == sums[i+1]: # Finding solutions
                if sums[i]<=t: # Limiting how many solutions printed.
                    print "%10d"%(sums[i]) # Printing desired outputs
                else:
                    break # Ending the function.

Ramanujan(10000)

Error:

Traceback (most recent call last):            for i in range(1,len(sums)-1):
  File "", line 1, in <module>

File "/private/var/folders/96/g5hyl4ps29dglpy8fnwww6x80000gn/T/tmpWiTKG1/___code___.py", line 16, in <module>
exec compile(u'Ramanujan(_sage_const_10000 )
File "", line 1, in <module>

File "/private/var/folders/96/g5hyl4ps29dglpy8fnwww6x80000gn/T/tmpWiTKG1/___code___.py", line 7, in     Ramanujan
crev[x3] = x + _sage_const_1 ;
IndexError: list assignment index out of range

Does anyone have an idea of how to fix the IndexError I am running across?

2015-10-12 06:44:49 +0100 received badge  Scholar (source)
2015-10-12 05:42:44 +0100 received badge  Editor (source)
2015-10-12 05:40:23 +0100 asked a question Can't find Cause of Traceback Error

I'm trying to convert this code that runs in Python 2.7 to Sage but can't even get a hint from Sage at what line the code is not accepted by Sage.

def Ramanujan(t):
    cubes = [x**3 for x in range(1,t/10)]
    crev = [] ''' Calculating Cube Roots'''
    for x,x3 in enumerate(cubes): crev[x3] = x + 1
    sums = sorted(x + y for x in cubes for y in cubes if y < x) ''' Organizing Data'''
    for i in range(1, len(sums)-1):
        if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
            if sums[i]<=t: ''' Limiting how many solutions printed.'''
                print "%10d"%(sums[i]) ''' Printing desired outputs '''
            else:
                break ''' Ending the function.'''

The only thing that Sage will report back is Traceback Error for SyntaxError: invalid syntax but no line information.. Can anyone tell where specifically the syntax is invalid??