Skip to content

Instantly share code, notes, and snippets.

@righ1113
Created September 28, 2019 06:48
Show Gist options
  • Save righ1113/83d71542a53d054bb2a3676f469626a1 to your computer and use it in GitHub Desktop.
Save righ1113/83d71542a53d054bb2a3676f469626a1 to your computer and use it in GitHub Desktop.
各桁の和が3の倍数な整数は3の倍数 in Egison
;; > egison -N
;; > loadFile("multiOf3-N.egi")
def sumMulti?(x, n2, y) =
match (sum(map(($i -> x_i), 1..n2)) + y) as mod(3)
| 0 -> #t
| _ -> #f
def afterCheck?(ans) =
all(($k -> k % 3 == 0), map(foldl(2#(%2 + 10 * %1), 0, $), ans))
def calcMultiOf3'(n) =
matchAll cons(1..9, take((n-1), repeat1(0..9))) as list(multiset(integer))
| _ <++>
loop($i, (1, [n-1], _), <cons ($x_i <:> _) ...>,
<cons ((?sumMulti?(x, (n-1), $) and $y) <:> _) <nil>>) ->
[@map(($i -> x_i), 1..(n-1)), y]
def calcMultiOf3(n) =
let ans = calcMultiOf3'(n)
in (ans, afterCheck?(ans))
def resultMultiOf3(n) = snd(calcMultiOf3(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment