Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Discussion Basic Fractal Numbers

Discussion in 'Scripting' started by fournicknet, Jan 20, 2023.

  1. fournicknet

    fournicknet

    Joined:
    Jan 8, 2013
    Posts:
    13
    The following is code that I created to create my fractal nubmers, sharing it with the community if it will help. I can get it to go up to 91 iterations with out breaking because of the 64 bit limitation. I'm sure there is another way to get above 91 iterations and if you do I would encourage you to respond to this post if you feel inclined to do so.

    Code (CSharp):
    1. internal class Program
    2.     {
    3.         static private long firstNumber, secondNumber, result;
    4.         static void Main(string[] args)
    5.         {
    6.             firstNumber = 0;
    7.             secondNumber = 1;
    8.             result = 0;
    9.  
    10.             for (int i = 0; i < 100; i++)
    11.             {
    12.                 result = firstNumber + secondNumber;
    13.                 Console.WriteLine("Level " + i + " is " + result);
    14.                 secondNumber = firstNumber;
    15.                 firstNumber = result;
    16.             }
    17.             Console.ReadLine();
    18.         }
    19. }
     
    Colonel_Campbell likes this.
  2. jasonboukheir3

    jasonboukheir3

    Joined:
    Apr 13, 2022
    Posts:
    81
    This is a variant of the Fibonacci sequence. Except it's slightly different because you set the
    secondNumber = firstNumber
    instead of
    secondNumber = result
    , which just delays the sequence by 1 (the first iteration swaps
    firstNumber
    and
    secondNumber
    .

    ...yeah...
     
    fournicknet and Colonel_Campbell like this.
  3. fournicknet

    fournicknet

    Joined:
    Jan 8, 2013
    Posts:
    13
    I took it for granted that it worked after line 20, good catch.
     
    Colonel_Campbell likes this.
  4. fournicknet

    fournicknet

    Joined:
    Jan 8, 2013
    Posts:
    13
    It is if you are using it for GameDevelopment, like a leveling system