Fixed negative GCD and allow Int64 for GCD and LCM

This commit is contained in:
Stefan Müller 2024-01-23 12:21:43 +01:00 committed by Stefan Müller
parent 571019d604
commit cccf5693f7
1 changed files with 8 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{ {
Solutions to the Advent Of Code. Solutions to the Advent Of Code.
Copyright (C) 2023 Stefan Müller Copyright (C) 2023-2024 Stefan Müller
This program is free software: you can redistribute it and/or modify it under This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software the terms of the GNU General Public License as published by the Free Software
@ -30,18 +30,20 @@ type
TNumberTheory = class TNumberTheory = class
public public
class function GreatestCommonDivisor(AValue1, AValue2: Integer): Integer; class function GreatestCommonDivisor(AValue1, AValue2: Int64): Int64;
class function LeastCommonMultiple(AValue1, AValue2: Integer): Integer; class function LeastCommonMultiple(AValue1, AValue2: Int64): Int64;
end; end;
implementation implementation
{ TNumberTheory } { TNumberTheory }
class function TNumberTheory.GreatestCommonDivisor(AValue1, AValue2: Integer): Integer; class function TNumberTheory.GreatestCommonDivisor(AValue1, AValue2: Int64): Int64;
var var
t: Integer; t: Int64;
begin begin
AValue1 := Abs(AValue1);
AValue2 := Abs(AValue2);
while AValue2 <> 0 do while AValue2 <> 0 do
begin begin
t := AValue2; t := AValue2;
@ -51,7 +53,7 @@ begin
Result := AValue1; Result := AValue1;
end; end;
class function TNumberTheory.LeastCommonMultiple(AValue1, AValue2: Integer): Integer; class function TNumberTheory.LeastCommonMultiple(AValue1, AValue2: Int64): Int64;
begin begin
Result := (Abs(AValue1) div GreatestCommonDivisor(AValue1, AValue2)) * Abs(AValue2); Result := (Abs(AValue1) div GreatestCommonDivisor(AValue1, AValue2)) * Abs(AValue2);
end; end;