# n-th prime factor and handling equations with multiple variables at once 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:

x=2336
y=2
z=80000
w=10
s=(2*x-w)
t=x/w
l=0
m=0

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))

what_im_looking_for(t,t+20000)


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 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

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


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)

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

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.

Sort by » oldest newest most voted Your best route would be to learn some Python before dealing with Sage. Think Python is a nice introductory book: https://greenteapress.com/wp/think-py...

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

divisors(36)^2


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

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.