SRL Archive - OSR Help

common denominator script


JennyCocoX

2013-09-26 16:08:25

STATUS = ONGOING SCRIPT - MAYBE STILL NEED A HELPING ^_^

I am trying to find the most common denominator using this script but I get the following error message:
Exception in Script: Found unexpected token ";", expected "Colon" at line 3, column 27


program FindingGGT;

Function ggT(x,y: Integer);
begin
repeat
begin
If (x >= y) then
Writeln(x-y);
If (x < y) then
Writeln(y-x);
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

Please help O.O :D:norris::norris::norris::norris::norris::norris::norris::norris::norris:

tealc

2013-09-26 16:31:01

From: JennyCocoX
I am trying to find the most common denominator using this script but I get the following error message:
Exception in Script: Found unexpected token ";", expected "Colon" at line 3, column 27


program FindingGGT;

Function ggT(x,y: Integer);
begin
repeat
begin
If (x >= y) then
Writeln(x-y);
If (x < y) then
Writeln(y-x);
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

Please help O.O :D:norris::norris::norris::norris::norris::norris::norris::norris::norris:


A function needs a return value, the example below will compile. But it won't give you the correct common denominator.


program FindingGGT;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
Writeln(x-y);
If (x < y) then
Writeln(y-x);
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

or if you don't want to return a value



program FindingGGT;

Procedure ggT(x,y: Integer);
begin
repeat
begin
If (x >= y) then
Writeln(x-y);
If (x < y) then
Writeln(y-x);
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

JennyCocoX

2013-09-26 17:46:02

Thanks for the reply! :)

From: tealc
….


program FindingGGT;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
begin
Writeln(x-y);
(x-y)=x;
[COLOR="#FF0000"] end;[/COLOR]
If (x < y) then
begin
Writeln(y-x);
(y-x)=y;
end;
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

gives me error
[Error] (11:18): Internal error (20) at line 11
Compiling failed.

Any ideas? I highlighted the part where it gives the error. Just trying to assign x-y to x so x keeps getting a new value or y depending on which one is bigger.
Euclidean algorithm is what I try to do here. It's not the best approach in this case since e.g. if x is 1000000000 and y 1 it'll take some time so I'm going to do division with remainder e.g. a = bq + r where r = b mod q etc… and a, b are integers, but I want to get this right first before I make the whole function more complicated.

tl;dr Need to know why I'm unable to make x-y = x plus how to fix error .

My Simba so rusty. :norris:

tealc

2013-09-26 20:10:23

From: JennyCocoX
Thanks for the reply! :)



program FindingGGT;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
begin
Writeln(x-y);
(x-y)=x;
[COLOR="#FF0000"] end;[/COLOR]
If (x < y) then
begin
Writeln(y-x);
(y-x)=y;
end;
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

gives me error
[Error] (11:18): Internal error (20) at line 11
Compiling failed.

Any ideas? I highlighted the part where it gives the error. Just trying to assign x-y to x so x keeps getting a new value or y depending on which one is bigger.
Euclidean algorithm is what I try to do here. It's not the best approach in this case since e.g. if x is 1000000000 and y 1 it'll take some time so I'm going to do division with remainder e.g. a = bq + r where r = b mod q etc… and a, b are integers, but I want to get this right first before I make the whole function more complicated.

tl;dr Need to know why I'm unable to make x-y = x plus how to fix error .

My Simba so rusty. :norris:


to assign the value of x-y to x you would use x:=x-y

:= is used to assign a value or reference to a variable
ie. x:=x-y

= is used for comparing
ie. if(x=y) then writeln(x and y are equal);

JennyCocoX

2013-09-27 03:26:34

I still get

[Error] (11:16): Internal error (20) at line 11
Compiling failed.

Changed my code a bit, but I still get this stupid error. Please can someone just tell me what I'm doing wrong?

program FindingGGT;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
begin
Writeln(x-y);
(x-y):=x;
end
else
begin
Writeln(y-x);
(y-x):=y;
end;
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.

Sjoe

2013-09-27 03:32:57

From: JennyCocoX
I still get

[Error] (11:16): Internal error (20) at line 11
Compiling failed.

Changed my code a bit, but I still get this stupid error. Please can someone just tell me what I'm doing wrong?

program FindingGGT;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
begin
Writeln(x-y);
(x-y):=x;
end
else
begin
Writeln(y-x);
(y-x):=y;
end;
end;
until (y=0) or (x=0);
end;

begin
ggT(693,286);
end.


(x-y):=x; should be x := (x-y);
(y-x):=y; should be y := (y-x);

edit:

repeat
until (y=0) or (x=0);
seems like an infinite loop to me

JennyCocoX

2013-09-27 03:44:58

Compiled successfully in 16 ms.
The most common denominator is 11.
Successfully executed.

From: Sjoe
…

Hey Sjoe thanks for the quick reply.
That really helped!
Script fully functional now and I'll be releasing a more advanced version soon. :)
As for infinite loop, no it can never be an infinite loop, since all integer values are divisible by 1 and x will be equal to 0 or y equal to 0 in all cases.
Feel free to try it out - I'm going to make a more complex one - this one might be a bit slow if you use very large numbers :)

program FindingGGT;

const
Debug = False;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
begin
If debug then
Writeln(x-y);
x := (x-y);
end
else
begin
if debug then
Writeln(y-x);
y := (y-x);
end;
end;
until (y=0) or (x=0);

If x>y then
Result := x;
If y>x then
Result := y;
end;

begin
Writeln('The most common denominator is ' + IntToStr(ggT(693,286))+ '.');
end.

EHHH???

I put in 643264326432,56423643264
and get 0 as the most common denominator. >_> How is that even possible??
help help help it seems to be doing 100% success fine with smaller numbers though. 412,4444 most common is 4 just got that :D

tealc

2013-09-27 06:19:25

From: JennyCocoX
Compiled successfully in 16 ms.
The most common denominator is 11.
Successfully executed.


Hey Sjoe thanks for the quick reply.
That really helped!
Script fully functional now and I'll be releasing a more advanced version soon. :)
As for infinite loop, no it can never be an infinite loop, since all integer values are divisible by 1 and x will be equal to 0 or y equal to 0 in all cases.
Feel free to try it out - I'm going to make a more complex one - this one might be a bit slow if you use very large numbers :)

program FindingGGT;

const
Debug = False;

Function ggT(x,y: Integer):Integer;
begin
repeat
begin
If (x >= y) then
begin
If debug then
Writeln(x-y);
x := (x-y);
end
else
begin
if debug then
Writeln(y-x);
y := (y-x);
end;
end;
until (y=0) or (x=0);

If x>y then
Result := x;
If y>x then
Result := y;
end;

begin
Writeln('The most common denominator is ' + IntToStr(ggT(693,286))+ '.');
end.

EHHH???

I put in 643264326432,56423643264
and get 0 as the most common denominator. >_> How is that even possible??
help help help it seems to be doing 100% success fine with smaller numbers though. 412,4444 most common is 4 just got that :D


the 0 from large numbers is because of the way numbers are stored.


if you change
Function ggT(x,y: Integer):Integer;
to
Function ggT(x,y: int64):Integer;
then 643264326432,56423643264 will work

int64 will work up to around 19 digits