Karim Belabas on Mon, 10 Aug 2015 10:16:51 +0200

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

Re: function for prime factors of an integer

* Bill Allombert [2015-08-06 15:14]:
> On Thu, Aug 06, 2015 at 12:57:31PM +0100, John Cremona wrote:
>> I know various ways of getting a list of the prime factors of an integr N
>> in two steps, such as
>> factor(N)[,1]~
>> whose syntax is a little obscure, but is there a cse for having a built-in
>> gp function for this?  If there is one I cannot find it...
> I do not think there is one, either.

Indeed, there isn't. Note that the above one-liner doesn't give the expected
answer for negative numbers or 0 : both 0 and -1 would appear in the
"primes" list. And it fails on 1 (empty matrix, doesn't have a first column).

I don't thing there's a strong need for a built-in trivial implementation.
The cost for writting it (1 or 2 minutes ?) is certainly lower than
hypothetically digging it up from the manual [ which would increase if
we started to include many such simple convenience functions, thereby
increasing the manual size :-) ]

I have tens of such functions hidden in my private GP libraries,
automatically loaded on startup ("read" statements in GPRC). When I 
teach a GP-using course, I usually distribute simple convenience
functions analogous to these to students. It gives them good examples
to start with the syntax as well...

It might be a good idea to compile a list of convenience functions
people use around here and distribute with the examples/ scripts.
(Which are currently much too complicated for beginners; and don't really
expose efficient GP programming techniques either.)



P.S My own pet peeve is for a matrix.number_of_rows() method...
Both obvious "elegant" solutions #x~ and #x[,1] are "wrong" : first one copies x
and is currently expensive - it could conceivably be optimized away by
the byte compiler -; second one is less expensive but fails on empty matrices.

matsize(x)[1] is bullet-proof but ugly. I usually recommend

  [m,n] = matsize(x);

which looks better in programs.

Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]