factor-group function

Q&A's, tips, howto's

factor-group function

Postby cameyo » Sun Nov 25, 2018 9:45 am

To solve a project euler problem i have written this factor-group function:
Code: Select all
(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
cameyo
 
Posts: 15
Joined: Sun Mar 27, 2011 3:07 pm

Re: factor-group function

Postby ralph.ronnquist » Thu Nov 29, 2018 2:06 am

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
Code: Select all
(map list unici (count unici fattori))
instead of
Code: Select all
(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 :)
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: factor-group function

Postby cameyo » Thu Nov 29, 2018 6:41 am

Thanks Ralph.
I am really enjoying newLisp :-)
cameyo
cameyo
 
Posts: 15
Joined: Sun Mar 27, 2011 3:07 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests