Strange problem with algorithm
Posted: Wed Aug 13, 2008 9:26 pm
I made a script to reduce a fraction using PGCD method (in english GCD, greatest common divisor), basing on this:
PGCD(a,b) = PGCD(b,a-b)
The main code is in bold. When i execute it, the result is like this:
4/8 -> 1/2
10/5 -> 2
24/30 -> 4/5
But the strange thing is if i divide something (less than 100) to 100, the script will be an endless loop, trapped inside the bold area. And when comepare it says that number is greater than 100.
Any idea?
PGCD(a,b) = PGCD(b,a-b)
$text = InputBox("","")
$array = _StringSplit($text,"/")
$a = $array[0]
$b = $array[1]
MsgBox(0,"","pgcd de " & $a & " et " & $b & " est")
If $a = $b Then
MsgBox(0,"","res = 1")
EndIf
While $a <> $b
If $a > $b Then
$a = $a - $b
Else
$b = $b - $a
EndIf
WEnd
$pgcd = $a
MsgBox(0,"",$a)
$array[0] /= $a
$array[1] /= $b
$res = $array[0] / $array[1]
If IsInt($res) Then
MsgBox(0,"",$res)
Else
MsgBox(0,"",$array[0] & "/" & $array[1])
EndIf
The main code is in bold. When i execute it, the result is like this:
4/8 -> 1/2
10/5 -> 2
24/30 -> 4/5
But the strange thing is if i divide something (less than 100) to 100, the script will be an endless loop, trapped inside the bold area. And when comepare it says that number is greater than 100.
Any idea?
