SRL Archive - SRL Archives

Math.scar


Blender

2009-07-15 01:45:21

Yeah, so I just took some math classes and found out that SCAR has nothing included that finds the GCM of anything.

I just mad a bunch of random (but useful) functions that have to do with math:

{*******************************************************************************
function GCF(a, b: Integer): Integer;
By: Blender
Description: Finds the Greatest Common Factor (GCF) of a and b.
*******************************************************************************}
function GCF(a, b: Integer): Integer;
var t: Integer;

begin
while not(b = 0) do
begin
t := b;
b := a mod b;
a := t;
end;

Result := a;
end;

{*******************************************************************************
function GCD(a, b: Integer): Integer;
By: Blender
Description: Finds the Greatest Common Denominator (GCD) of a and b.
*******************************************************************************}
function GCD(a, b: Integer): Integer;
begin
Result := GCF(a, b);
end;

{*******************************************************************************
function LCM(a, b: Integer): Integer;
By: Blender
Description: Finds the Least Common Multiple (LCM) of a and b.
*******************************************************************************}
function LCM(a, b: Integer): Integer;
var c: Extended;
begin
c := abs(a) / GCD(a, b);
Result := StrToInt(FloatToStr(c * abs(b)));
end;

Awkwardsaw

2009-07-15 01:53:53

hmm, couldnt it just be:

function GCF(a, b: Integer): Integer;
var t: Integer;
begin
while not(b = 0) do
t := a mod b;
Result := t;
end;

since, a, b, and t are all set to the same thing pretty much.

t was = to b,
b was set to a mod b,
and a was set to t.

i havnt used GCF for ever though, i may be wrong ;)

im not on the correct cpu so i cant use SCAR atm :p

senrath

2009-07-15 01:59:11

GCD is pointless, since it's just calling GCF and doing nothing else. You can also replace this:

Result := StrToInt(FloatToStr(c * abs(b)));
With this:

Result := Ceil(c * abs(b));

Blender

2009-07-15 01:59:30

Naw, see the modulus 'm' of 'n' is the number of times 'n' goes into 'm', with a remainder. The modulus is the remainder.

The GCF is repeating the process, until 'n' is equal to zero ('n' divides 'm' evenly). When 'n' is zero, then 't' is therefore the GCF.

It's hard to explain, really!


Oh, and senrath, people call GCF a couple of things. The only one that I ever heard was GCD, so I just made two of them.

In the SCAR manual, there isn't a FloatToInt() function. That was the only conversion that I could find.

ian.

2009-07-15 02:01:48

From: senrath
GCD is pointless, since it's just calling GCF and doing nothing else. You can also replace this:

Result := StrToInt(FloatToStr(c * abs(b)));
With this:

Result := Ceil(c * abs(b));


>.> Tons of functions in SRL just call one thing. (Result := Something;..)

And Awkward, what you posted doesn't work ;)

senrath

2009-07-15 02:02:40

From: 99_ aka ian.
>.> Tons of functions in SRL just call one thing. (Result := Something;..)


Yes, but they don't have the exact same parameters and pass them in the exact same manner.

ian.

2009-07-15 02:07:22

From: senrath
Yes, but they don't have the exact same parameters and pass them in the exact same manner.


=X haha I messed around with them so they were different, so I didn't see the problem. But yes, they're the same thing ;)