This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-12-10 05:25:00

john100--
New Scratcher
Registered: 2012-12-10
Posts: 5

Euclid's algorithm

I have to write Euclid's algorithm in Scratch. I do not know how to do it. Could someone help me?

Offline

 

#2 2012-12-10 05:55:24

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: Euclid's algorithm

Well, it is actually pretty easy; all you need is a loop and the mod block.

I didn't test this script, it's off the top of my head, but it should work.  smile

set [num1 v] to [0] // The first number
set [num2 v] to [0] // The second number
if <(num1) > (num2)>
 set [dir v] to [1]
else
 set [dir v] to [0]
end
repeat until <<<((num1) mod (num2)) = [0]> and <(dir) = [1]>> or <<((num2) mod (num1)) = [0]> and <(dir) = [0]>>>
 if <(dir) = [0]>
  set [num1 v] to ((num1) mod (num2))
  set [dir v] to [1]
 else
  set [num2 v] to ((num2) mod (num1))
  set [dir v] to [0]
 end
end
if <(dir) = [0]>
 set [answer v] to (num2)
else
 set [answer v] to (num1)
end

Offline

 

#3 2012-12-10 06:05:09

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: Euclid's algorithm

I made a mistake in the above script and reported it to be fixed since I can't edit yet. Sorry 'bout that, i switched the num1 and num2 reporters in the last if/else. If you see "last edited by" in italics at the bottom of the psot, it has been fixed.  smile

Offline

 

#4 2012-12-10 06:15:22

jontmy00
Scratcher
Registered: 2011-11-28
Posts: 1000+

Re: Euclid's algorithm

Gravitation wrote:

I made a mistake in the above script and reported it to be fixed since I can't edit yet. Sorry 'bout that, i switched the num1 and num2 reporters in the last if/else. If you see "last edited by" in italics at the bottom of the psot, it has been fixed.  smile

Why don't you make some projects to be a Scratcher?

[offtopic]Look at the text in red. TBG time!  tongue [/offtopic]


FOR ALL THE NEWS ON UPDATES FOR SIMPLISTICRAFT, CLICK HERE.

Offline

 

#5 2012-12-10 07:03:13

john100--
New Scratcher
Registered: 2012-12-10
Posts: 5

Re: Euclid's algorithm

Thank you very much. Everything works. You save my life. I still have a harder second-Extended Euclidean algorithm. Someone has an idea how to do it?

Offline

 

Board footer