How to resolve the algorithm Factorial step by step in the Haxe programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factorial step by step in the Haxe programming language

Table of Contents

Problem Statement

Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative   n   errors is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factorial step by step in the Haxe programming language

Source code in the haxe programming language

static function factorial(n:Int):Int {
  var result = 1;
  while (1<n)
    result *= n--;
  return result;
}

static function factorial(n:Int):Int {
  return n == 0 ? 1 : n * factorial2(n - 1);
}

inline static function _fac_aux(n, acc:Int):Int {
  return n < 1 ? acc : _fac_aux(n - 1, acc * n);
}

static function factorial(n:Int):Int {
  return _fac_aux(n,1);
}

static function factorial(n:Int):Int {
  return [for (i in 1...(n+1)) i].fold(function(num, total) return total *= num, 1);
}

using StringTools;
using Lambda;

class Factorial {
  // iterative
  static function factorial1(n:Int):Int {
    var result = 1;
    while (1<n)
      result *= n--;
    return result;
  }

  // recursive
  static function factorial2(n:Int):Int {
    return n == 0 ? 1 : n * factorial2(n - 1);
  }

  // tail-recursive
  inline static function _fac_aux(n, acc:Int):Int {
    return n < 1 ? acc : _fac_aux(n - 1, acc * n);
  }

  static function factorial3(n:Int):Int {
    return _fac_aux(n,1);
  }

  // functional 
  static function factorial4(n:Int):Int {
    return [for (i in 1...(n+1)) i].fold(function(num, total) return total *= num, 1);
  }

  static function main() {
    var v = 12;
    // iterative
    var start = haxe.Timer.stamp();
    var result = factorial1(v);
    var duration = haxe.Timer.stamp() - start;
    Sys.println('iterative'.rpad(' ', 20) + 'result: $result time: $duration ms');

    // recursive
    start = haxe.Timer.stamp();
    result = factorial2(v);
    duration = haxe.Timer.stamp() - start;
    Sys.println('recursive'.rpad(' ', 20) + 'result: $result time: $duration ms');

    // tail-recursive
    start = haxe.Timer.stamp();
    result = factorial3(v);
    duration = haxe.Timer.stamp() - start;
    Sys.println('tail-recursive'.rpad(' ', 20) + 'result: $result time: $duration ms');

    // functional
    start = haxe.Timer.stamp();
    result = factorial4(v);
    duration = haxe.Timer.stamp() - start;
    Sys.println('functional'.rpad(' ', 20) + 'result: $result time: $duration ms');
  }
}

  

You may also check:How to resolve the algorithm Resistor mesh step by step in the D programming language
You may also check:How to resolve the algorithm Duffinian numbers step by step in the APL programming language
You may also check:How to resolve the algorithm Soundex step by step in the J programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the bash programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the МК-61/52 programming language