Page 1 of 1

Strange problem with algorithm

Posted: Wed Aug 13, 2008 9:26 pm
by NuclearSilo
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)

$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? :?

Re: Strange problem with algorithm

Posted: Wed Aug 13, 2008 10:27 pm
by CloudStrider
NuclearSilo wrote: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)

$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? :?



I suck at that, but good luck Silo.

Re: Strange problem with algorithm

Posted: Wed Aug 13, 2008 11:40 pm
by LaloHao
$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


Try

Code: Select all

$text = InputBox("","")
$array = _StringSplit($text,"/")
$a = $array[0]
$b = $array[1]
MsgBox(0,"","pgcd de " & $a & " et " & $b & " est: ")

While ($a % $b) != 0
  $c = $b;
  $b = $a % $b;
  $a = $c;
WEnd
MsgBox(0,"",$b)

Dont know if it'll work.
BTW what language is that? AutoIT?

Re: Strange problem with algorithm

Posted: Wed Aug 13, 2008 11:42 pm
by Motorola
Image

Re: Strange problem with algorithm

Posted: Wed Aug 13, 2008 11:44 pm
by CloudStrider
LOL! LOL!

Motor man that was some funny stuff, That gets picture of the year in my books, except maybe some pictures in the Hot or Not thread.

Re: Strange problem with algorithm

Posted: Thu Aug 14, 2008 12:28 am
by PB_and_J
icealya pic > silo pic