We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Pentagonal Numbers
Pentagonal Numbers
Sort by
recency
|
22 Discussions
|
Please Login in order to post a comment
I have reached the next solution in common lisp, but the last test keeps failing with wrong result. I cannot undestand why, I have tested locally and the result seems to be the expected, and the performance is also good. Can anyone explain me what is the problem?
(defun sumpent (n) (if (< n 2) 1 (+ (memopent (1- n)) (- (* 3 n) 2))))
(let ((npast (make-hash-table))) (defun memopent (n) (let ((currval (gethash n npast))) (if currval currval (setf (gethash n npast) (sumpent n))))))
(let ((n (read))) (loop for line from 0 below n do (format t "~a~%" (memopent (read)))))
So for haskell you have to kind of get weird with it. The list random access operator is too slow to solve this the way I think you normally would. What I do it generate the list of pentagonal numbers and then access them in the order the test cases require. I noticed that they are either ascending or descending but also in order up to the amount provided. That's how I got around using the !! operator. Let me know if there's an actual performant dp solution.
Epic haskell slution
compact clojure:
The honest-but-not-DP way:
The cheating way: