newLISP Fan Club

Forum => newLISP in the real world => Topic started by: cameyo on November 25, 2018, 01:45:59 AM

Title: factor-group function
Post by: cameyo on November 25, 2018, 01:45:59 AM
To solve a project euler problem i have written this factor-group function:
(define (factor-group x)
  (letn (fattori (factor x)
         unici (unique fattori))
    (transpose (list unici (count unici fattori)))))

(factor-group 2000)
;-> ((2 4) (5 3))
(factor-group 232792560)
;-> ((2 4) (3 2) (5 1) (7 1) (11 1) (13 1) (17 1) (19 1))

Do you known a better/faster way?

Thanks.



cameyo
Title: Re: factor-group function
Post by: rrq on November 28, 2018, 06:06:37 PM
Nice.



The factorization is of course the "slow" part, whereas the result juggling is almost irrelevant time-wise. I wouldn't try to improve on your implementation, but as a hind-sight, I probably would have used
(map list unici (count unici fattori)) instead of
(transpose (list unici (count unici fattori))) just because it intuitively would be less structure re-arrangement.

And I also wouldn't have known to use Italian named variables :)
Title: Re: factor-group function
Post by: cameyo on November 28, 2018, 10:41:51 PM
Thanks Ralph.

I am really enjoying newLisp :-)

cameyo
Title: Re: factor-group function
Post by: cameyo on January 31, 2019, 08:47:03 AM
I forgot to add the inverse function:


(setq fg (factor-group 220))
;-> ((2 2) (5 1) (11 1))


(setq num-fg (apply * (map (lambda (x) (pow (first x) (last x))) fg)))
;-> 220


cameyo