Skip to content

Commit

Permalink
Eulerstoient node and IsPrimeNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlinka committed Jun 25, 2024
1 parent a5c335b commit 047d1f1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ProjectObsidian/ProtoFlux/Math/EulersTotientFunctionNode.cs
Original file line number Diff line number Diff line change
@@ -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<FrooxEngineContext, int>
{
public ValueInput<int> 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;
}
}
}
40 changes: 40 additions & 0 deletions ProjectObsidian/ProtoFlux/Math/IsPrimeNumberNode.cs
Original file line number Diff line number Diff line change
@@ -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<FrooxEngineContext, bool>
{
public ValueInput<int> 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;
}
}
}

0 comments on commit 047d1f1

Please sign in to comment.