Pythagorean triples: Difference between revisions

m
→‎{{header|Common Lisp}}: reformat code (logic unchanged)
(Added Crystal translation of Ruby version.)
m (→‎{{header|Common Lisp}}: reformat code (logic unchanged))
Line 766:
for z in b sum (* y z))))
 
(defun count-tri (lim &aux (prim 0) (cnt 0))
(letlabels ((primcount1 0)(tr &aux (cntperi 0(reduce #'+ tr)))
(labels ((count1when (tr<= peri lim)
(let ((peri (reduce #'+ tr)) (incf prim)
(when (<=incf pericnt (truncate lim peri))
(count1 (mmul '(( 1 -2 2) ( 2 -1 2) ( 2 -2 3)) tr))
(incf prim)
(incfcount1 cnt(mmul '(truncate( 1 2 2) ( 2 1 2) ( 2 2 lim3)) peritr))
(count1 (mmul '(( -1 - 2 2) ( -2 - 1 2) ( -2 - 2 3)) tr)))))
(count1 (mmul '((3 1 2 2) ( 2 1 2) ( 2 2 3))4 tr5))
(count1format (mmult '((-1"~a: ~a 2prim, 2) (-2 1 2) (-2~a all~%" 2lim 3))prim tr)))cnt)))
(count1 '(3 4 5))
(format t "~a: ~a prim, ~a all~%" lim prim cnt))))
 
(loop for p from 2 do (count-tri (expt 10 p)))</lang>output<lang>100: 7 prim, 17 all
Anonymous user