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: Pádraig Brady
Subject: Re: [PATCH 1/2] factor: add option for printing in x^y format
Date: Wed, 27 Apr 2022 19:42:36 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Thunderbird/97.0

On 27/04/2022 19:36, Pádraig Brady wrote:
On 27/04/2022 18:21, Bernhard Voelker wrote:
On 4/27/22 16:14, Pádraig Brady wrote:
On 27/04/2022 11:07, Rasmus Villemoes wrote:
When factoring numbers that have a large 2^n factor, it can be hard to
eyeball just how many 2's there are. Add an option to print each prime
power factor in the x^y format (omitting the exponent when it is 1).

* src/factor.c: Add --exponents option for printing in x^y format.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

Thanks for the suggestion.
This is one of those borderline cases.

Mainly because it's fairly easy to postprocess the output.
Consider for example:

     $ factor 13 36 | tr ' ' '\n' | uniq -c
           1 13:
           1 13
           1 36:
           2 2
           2 3

Though for interactive use, the new option is a benefit.

Given the interactive benefit and a previous request to add this functionality¹
I'm tentatively leaning towards adding this.

cheers,
Pádraig

¹ https://lists.gnu.org/archive/html/coreutils/2017-11/msg00015.html

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

nice!
Re. the patch:
the implementation is quite clean in 2 places - the LONGINT_OK and the MPZ case 
- and
doesn't seem to get into the way of any future extensions (null-separated 
output, whatever).

Therefore, I'm also 70:30 for adding it.
Of course, we'd also need a long option and texinfo documentation.

Finally, regarding '-e': is there any precedence (or a clashing option) in any 
other
factor(1) implementation?

We generally avoid new short options where possible.
This would just need the long option to enable this
given it's kind of esoteric and not often used interactively.

I see NetBSD has used '-h' for this "human readable" functionality.
I suppose for more compat with that we could have:

  -h, --exponents

We might leave the -h undocumented,
for the reasons stated above.

cheers,
Pádraig



reply via email to

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