Ad

div_t is an structure and cause more memory usage you can use % and / operators instead.also you can use while(1) with break for this code.

Code
Diff
  • #include <stdlib.h>
    #include <math.h>
    
    int reverse_int(int int_reverse) {
      int o = 0;
      while (1) {
        o += ((int)int_reverse % 10);
        if (int_reverse /= 10) 
          o *= 10; 
        else break;
      }
      return o;
    }
    • #include <stdlib.h>
    • #include <math.h>
    • int reverse_int(int int_reverse) {
    • div_t div_x = div(int_reverse, 10);
    • int reverse_int = div_x.rem;
    • while (div_x.quot) {
    • div_x = div(div_x.quot, 10);
    • reverse_int *= 10;
    • reverse_int += div_x.rem;
    • }
    • return reverse_int;
    • int o = 0;
    • while (1) {
    • o += ((int)int_reverse % 10);
    • if (int_reverse /= 10)
    • o *= 10;
    • else break;
    • }
    • return o;
    • }

this is faster cause n===1 || n===2 has two steps and n<=1 has one

Code
Diff
  • function factorial (n) {
      if (n <= 1) {
        return 1;
      } else {
        return n * factorial(n - 1);
      }
    }
    • function factorial (n) {
    • if (n === 0 || n === 1) {
    • if (n <= 1) {
    • return 1;
    • } else {
    • return n * factorial(n - 1);
    • }
    • }