From 047d1f1fb98835fb7c738e84457428b806a16edf Mon Sep 17 00:00:00 2001 From: xLinka Date: Tue, 25 Jun 2024 14:29:53 +0100 Subject: [PATCH] Eulerstoient node and IsPrimeNumber --- .../Math/EulersTotientFunctionNode.cs | 28 +++++++++++++ .../ProtoFlux/Math/IsPrimeNumberNode.cs | 40 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 ProjectObsidian/ProtoFlux/Math/EulersTotientFunctionNode.cs create mode 100644 ProjectObsidian/ProtoFlux/Math/IsPrimeNumberNode.cs diff --git a/ProjectObsidian/ProtoFlux/Math/EulersTotientFunctionNode.cs b/ProjectObsidian/ProtoFlux/Math/EulersTotientFunctionNode.cs new file mode 100644 index 0000000..0955287 --- /dev/null +++ b/ProjectObsidian/ProtoFlux/Math/EulersTotientFunctionNode.cs @@ -0,0 +1,28 @@ +using Elements.Core; +using FrooxEngine.ProtoFlux; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math +{ + [NodeCategory("Obsidian/Math")] + [NodeName("EulersTotientFunction")] + public class EulersTotientFunctionNode : ValueFunctionNode + { + public ValueInput Input; + + protected override int Compute(FrooxEngineContext context) + { + var result = Input.Evaluate(context); + var inputCopy = result; + for (var p = 2; p * p <= inputCopy; ++p) + { + if (inputCopy % p != 0) continue; + while (inputCopy % p == 0) inputCopy /= p; + result -= result / p; + } + if (inputCopy > 1) result -= result / inputCopy; + return result; + } + } +} diff --git a/ProjectObsidian/ProtoFlux/Math/IsPrimeNumberNode.cs b/ProjectObsidian/ProtoFlux/Math/IsPrimeNumberNode.cs new file mode 100644 index 0000000..17197db --- /dev/null +++ b/ProjectObsidian/ProtoFlux/Math/IsPrimeNumberNode.cs @@ -0,0 +1,40 @@ +using Elements.Core; +using FrooxEngine.ProtoFlux; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math +{ + [NodeCategory("Obsidian/Math")] + [NodeName("IsPrimeNumber")] + public class IsPrimeNumberNode : ValueFunctionNode + { + public ValueInput Input; + + protected override bool Compute(FrooxEngineContext context) + { + var num = Input.Evaluate(context); + switch (num) + { + case < 2: + return false; + case 2: + return true; + default: + { + if (num % 2 == 0) return false; + break; + } + } + double sqrtNum = MathX.Sqrt(num); + for (var i = 3; i <= sqrtNum; i += 2) + { + if (num % i == 0) + { + return false; + } + } + return true; + } + } +}