coreutils
[Top][All Lists]
Advanced

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

Re: factor -- print factorization in exponential form


From: Eduardo A . Bustamante López
Subject: Re: factor -- print factorization in exponential form
Date: Fri, 10 Nov 2017 20:14:53 -0600
User-agent: NeoMutt/20170609 (1.8.3)

On Sat, Nov 11, 2017 at 12:49:36AM +0100, Emanuel Landeholm wrote:
> List,
> 
> I'm working with large composite numbers and would like to have the option
> of printing the factorization in exponential form. I have attached a patch
> for printing factorizations in this form:
> 
>     $ src/factor
>     68000
>     68000: 2^5 5^3 17

[Note: I'm not a coreutils developer, I just lurk here :-) ]

Hey!

I couldn't help but notice that this could be done with a simple AWK script and 
a pipe:


dualbus@ubuntu:~$ cat exp.awk 
#!/usr/bin/awk -f
{
    number = $1
    Factors[u = 1] = $2; Count[$2]++
    for (i = 3; i <= NF; i++) {
        if (Factors[u] != $i) {
            Factors[++u] = $i
        }
        Count[$i]++
    }

    printf ("%s", number)
    for (i = 1; i <= u; i++) {
        if (Count[Factors[i]] == 1) {
            printf (" %d", Factors[i])
        } else {
            printf (" %d^%d", Factors[i], Count[Factors[i]])
        }
    }
    printf ("\n")
}

dualbus@ubuntu:~$ for number in 25 65536 68000 111111; do factor $number | 
./exp.awk; done
25: 5^2
65536: 2^16
68000: 2^5 5^3 17
111111: 3 7 11 13 37



reply via email to

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