• Das kreative Thema des Monats Oktober wird vor allem diejenigen unter Euch besonders ansprechen, denen Do-it-yourself quasi im Blut liegt. Zeigt uns Eure Improvisationskünste oder fotografiert, was andere Leute sich so im Alltag zusammengestrickt haben. --> Zum Mitmachen bitte hier klicken

Erweiterter Euklidischer Algorithmus

mac@engelthal

Rheinischer Krummstiel
Registriert
15.03.07
Beiträge
386
Hallo zusammen,
kann mir jemand helfen, diesen Algorithmus in Apple Script zu formulieren? Wäre echt cool, wenn mir da jemand helfen könnte.
Gruß,
--Alex
 
Habe die Lösung gefunden:

Code:
on extEucl(a, b)
	set aa to a
	set bb to b
	
	set x0 to 1
	set x1 to 0
	set y0 to 0
	set y1 to 1
	
	set x to 0
	set y to 1
	
	if b = 0 then
		set x to 1 --Alpha
		set y to 0 --Beta
		set b to a --gcD
	else
		set q to a div b
		set r to a mod b
		
		repeat while r is not 0
			set x to x0 - q * x1
			set y to y0 - q * y1
			
			set x0 to x1
			set y0 to y1
			
			set x1 to x
			set y1 to y
			
			set a to b
			set b to r
			
			set q to a div b
			set r to a mod b
		end repeat
	end if
	
	if x < 0 then
		set x to bb + x
		set y to (1 - aa * x) / bb
	end if
	
	return x
end extEucl