Skip to content

Commit

Permalink
Create FibonacciNode.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlinka committed Jun 25, 2024
1 parent 047d1f1 commit 9ee49fe
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Elements.Core;
using FrooxEngine.ProtoFlux;
using ProtoFlux.Core;
using ProtoFlux.Runtimes.Execution;

namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math
{
[NodeCategory("Obsidian/Math")]
[NodeName("Fibonacci")]
public class FibonacciNode : ValueFunctionNode<FrooxEngineContext, int>
{
public ValueInput<int> Input;

protected override int Compute(FrooxEngineContext context)
{
int n = Input.Evaluate(context);
return Fibonacci(n);
}

private int Fibonacci(int n)
{
if (n < 0)
throw new ArgumentException("Negative numbers are not allowed.");
if (n == 0)
return 0;
if (n == 1)
return 1;

int a = 0, b = 1, temp;
for (int i = 2; i <= n; i++)
{
temp = a + b;
a = b;
b = temp;
}
return b;
}
}
}

0 comments on commit 9ee49fe

Please sign in to comment.