From cccf5693f7105c4274f83b484442b16e6ab81520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Tue, 23 Jan 2024 12:21:43 +0100 Subject: [PATCH] Fixed negative GCD and allow Int64 for GCD and LCM --- UNumberTheory.pas | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/UNumberTheory.pas b/UNumberTheory.pas index f46ce3e..99b7fd5 100644 --- a/UNumberTheory.pas +++ b/UNumberTheory.pas @@ -1,6 +1,6 @@ { 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 the terms of the GNU General Public License as published by the Free Software @@ -30,18 +30,20 @@ type TNumberTheory = class public - class function GreatestCommonDivisor(AValue1, AValue2: Integer): Integer; - class function LeastCommonMultiple(AValue1, AValue2: Integer): Integer; + class function GreatestCommonDivisor(AValue1, AValue2: Int64): Int64; + class function LeastCommonMultiple(AValue1, AValue2: Int64): Int64; end; implementation { TNumberTheory } -class function TNumberTheory.GreatestCommonDivisor(AValue1, AValue2: Integer): Integer; +class function TNumberTheory.GreatestCommonDivisor(AValue1, AValue2: Int64): Int64; var - t: Integer; + t: Int64; begin + AValue1 := Abs(AValue1); + AValue2 := Abs(AValue2); while AValue2 <> 0 do begin t := AValue2; @@ -51,7 +53,7 @@ begin Result := AValue1; end; -class function TNumberTheory.LeastCommonMultiple(AValue1, AValue2: Integer): Integer; +class function TNumberTheory.LeastCommonMultiple(AValue1, AValue2: Int64): Int64; begin Result := (Abs(AValue1) div GreatestCommonDivisor(AValue1, AValue2)) * Abs(AValue2); end;