Ask Your Question

n-th prime factor and handling equations with multiple variables at once

asked 2023-01-30 04:38:13 +0200

aviolette gravatar image

updated 2023-02-17 00:17:31 +0200

Hello! I was wondering how to get the n-th factor of a number in such a way that I can work with it easily(Without it being in parenthesis as an example). For example, the 5th factor of 36 and in Sage I want to do (nth factor of 36)^2. Also, how does one have sage run code involving multiple variables with something like this without having to manually run it for each value of lets say the variable "l"? For my code here:


def a(x, i):
    return (floor(abs((y*x*(2*i+1)/(s*(2*i+2)-y*(x*(2*i+1))))))+l)

def a2(x, i):
    return (floor(abs((y*x*(2*i+1)*a(x, i))/(s*(2*i+2)*(a(x, i)+1)- y*(x*(2*i+1)*a(x, i)))))-m)

def what_im_looking_forb(min_range, max_range):
    for l in range(200):
        for i in range(min_range, max_range):
            if abs(s*(2*i+2)*(a(x, i)+1)*(a2(x, i)+1)-y*x*(2*i+1)*(a(x, i))*(a2(x, i)))<z and (a(x, i) in Primes())==True and (a2(x, i) in Primes())==True and ((2*i+1) in Primes())==True:
                print l, (2*i+1), x*(2*i+1), x*(2*i+1)*(a(x, i)), x*(2*i+1)*(a(x, i))*a2(x, i), s*(2*i+2)*(a(x, i)+1)*(a2(x, i)+1)-y*x*(2*i+1)*(a(x, i))*a2(x, i), gcd(x*(2*i+1)*(a(x, i))*(a2(x, i))*y,s*(2*i+2)*(a(x, i)+1)*(a2(x, i)+1))


I want it to run through t, t+20000 at l=0, and then go to t, t+20000 at l=1, all the way to l=200 without having to do each input from l=0 to l=200 on my own. Currently only loops on l=0 and not on multiple l values.

edit retag flag offensive close merge delete


It's looks like you're using Sage based Python version 2.x which should be considered outdated. Please switch to a newer version of Sage based on Python 3.x

Max Alekseyev gravatar imageMax Alekseyev ( 2023-02-15 18:37:17 +0200 )edit

Try to replace for l in range(200) (which creates a local variable l independent of the global variable l) with

for l2 in range(200):
    l = l2
Max Alekseyev gravatar imageMax Alekseyev ( 2023-02-15 20:35:48 +0200 )edit

I did and I got a Syntax Error of "can't assign function to call."(Had the line below on the same indentation position the l=12 was because it gave me an indentation error otherwise)

aviolette gravatar imageaviolette ( 2023-02-17 00:17:49 +0200 )edit

It's not 12 (twelve) but l2 (el-two).

Max Alekseyev gravatar imageMax Alekseyev ( 2023-02-17 00:27:24 +0200 )edit

Ohhh, l2 and 12 look very similar in that code font so that explains why I interpreted it wrong. Its now looping but I have the same issue of it repeating the same outputs from l=0 like last time instead of it giving me different outputs for each l.

aviolette gravatar imageaviolette ( 2023-02-17 00:40:55 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-01-31 19:21:53 +0200

Max Alekseyev gravatar image

updated 2023-01-31 19:22:29 +0200

Your best route would be to learn some Python before dealing with Sage. Think Python is a nice introductory book:

As for you specific questions, this seems to answer your first question:


and the second one can be answered with two loops and is_prime() function - like:

def mytest(x):
    for y in range(6):
        for z in range(x,x+101):
             if is_prime(3^z + 2*y):
                  # do what you want
edit flag offensive delete link more


Thank you! I was able to get more complex code to work for the first question, the second though while I got it to loop, it kept repeating the same outputs. I was wondering how everyone here is able to surround their code in that grey block format taking whole rows as I found nothing under the faq section here.

aviolette gravatar imageaviolette ( 2023-02-05 18:27:34 +0200 )edit

Please add your code to the question.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-02-05 19:28:44 +0200 )edit

Just edited it

aviolette gravatar imageaviolette ( 2023-02-15 18:34:51 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2023-01-30 04:38:13 +0200

Seen: 152 times

Last updated: Feb 17 '23