Strange problem with algorithm

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Strange problem with algorithm

Post 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? :?
Playing Age of Wushu, dota IMBA

User avatar
CloudStrider
Advanced Member
Posts: 2125
Joined: Tue Jul 15, 2008 2:45 pm
Quick Reply: Yes
Location: Away

Re: Strange problem with algorithm

Post 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.
penfold1992 wrote:durka durka muhammad gihad allah 10k plys. thats all i hear :S

Image Image
Mad props to Verfo for the sig, ty!

User avatar
LaloHao
Loyal Member
Posts: 1987
Joined: Thu Nov 22, 2007 10:59 pm
Quick Reply: Yes
Location: Mexico

Re: Strange problem with algorithm

Post 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?
Last edited by LaloHao on Thu Aug 14, 2008 12:01 am, edited 1 time in total.

User avatar
Motorola
Casual Member
Posts: 55
Joined: Tue Jul 29, 2008 7:47 pm
Quick Reply: Yes
Location: Work..

Re: Strange problem with algorithm

Post by Motorola »

Image
Image
Click here to rub my wiener!
Free Sex In Return!

User avatar
CloudStrider
Advanced Member
Posts: 2125
Joined: Tue Jul 15, 2008 2:45 pm
Quick Reply: Yes
Location: Away

Re: Strange problem with algorithm

Post 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.
penfold1992 wrote:durka durka muhammad gihad allah 10k plys. thats all i hear :S

Image Image
Mad props to Verfo for the sig, ty!

User avatar
PB_and_J
Ex-Staff
Posts: 2639
Joined: Sun Jan 01, 2006 2:18 am
Location: 4 hour jack sessions with stallowned

Re: Strange problem with algorithm

Post by PB_and_J »

icealya pic > silo pic
Image

Post Reply

Return to “Off Topic Lounge”