# 数论解释器，模n

12

• `0``'`（后继） -后继手段`+1`，所以`0'''' = 0 + 1 + 1 + 1 + 1 = 4`
• `+`（加法）和`*`（乘法）
• `=` （等于）
• `(``)`（括号）
• 逻辑运算符`nand``a nand b``not (a and b)`
• `forall` （通用量词）
• `v0``v1``v2`等。（变量）

这是一个句子的示例：

``````forall v1 (forall v2 (forall v3 (not (v1*v1*v1 + v2*v2*v2 = v3*v3*v3))))
``````

`not x`是简写`x nand x`-实际的句子会用到`(v1*v1*v1 + v2*v2*v2 = v3*v3*v3) nand (v1*v1*v1 + v2*v2*v2 = v3*v3*v3)`，因为`x nand x = not (x and x) = not x`

``````forall v0 (v0 * v0 * v0 = v0)
``````

（对于所有数字x，x 3 = x 的陈述）

``````0 * 0 * 0 ≡ 0 (mod 3)
1 * 1 * 1 ≡ 1 (mod 3)
2 * 2 * 2 ≡ 8 ≡ 2 (mod 3)
``````

### 输入输出格式

``````("forall v0 (v0 * v0 * v0 = v0)", 3)
"3:forall v0 (((v0 * v0) * v0) = v0)"
"(forall v0)(((v0 * v0) * v0) = v0) mod 3"
[3, "forall", "v0", "(", "(", "(", "v0", "*", "v0", ")", "*", "v0", ")", "=", "v0", ")"]
(3, [8, 9, 5, 5, 5, 9, 3, 9, 6, 3, 9, 6, 4, 9, 6]) (the sentence above, but with each symbol replaced with a unique number)
"f v0 = * * v0 v0 v0 v0"
[3, ["forall", "v0", ["=", ["*", "v0", ["*", "v0", "v0"]], "v0"]]]
"3.v0((v0 * (v0 * v0)) = v0)"
``````

### 测试用例

``````forall v0 (v0 * v0 * v0 = v0) mod 3
true

forall v0 (v0 * v0 * v0 = v0) mod 4
false (2 * 2 * 2 = 8 ≡ 0 mod 4)

forall v0 (v0 = 0) mod 1
true (all numbers are 0 modulo 1)

0 = 0 mod 8
true

0''' = 0 mod 3
true

0''' = 0 mod 4
false

forall v0 (v0' = v0') mod 1428374
true

forall v0 (v0 = 0) nand forall v1 (v1 = 0) mod 2
true (this is False nand False, which is true)

forall v0 ((v0 = 0 nand v0 = 0) nand ((forall v1 (v0 * v1 = 0' nand v0 * v1 = 0') nand forall v2 (v0 * v2 = 0' nand v0 * v2 = 0')) nand (forall v3 (v0 * v3 = 0' nand v0 * v3 = 0') nand forall v4 (v0 * v4 = 0' nand v0 * v4 = 0')))) mod 7
true
(equivalent to "forall v0 (v0 =/= 0 implies exists v1 (v0 * v1 = 0)), which states that every number has a multiplicative inverse modulo n, which is only true if n is 1 or prime)

forall v0 ((v0 = 0 nand v0 = 0) nand ((forall v1 (v0 * v1 = 0' nand v0 * v1 = 0') nand forall v2 (v0 * v2 = 0' nand v0 * v2 = 0')) nand (forall v3 (v0 * v3 = 0' nand v0 * v3 = 0') nand forall v4 (v0 * v4 = 0' nand v0 * v4 = 0')))) mod 4
false

``````

1

1
@JoKing如果您希望他们成为他们，您可以使用- `var number`甚至可以使用`1 + number``1`应该是`v0``2`将会是`v1`，等等）
Leo Tenenbaum

1
@JoKing（理论上）您应该允许无限数量的变量。如果最大数量的变量受整数的最大大小限制，那是可以的，但是您不应有这么低的限制。如果您遇到问题，则可以选择其他输入格式之一。
Leo Tenenbaum

1
@UnrelatedString当然可以，只要它们可以任意长即可。
Leo Tenenbaum

1

Xcoder先生19年

3

# Python 2中，252个 236字节

``````def g(n,s):
if str(s)==s:return s.replace("'","+1")
o,l,r=map(g,[n]*3,s);return['all((%s)for %s in range(%d))'%(r,l,n),'not((%s)*(%s))'%(l,r),'(%s)%%%d==(%s)%%%d'%(l,n,r,n),'(%s)%s(%s)'%(l,o,r)]['fn=+'.find(o)]
print eval(g(*input()))``````

``[3, ["f", "v0", ["=", ["*", "v0", ["*", "v0", "v0"]], "v0"]]]``

Leo Tenenbaum

@LeoTenenbaum是的，我在第一个版本中使用了它，但是忘了在打完高尔夫球后重新添加它
TFeld 19'Aug

1

# APL（Dyalog Unicode），129字节SBCS

``{x y z←3↑⍵⋄7≤x:y×7<x⋄5≡x:∧/∇¨y{⍵≡⍺⍺:⍵⍺⋄x y z←3↑⍵⋄7≤x:⍵⋄6≡x:x(⍺∇y)⋄x(⍺∇⍣(5≢x)⊢y)(⍺∇z)}∘z¨⍳⍺⍺⋄y←∇y⋄6≡x:1+y⋄y(⍎x⊃'+×⍲',⊂'0=⍺⍺|-')∇z}``

``plus times nand eq forall succ zero ← 1 2 3 4 5 6 7``

### 取消评论

``````⍝ node types; anything ≥8 will be considered a var
plus times eq nand forall succ zero var←⍳8
⍝ AST nodes have 1~3 length, 1st being the node type
⍝ zero → zero, succ → succ arg, var → var | var value (respectively)

⍝ to (from replace) AST → transform AST so that 'from' var has the value 'to' attached
replace←{
⍵≡⍺⍺:⍵⍺             ⍝ variable found, attach the value
x y z←3↑⍵
zero≤x: ⍵           ⍝ zero or different variable: keep as is
succ≡x: x(⍺∇y)      ⍝ succ: propagate to y
forall≡x: x y(⍺∇z)  ⍝ forall: propagate to z
x(⍺∇y)(⍺∇z)         ⍝ plus, times, eq, nand: propagate to both args
}
⍝ (mod eval) AST → evaluate AST with the given modulo
eval←{
x y z←3↑⍵
zero≡x:   0
var≤x:    y                    ⍝ return attached value
forall≡x: ∧/∇¨y replace∘z¨⍳⍺⍺  ⍝ check all replacements for given var
succ≡x:   1+∇y
plus≡x:   (∇y)+∇z
times≡x:  (∇y)×∇z
eq≡x:     0=⍺⍺|(∇y)-∇z         ⍝ modulo equality
nand≡x:   (∇y)⍲∇z              ⍝ nand symbol does nand operation
}``````

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.