packages feed

egison-3.7.8: sample/math/number/sum-of-squares.egi

;;;;;
;;;;;
;;;;; Sum of Squares
;;;;;
;;;;;

; Infintite list of sum of squres.
; -- [m n (+ m^2 n^2)]
(define $sum-of-squares
  (let {[$square (lambda [$x] (* x x))]}
    (match-all nats (list integer)
      [<join _ (& <cons $m _> <join _ <cons $n _>>)> [m n (+ (square m) (square n))]])))

; sample output
(test (take 30 sum-of-squares))

; list numbers that is the sum of two non-zero square numbers in two distinct way
(test (let {[$n 2]}
        (take 5 (match-all sum-of-squares (list [integer integer integer])
                  [<join _ <cons [$x_1 $y_1 $c]
                    (loop $i [2 n]
                      <join _ <cons [$x_i $y_i ,c] ...>>
                      _)>>
                   (map (lambda [$i] [x_i y_i c]) (between 1 n))]))))

; prime-factorize sum of squares
; -- [m n {p1 p2 ...}]
(define $sum-of-squares-pf (map (match-lambda [integer integer integer] {[[$m $n $c] [m n (p-f c)]]}) sum-of-squares))

; sample output
(test (take 30 sum-of-squares-pf))

; list prime numbers that is the sum of two non-zero square numbers
(test (take 30 (match-all sum-of-squares-pf (list [integer integer (multiset integer)])
                 [<join _ <cons [$m $n <cons $p <nil>>] _>> [m n p]])))