Hi. I have just a little piece of math code for prime decomposition (see below) and I can’t find why I’m getting just one right answer, followed by nil values (which should not be). I think it is about the variable definitions, but my “code-intuition” is failing right now #-o

Thanks in advance.

-- Use this function to perform your initial setup
function setup()
s,w={},{}
s[0],w[0]=2,2
N=221
p=0
for i=1,6 do
s[i]=(s[i-1]^2+1)%N
e=(w[i-1]^2+1)%N
w[i]=(e^2+1)%N
p=mcd(math.abs(s[i]-w[i]),N)
print(p)
end
end
function draw()
background(40, 40, 50)
strokeWidth(5)
end
function mcd(a,b)
--Euclid's greatest common divisor
for i = 1,20 do
if a~=0 and r~=0 then
r=a%b
a=b
b=r
if r==0 then
return a
end
end
end
end

No, Lua tables can start from any number, and the code looks ok to me

@quezadav - I get stuck like this all the time. It takes patience and sometimes a lot of “print” testing before I find the bug. Are you sure you copied the algorithm correctly?

Oh alright my mistake
EDIT: from a second glance, the code in the mcd(a,b) function only returns a value if r==0, which only occurs once. Is this intentional? It seems to be the root of the issue.

The result is ok and more 1.0 shloud follow the first one. It’s part of the test and the 1.0 indicates everything is fine for the six runs of the loop in the setup function.

@quezadav I’m not sure what’s wrong with your code, but here’s one I found online. I made minor changes so it would run here. Continue to work on your code because that’s where the fun is and that’s where you’ll learn the most. I find debugging code more interesting than playing games. I won’t have time until tomorrow to look at it, and I’m sure someone will fix it by then.

function setup()
value=123456789
print(table.concat(PrimeDecomposition(value)," "))
end
function PrimeDecomposition( n )
local f = {}
if IsPrime( n ) then
f[1] = n
return f
end
local i = 2
repeat
while n % i == 0 do
f[#f+1] = i
n = n / i
end
repeat
i = i + 1
until IsPrime( i )
until n == 1
return f
end
function IsPrime( n )
if n <= 1 or ( n ~= 2 and n % 2 == 0 ) then
return false
end
for i = 3, math.sqrt(n), 2 do
if n % i == 0 then
return false
end
end
return true
end

@dave1707, thanks. I agree. Actually I’m coding the algorithm just for fun: coding is my second favorite hobby (after book reading) and Codea is my drug. I just started a couple of days ago with the problems of Project Euler (which I fully recommend to all Codea users) and it has been a lot of fun using Codea to solve some of them. I’ve been through a lot of more complicated stuff (found in the mentioned Project) with pain but success… until now, with this little piece of… code 8-}

The value of r is persisting between calls to mcd. So once it hits zero, it stays there. Avoid global variables where they don’t need to be global.

-- Use this function to perform your initial setup
function setup()
local s,w,N,p,e
s,w={},{}
s[0],w[0]=2,2
N=221
p=0
for i=1,6 do
s[i]=(s[i-1]^2+1)%N
e=(w[i-1]^2+1)%N
w[i]=(e^2+1)%N
p=mcd(math.abs(s[i]-w[i]),N)
print(p)
end
end
function draw()
background(40, 40, 50)
strokeWidth(5)
end
function mcd(a,b)
--Euclid's greatest common divisor
local r
for i = 1,20 do
if a~=0 and r~=0 then
r=a%b
a=b
b=r
if r==0 then
return a
end
end
end
end

@quezadav Nice video. Do you get the correct length when you run it using the example ellipse given in PE 525. I’m trying to work on a math solution, not sure if I’ll solve it or not. Are you truely rolling the ellipse, or are you moving it a certain distance and rotating it to give the appearance that it’s rolling. Unlike a circle, the ellipse will move different distances given the same rotation angle.

@dave1707, I’m not finished yet: I’ve done just the harder part (rolling the ellipse) and stopped to clean my “spagetti” and swaped to other PE problems. The last part (the curve’s lenght calculation) will not pose any trouble, but I left it for later.

Yes, in fact, the ellipse rolls because of the translation and rotation imposed on it: every y is evaluated in terms of the rotation angle, which in turn isn’t constant but dependent on x.

The problem can be purely solved mathematically with a symbolic math software, but I thought, it would be funnier to do it with my lovely Codea. Actually, I began trying to solve the problem using a “physical” ellipse instead of a “drawn” one. I’ll post a video of my draft about it in a couple of minutes. ~O)

@dave1707, here is a video of my “physical” ellipse’s draft. The rotation and translation were automatically imposed by the physics engine of Codea because I constructed a physical body http://youtu.be/h9xaXA4fpoc

@quezadav Nice demo. I was thinking if I couldn’t solve this with math then I would create the physics body which you already did. You need to add the plot of the ellipse center when it hits the floor and rolls along in your next demo.