Looking for variant approach/sketch

@GolamMostafa

Swapping the I and R version in your test, showed a problem

1011
479001600
1046
479001600
921
479001600
1136
479001600

Modified your sketch to include a delay() to flush the Serial.

void setup()
{
  Serial.begin(115200);
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  delay(100);
  TCCR1B = 0x01;  //TC1 ON with prescaler-1
  uint32_t facValue = factorialR(12);
  TCCR1B =  0;     //TC1 is OFF
  Serial.println(TCNT1);  //read counts
  Serial.println(facValue);
  delay(100);

  //----iterative--------------------------
  TCNT1 = 0;
  TCCR1B = 0x01;  //TC1 ON with prescaler-1
  facValue = factorialI(12);
  TCCR1B =  0;     //TC1 is OFF
  Serial.println(TCNT1);  //read counts
  Serial.println(facValue);
}

void loop()
{

}

//--------------------------
uint32_t factorialR(uint8_t n)
{
  if (n == 1)
  {
    return 1;
  }
  return (n * factorialR(n - 1));
}

//--------------------------
uint32_t factorialI(uint8_t n)
{
  uint32_t fac = n;
  while ((--n) > 1) fac *= n;
  return fac;
}

Output

1011
479001600
921
479001600

Swapping the I and R version now only re-orders the output.

Finally the numbers are now in line with numbers from @LarryD post #22

Thank you @build_1971 for the challenge :slight_smile:

1 Like