toybox/toys/other/factor.c
<<
>>
Prefs
   1/* factor.c - Factor integers
   2 *
   3 * Copyright 2014 Rob Landley <rob@landley.net>
   4 *
   5 * See https://man7.org/linux/man-pages/man1/factor.1.html
   6 *
   7 * -h and -x options come from https://man.netbsd.org/factor.6
   8
   9USE_FACTOR(NEWTOY(factor, "?hx", TOYFLAG_USR|TOYFLAG_BIN))
  10
  11config FACTOR
  12  bool "factor"
  13  default y
  14  help
  15    usage: factor NUMBER...
  16
  17    Factor integers.
  18
  19    -h  Human readable: show repeated factors as x^n
  20    -x  Hexadecimal output
  21*/
  22
  23#define FOR_factor
  24#include "toys.h"
  25
  26static void factor(char *s)
  27{
  28  unsigned long long l, ll, lll;
  29  char *pat1 = FLAG(x) ? " %llx" : " %llu", *pat2 = FLAG(x) ? "^%x" : "^%u";
  30
  31  for (;;) {
  32    char *err = s;
  33    int dash = 0, ii;
  34
  35    while(isspace(*s)) s++;
  36    if (*s=='-') dash = *s++;
  37    if (!*s) return;
  38
  39    errno = 0;
  40    l = strtoull(s, &s, 0);
  41    if (errno || (*s && !isspace(*s))) {
  42      error_msg("%s: not integer", err);
  43      while (*s && !isspace(*s)) s++;
  44      continue;
  45    }
  46
  47    if (dash) xputc('-');
  48    printf(pat1+1, l);
  49    xputc(':');
  50
  51    // Negative numbers have -1 as a factor
  52    if (dash) printf(" -1");
  53
  54    // test 2 and odd numbers until square is > remainder or integer wrap.
  55    for (ll = 2;; ll += 1+(ll!=2)) {
  56      lll = ll*ll;
  57      if (lll>l || lll<ll) {
  58        if (l>1 || ll==2) printf(pat1, l);
  59        break;
  60      }
  61      for (ii = 0; !(l%ll); ii++) {
  62        if (!ii || !FLAG(h)) printf(pat1, ll);
  63        l /= ll;
  64      }
  65      if (ii>1 && FLAG(h)) printf(pat2, ii);
  66    }
  67    xputc('\n');
  68  }
  69}
  70
  71void factor_main(void)
  72{
  73  char *s = 0, **ss;
  74  size_t len = 0;
  75
  76  if (toys.optc) for (ss = toys.optargs; *ss; ss++) factor(*ss);
  77  else for (;;) {
  78    if (-1 == getline(&s, &len, stdin)) break;
  79    factor(s);
  80  }
  81}
  82