From ae19ce25a7dad3d9365f21d468da1b6c55bf19ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Sun, 21 Sep 2025 11:23:52 +0200 Subject: [PATCH] Implement lookup-based Mod10 for GPU compatibility --- PerfectNumbers.Core/ULongExtensions.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/PerfectNumbers.Core/ULongExtensions.cs b/PerfectNumbers.Core/ULongExtensions.cs index 77f4241..118617d 100644 --- a/PerfectNumbers.Core/ULongExtensions.cs +++ b/PerfectNumbers.Core/ULongExtensions.cs @@ -137,8 +137,29 @@ public static class ULongExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong Mod10(this ulong value) { - ulong quotient = (ulong)(((UInt128)value * 0xCCCCCCCCCCCCCCCDUL) >> 67); - return value - quotient * 10UL; + ulong parity = value & 1UL; + ulong mod5 = value.Mod5(); + + if (parity == 0UL) + { + return mod5 switch + { + 0UL => 0UL, + 1UL => 6UL, + 2UL => 2UL, + 3UL => 8UL, + _ => 4UL, + }; + } + + return mod5 switch + { + 0UL => 5UL, + 1UL => 1UL, + 2UL => 7UL, + 3UL => 3UL, + _ => 9UL, + }; } [MethodImpl(MethodImplOptions.AggressiveInlining)]