Print valid arithmetic expressions

4

Recursively define a "valid arithmetic expression":

  1. Any natural number is a valid arithmetic expression.
  2. If s is a valid arithmetic expression, then so is (-s).
  3. If p and q are valid arithmetic expressions, then so is (p+q).

In the above, a "natural number" is recursively defined as follows:

  1. 0 1 2 3 4 5 6 7 8 9 are natural numbers.
  2. If a natural number n is not 0, then n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 are natural numbers.

Alternatively, via the regex /^(0|[1-9][0-9]*)$/

Examples of valid arithmetic expressions:

0
314
(-7)
(0+0)
(314+(-314))

Examples of invalid arithmetic expressions:

01
-5
5+10

The challenge is to print every valid arithmetic expression line by line, i.e. create a program that will output strings line by line with the constraint that every valid arithmetic expression is eventually printed, and that no invalid arithmetic expression is ever printed.

The valid arithmetic expressions can be printed more than once.

The challenge is to do so in as few bytes as possible, since this is .

Standard loopholes apply.

Leaky Nun

Posted 2018-04-16T11:30:54.947

Reputation: 45 011

Can we expect the input to be non-empty? – LiefdeWen – 2018-04-16T12:25:01.277

2The output should be infinite, presumably? How can we demonstrate that every valid expression will be printed? – Phil H – 2018-04-16T12:46:50.353

1@PhilH well you can challenge an answer that doesn't meet the criterion and the author of the answer would have to justify it... – Leaky Nun – 2018-04-16T12:49:48.560

@LiefdeWen you mean empty? – Leaky Nun – 2018-04-16T12:49:56.963

Suppose someone writes an entry which just enumerates the natural numbers, it would only finish that after infinity... – Phil H – 2018-04-16T13:27:02.133

1@PhilH then the entry is invalid. – Leaky Nun – 2018-04-16T13:27:28.207

1@PhilH That doesn't satisfy "every valid arithmetic expression is eventually printed", as (-0) is never printed. – user202729 – 2018-04-16T13:40:03.540

@user202729 I could claim it would do so eventually, but also isn't this supposed to be code rather than theory of code? What's the real difference between a program that prints all the positive natural numbers then starts on the negative numbers, and one that just prints the positive numbers? The outputs are the same for all time... – Phil H – 2018-04-16T15:08:31.787

1@PhilH Seems that you misunderstood the rules? The validity criteria is "for all expression E, there exists a (finite) natural number N, such that the N'th line printed is E" Your example does not satisfy that. – user202729 – 2018-04-16T15:19:54.210

Answers

1

Jelly, ... 33 bytes

⁾()j
p`j”+Ç$€;;”-;ÇƊ€;®‘©¤Ṅ€
WÇ1¿

Try it online!

Wow, writing a correct submission is hard.


Python pseudocode equivalent:

import itertools

# Jelly: ⁾()j
def wrap_in_parentheses(x):
 return str(x).join(['(',')'])
 # or equivalently, `return '('+str(x)+')'

# Jelly: p`j”+Ç$€;;”-;ÇƊ€;®‘©¤Ṅ€
register = 0
def expand_and_print(expressions):
 # Jelly: p`
 result = list(itertools.product(expressions, repeat=2))

 # Jelly: j”+Ç$€
 result = [wrap_in_parentheses('+'.join(expr)) for expr in result]

 # Jelly: ;
 result.extend(expressions)

 # Jelly: ;”-;ÇƊ€
 result.extend([wrap_in_parentheses('-'+expr) for expr in expression])

 # Jelly: ;®‘©¤
 global register
 register += 1
 result.append(register)

 # Jelly: Ṅ€
 for expr in result:
  print(expr)

 return result

# Jelly: WÇ1¿
expressions = ['0']
while 1:
 expressions = expand_and_print(expressions)

user202729

Posted 2018-04-16T11:30:54.947

Reputation: 14 620

1

JavaScript ES6, 79 bytes

for(x=[n=0];;++n)x.map(i=>x.map(j=>x.push(n,`(-${i})`,`(${i}+${j})`))+alert(i))

l4m2

Posted 2018-04-16T11:30:54.947

Reputation: 5 985

>

  • Infinite large integer in variable, assumed; 2. Totally unrunnable in fact
  • < – l4m2 – 2018-04-16T14:10:59.480

    1

    Python, 241 bytes

    g=lambda n,s=0:s<6 and(l+r for x in(["\x13 8"]+list("I-x</"))[s]for a in range(n)for l in g(a,ord(x)%12)for r in g(n-a,ord(x)//12))or[]if n^1else s>7and["(-)+"[s-8]]or list("0123456789"[s==7:])*(s%6<2)
    i=1
    while i:print("\n".join(g(i)));i+=1
    

    This isn't too short, but it's a table-driven approach that's semi-automatically generated from a CFG I wrote for this language. It's unreasonably fast (speed sacrificed for bytes) :)

    orlp

    Posted 2018-04-16T11:30:54.947

    Reputation: 37 067