SRL Archive - SRL Snippets

Highest Common Factor


JennyCocoX

2013-09-27 09:01:52

Calculates the HCF for Runescape botting.

program FindingHCF;

const
Debug = True;
x = 623412;
y = 423348;

//calculates HCF of two integers x,y

Function HCF(x,y: int64):Integer;
var
i: Integer;
begin
repeat
begin
Inc(i);
If (x >= y) then
begin
If debug then
begin
Writeln('x = ' + IntToStr(x));
Writeln('y = ' + IntToStr(y));
Writeln('x mod y = ' + IntToStr(x mod y));
If i = 1 then
begin
Writeln('Completed ' + IntToStr(i) + ' calculation step.');
end
else
Writeln('Completed ' + IntToStr(i) + ' calculation steps.');
end;
x := (x mod y);
end
else
begin
if debug then
begin
Writeln('x = ' + IntToStr(x));
Writeln('y = ' + IntToStr(y));
Writeln('y mod x = ' + IntToStr(y mod x));
If i = 1 then
begin
Writeln('Completed ' + IntToStr(i) + ' calculation step.');
end
else
Writeln('Completed ' + IntToStr(i) + ' calculation steps.');
end;
y := (y mod 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 HCF is ' + IntToStr(HCF(x,y)) +'.');
end.

:(
[PHP]Compiled successfully in 16 ms.
x = 623412
y = 423348
The HCF is 12.
Successfully executed.
[/PHP]

With debugging
[PHP]Compiled successfully in 16 ms.
x = 623412
y = 423348
x mod y = 200064
Completed 1 calculation step.
x = 200064
y = 423348
y mod x = 23220
Completed 2 calculation steps.
x = 200064
y = 23220
x mod y = 14304
Completed 3 calculation steps.
x = 14304
y = 23220
y mod x = 8916
Completed 4 calculation steps.
x = 14304
y = 8916
x mod y = 5388
Completed 5 calculation steps.
x = 5388
y = 8916
y mod x = 3528
Completed 6 calculation steps.
x = 5388
y = 3528
x mod y = 1860
Completed 7 calculation steps.
x = 1860
y = 3528
y mod x = 1668
Completed 8 calculation steps.
x = 1860
y = 1668
x mod y = 192
Completed 9 calculation steps.
x = 192
y = 1668
y mod x = 132
Completed 10 calculation steps.
x = 192
y = 132
x mod y = 60
Completed 11 calculation steps.
x = 60
y = 132
y mod x = 12
Completed 12 calculation steps.
x = 60
y = 12
x mod y = 0
Completed 13 calculation steps.
The HCF is 12.
Successfully executed.[/PHP]
Still only 100% real work with smallers number because int64 is a disabled person.

rj

2013-09-27 15:02:27

Wait what do you mean

Calculates the HCF for Runescape botting.

Le Jingle

2013-09-27 16:27:03


function gcd(a, b: Integer): Integer;
var c: Integer;
begin
while (a <> 0) do
begin
c := a;
a := b mod a;
b := c;
end;
Result := b;
end;

begin
WriteLn(gcd(623412, 423348));
end.

?

The Killer

2013-09-27 17:08:55

Err when would you need the HCF in runescape? (genuine question)

masterBB

2013-09-27 18:16:11

From: Le Jingle

function gcd(a, b: Integer): Integer;
var c: Integer;
begin
while (a <> 0) do
begin
c := a;
a := b mod a;
b := c;
end;
Result := b;
end;

begin
WriteLn(gcd(623412, 423348));
end.

?


In asm/c++ my solution might be faster:
function MCD(a, b:Integer): Integer;
var
d: Integer;
begin
while ((a and 1 = false) and (b and 1 = false)) do
begin
inc(d);
a := a shr 1;
b := b shr 1;
end;

while (a and 1 = false) do
a := a shr 1;

while(true) do
begin
while (b and 1 = false) do
b := b shr 1;


if (a = b) then
break;

if (a > b) then
begin
a := a xor b;
b := a xor b;
a := a xor b;
end;

if (a = 1) then
break;

b := b - a;
end;

result := a shl d;
end;

But I think Jenny his solution is good enough for whatever he wants to use it for.

JennyCocoX

2013-09-28 19:03:34

From: The Killer
Err when would you need the HCF in runescape? (genuine question)


demand and supply my dear

demand and supply…

There is nothing more I can tell you though. ;) Wouldn't be wanting to give away my magic recipe for botting.