coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 1/2] factor: add option for printing in x^y format


From: Kaz Kylheku
Subject: Re: [PATCH 1/2] factor: add option for printing in x^y format
Date: Thu, 28 Apr 2022 01:41:50 -0700
User-agent: Roundcube Webmail/1.4.13

On 2022-04-27 10:21, Bernhard Voelker wrote:
> Regarding the postprocess solution:
> this is a little awk(1) snippet to get the same output as with the patch:
> 
>   $ factor 13 36 | tr ' ' '\n' | uniq -c | awk '\
>       BEGIN { n="" }
>       /:$/  { printf("%s%s", n, $2); n="\n"; next };
>             { printf(" %s%s", $2, ($1>1?"^"$1:"")); }
>       END   {printf("%s",n); }
>       '
>   13: 13
>   36: 2^2 3^2

All in awk:

factor 13 36 65536 | awk '
{
  printf "%s", $1
  for (i = 2; i <= NF + 1; i++) {
    if ($i"" == fac) {
      count++
    } else {
      if (fac) {
        printf " %s", fac
        if (count > 1)
          printf "^%s", count
      }
      fac = $i
      count = 1
    }
  }
  print ""
}'

13: 13
36: 2^2 3^2
65536: 2^16

There is always:

factor 13 36 65536 | txr -e '
(while-match `@num: @facs` (get-line)
  (put-line `@num: @(flow facs
                      (spl " ")
                      (partition-by identity)
                      (mapcar [juxt first len])
                      (mapcar (ado if (> @2 1) `@1^@2` `@1`)))`))'
13: 13
36: 2^2 3^2
65536: 2^16

I needed a "core util" like this, so I made one. :)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]