105
\$\begingroup\$

Summary:

For any given language, what is the smallest amount of unique characters for your language to be Turing-Complete?

Challenge:

For any language of your choice, find the smallest subset of characters that allows your language to be Turing-Complete. You may reuse your set of characters as many times as you want.


Examples:

  • JavaScript: +!()[] (http://www.jsfuck.com)

  • Brainfuck: +<>[] (assumes a wrapping cell size)

  • Python 2: ()+1cehrx (made from scripts like exec(chr(1+1+1)+chr(1)))

Scoring:

This challenge is scored in characters, not bytes. For example, The scores for the examples are 6, 5, and 9.


Notes:

  • This challenge differentiates from others in the sense that you only your language to be Turing-Complete (not necessarily being able to use every feature of the language.)

  • Although you can, please do not post answers without reducing the characters used. Example: Brainfuck with 8 characters (since every other character is a comment by default.)

  • You MUST provide at least a brief explanation as to why your subset is Turing-Complete.

\$\endgroup\$
  • 87
    \$\begingroup\$ Unary, 1 character. sighs \$\endgroup\$ – Dennis Feb 20 '17 at 15:24
  • 4
    \$\begingroup\$ @Dennis It's not that different from Jelly or 05AB1E having a built-in for an interesting number theory problem. This challenge still seems like an interesting and non-trivial optimisation problem in any language that wasn't designed to be a tarpit. \$\endgroup\$ – Martin Ender Feb 20 '17 at 15:35
  • 7
    \$\begingroup\$ @MartinEnder I'd be especially interested to see answers in languages like Java or C. \$\endgroup\$ – Julian Lachniet Feb 20 '17 at 15:41
  • 9
    \$\begingroup\$ Please don't post solutions in esolangs where the solution is every valid character in the language. It's not intresting or clever. \$\endgroup\$ – Pavel Feb 20 '17 at 17:20
  • 19
    \$\begingroup\$ @Pavel Not interesting or clever may mean that it shouldn't get upvoted, but certainly not that it shouldn't get posted. \$\endgroup\$ – Dennis Feb 20 '17 at 21:36

49 Answers 49

4
\$\begingroup\$

PowerShell, 15 14 characters

+[char](1)|iex

Thanks to @Erik-the-Outgolfer for seeing that we don't need the " marks.

I'm reasonably confident this is the smallest set we can have. Similar to the Python answers, this constructs up a program one character at a time (via things like [char](1+1+1+1+1...+1+1) to get the appropriate ASCII value) and then evaluating the string via |iex. For example, here is an example program that is equivalent to "Test: "+(3+4). As a result, we can construct literally any PowerShell program with this method, and this is therefore Turing-Complete.

\$\endgroup\$
  • \$\begingroup\$ I don't think you need the ", I tried removing them in your example program. \$\endgroup\$ – Erik the Outgolfer Feb 26 '17 at 9:35
  • \$\begingroup\$ @EriktheOutgolfer You're right -- thanks! Must be a difference in behavior for newer versions of PowerShell, since previous versions would try to mathematically add the chars together, rather than concatenate. \$\endgroup\$ – AdmBorkBork Feb 27 '17 at 13:53
3
\$\begingroup\$

APL, 9 characters

⍎⎕UCS(≢⍬)

Why this is Turing-complete:

  • is length, is the empty list, and a list can be expressed simply by naming its elements, i.e. ⍬⍬⍬ is a list of three empty lists. This way, all numbers can be formed. ≢⍬ is 0, ≢≢⍬ is 1, and from then on ≢⍬⍬⍬... is N, where N is the amount of s.
  • () are used to change evaluation order. List construction works with anything, so this way (≢⍬)(≢⍬⍬)(≢⍬⍬⍬) evaluates to [0,2,3].
  • ⎕UCS gives a string of Unicode characters given a list of numbers. We can now generate any text we want.
  • is evaluate.
\$\endgroup\$
  • \$\begingroup\$ ≢≢⍬ does not look right. Should it be ≢⍬⍬? \$\endgroup\$ – CalculatorFeline May 30 '17 at 19:23
  • \$\begingroup\$ @CalculatorFeline: no, ≢⍬⍬ is 2. ⍬⍬ is the list containing two empty lists, and its length () is 2. ≢≢⍬ is 1, because is the empty list, its length () is 0, and the length of that () is 1. ≢≢⍬ = ≢0 = 1.Try it yourself: tryapl.org/… \$\endgroup\$ – marinus May 31 '17 at 20:12
  • \$\begingroup\$ Save a character: ⍎⎕AV[≢],). One-based indexing obviates the need for any "zeroth" character. \$\endgroup\$ – Adám Jun 2 '17 at 14:21
  • \$\begingroup\$ Change any code to an expression consisting of those 8 chars: Try it online! \$\endgroup\$ – Adám Jun 2 '17 at 15:36
3
\$\begingroup\$

Java, 30 26 characters

 ()+-.0;=Sacdefgimnorstv{}

Taking a different approach from the other (more clever) Java answer, this one uses "regular" characters.

Java (like most languages) offers many facilities above and beyond what is required to be Turing-complete: basic arithmetic, jumps, and declaring variables (memory on the tape). The only types of jumps necessary are the simple if and for statements.

I started by writing a small program shell (main method), then adding statements that implement the bare minimum set that represents a Turing-complete subset of Java. I did so in a way that used the fewest characters possible, and came up with this:

interface S {

  static void main(String... s) {
    int r = 0;
    r++;
    int t = p;
    t--;
    if (t == 0) {
      r--;
    }
    for(;;) {
      t++;
      r++;
    }
  }
}

Removing all whitespace except for one space (0x20), sorting, and removing duplicates provides the string above.

These characters allow:

  • if conditionals.
  • Variable assignments.
  • Comparing variables against each other and zero.
  • for loops, including infinite loops (for(;;))
  • Adding and subtracting arbitrary numbers via repeated unary increment and decrement.

In other words, I have reduced Java to a slightly more readable version of Brainfuck.

\$\endgroup\$
  • 1
    \$\begingroup\$ You need some way to create an infinite loop, for Turing completeness. I suspect you can do it via recursion (or for(;;)), but you probably need to mention that in your submission; manually unrolling an infinite loop is of course impossible, so the current explanation doesn't work. \$\endgroup\$ – user62131 Feb 22 '17 at 5:16
  • \$\begingroup\$ You can use interface instead of class, which allows you to drop the public. \$\endgroup\$ – corvus_192 Feb 24 '17 at 16:11
  • \$\begingroup\$ Also, replace the [] with ... to save another character. \$\endgroup\$ – corvus_192 Feb 24 '17 at 16:12
  • \$\begingroup\$ @corvus_192 thanks, good catches. [] could be useful in a state machine, but is not strictly necessary. To use it, however, I would need to add w to support new. \$\endgroup\$ – user18932 Feb 24 '17 at 16:25
  • \$\begingroup\$ You can drop the minus or plus and use integer overflow. A decrement is a mere 4294967295 increments. \$\endgroup\$ – Robert Fraser Sep 21 '17 at 7:07
3
\$\begingroup\$

PHP 7, 6 characters

'().;^

The idea is that it's possible to execute arbitrary code using the following construction:

('create_function')('','<code>')();

eval wouldn't work here, because it's a language construct and cannot be called using variable functions.

create_function and the code could be written as a concatenation of bitwise XORs of available characters:

(<char1_1>^<char1_2>^...).(<char2_1>^<char2_2>^...)...

Using ().;^ for <charX_Y>, we can get

()./:;<=JKLMXY^_bcdepqvw

and some unprintable characters. It's not enough, but now we can call 'eXp'() and get some numeric characters too:

''.'eXp'('eXp'('')) -> 1
''.'eXp'('eXp'('eXp'(''))) -> 2.718281828459
''.'eXp'('eXp'('eXp'('eXp'('eXp'(''))))) -> 3814279.1047602

It gives us 1, 2 and 3 (other characters will be ignored by XOR, if the other string is one character long). From ().;^123 we can now generate all the ASCII charset.

Try it online

\$\endgroup\$
3
\$\begingroup\$

J language, 7 char

To acheive Turing completeness, J can make do with the following 6 characters, plus space.

".u:1b

1b is a prefix for numbers meaning they are expressed in unary, so that e.g. 1b1111 1b11 is the array 4 2. This can represent every positive integer.

Then, u: converts ASCII character codes to characters, and ". evaluates a string as J code. This allows full access to the language.

Is this minimal?

Probably. What I have is pretty darn lean.

No proper subset of these characters is sufficient, though there are a couple of equivalent sets like do u:1b and ".1b {a.

J has no good facilities for doing something overly clever like embedding some lambda calculus or tag system, either, so I don't think a different strategy has a better shot, but I won't rule out the chance that I'm overlooking something sneaky.

\$\endgroup\$
  • 5
    \$\begingroup\$ Why not just put the space in the list? \$\endgroup\$ – mbomb007 Feb 21 '17 at 21:45
2
\$\begingroup\$

Nock, 6 characters

[ ]012

Nock is a minimal virtual machine based on combinator reduction. It's memory model is a binary tree of bignums, and the spec gzips to 340 bytes. There's a trivial transformation from Nock operations to the SKI combinators, which I stole from the Urbit examples library (which seems to originate from this reddit discussion):

S = [[1 1 2] [1 0 1] [1 1] 0 1]
K = [[1 1] 0 1]
I = [0 1]

A more interesting way to do this would be to re-compile Nock with the Nock 4 operator, which is increment, to create the other operators. [4 1 1] is 2, [4 4 1 1] is 3, etc. S could alternatively be defined [[1 4 1 1] [1 0 1] [1 1] 0 1], for example. I think that you still need a non-synthesized 2 operator in order to apply functions and reduce the 4, though.

\$\endgroup\$
2
\$\begingroup\$

Scala, 12 chars

I dont't have a degree in computer science, so I'm not sure if this is valid. Feel free to correct me.

(),:;=>[]def

Using these characters, you can encode the SKI calculus. I replaced the semicolons with newlines for readability:

def>[d,e,f]:(d=>(e=>f))=>(d=>e)=>(d=>f)=(dd:d=>e=>f)=>(ee:d=>e)=>(ff:d)=>dd(ff)(ee(ff))
def>>[d,e]:d=>e=>d=(dd:d)=>(ee:e)=>dd
def>>>[d]:d=>d=(>[d,d=>d,d])(>>[d,d=>d])(>>[d,d])

(Ab-)using the fact that you can call a method >, which will be seperated from the def by the parser to save the space.

Borrowed from here and optimised for this challenge.

\$\endgroup\$
  • \$\begingroup\$ I don't think you need to have a computer science degree to know whether something is Turing-Complete... \$\endgroup\$ – Julian Lachniet Feb 21 '17 at 23:36
  • \$\begingroup\$ @DLosc Right, you'd have to add either a newline or a semicolon. \$\endgroup\$ – corvus_192 Feb 24 '17 at 16:02
2
\$\begingroup\$

BitCycle, 8 characters

AB>/+~

plus space and newline.

My first demonstration of BitCycle's Turing-completeness was a Bitwise Cyclic Tag interpreter. But it turns out I can avoid quite a few extra characters by instead constructing a reduction, this time from a cyclic tag system.

Consider any cyclic tag system, which consists of an ordered list of productions: strings of 0's and 1's (possibly including the empty string). Encode it as a string of 0's, 1's, and semicolons, with a semicolon following each production. For instance, the example from the Esolangs article, with productions (011, 10, 101), would be represented as 011;10;101;. Then translate each element to a block of BitCycle instructions as follows:

0

    >>      ~ 
     +~ ~     
  > +         
    > ~       
        > A~  
B /    ~   >> 






   +   ~    ~ 

1

    >>      ~ 
     +~ /     
  > +         
    > ~       
      >   A~  
B /    ~   >> 






   +   ~    ~ 

;

    . 




B / > 






    . 

(The . characters here are placeholders and don't affect the function of the program. They should be replaced with spaces in the actual reduction.)

Concatenate these blocks side-by-side according to the three-character representation of the cyclic tag system. Then wrap the concatenation in this looping construct:

> ... ~

~     ~

where ... represents the rest of the program, the > is on the same line as the B collectors, and the ~ ~ don't have anything but spaces in between them.

To test this, insert a ? before the > in the wrapper and give the input string as a command-line argument. For example, here's the cyclic tag system 1;0;:

       >>      ~           >>      ~         
        +~ /                +~ ~             
     > +                 > +                 
       > ~                 > ~               
         >   A~                > A~          
?> B /    ~   >> B / > B /    ~   >> B / > > ~

 ~                                           ~

       1           ;       0           ;     


      +   ~    ~          +   ~    ~         

I will add a detailed explanation if people are interested--just leave a comment. Right now it's past my bedtime. :)

\$\endgroup\$
2
\$\begingroup\$

ARM7 assembly - 8 bytes

CRS15,

And space and newline

With these characters, one can construct the following:

  • Registers R1, R5, and R15 (R15 is the instruction pointer)
  • The instruction RSC (Reverse Subtract with Carry)
  • The condition code CC (do if carry clear)
  • Any decimal number consisting of the numerals 1 and 5

These allow for data manipulation (subtract two registers), memory manipulation (specify destination as an address made up of 1s and 5s), and conditional jumping (R15 as the destination of a subtract with a condition code).

Comma, space, and newline are syntactic requirements of assemblers and cannot be avoided (in most cases).

One may be apt to point out that ARM does not have infinite pointers, and thus cannot be Turing complete. True, however no computer is Turing complete, and all of these languages are limited by their implementation. It is entirely possible to extend the ARM specification to allow for larger addresses. Ultimately, you'd have to let this one slide, and assume the best for the challenge.

Also, I admit to not knowing the minimum version of ARM this works in; I picked the one I know works

\$\endgroup\$
2
\$\begingroup\$

///, 2 characters

/\

It was proven Turing Complete when someone wrote a Bitwise Cyclic Tag interpreter using it.

Shortened to 3 characters thanks to @Leo and @ETHproductions.

Shortened to 2 characters thanks to @ØrjanJohansen

\$\endgroup\$
  • 6
    \$\begingroup\$ I'm fairly sure that /// is Turing-complete with just forward slash and backslash, but I'm not sure if that's actually been proven anywhere. This can likely be minimized, anyway. \$\endgroup\$ – user62131 Feb 24 '17 at 18:10
  • \$\begingroup\$ I think that at least characters ()|PD01 are used only for convenience in that code (it could be written without them, but it would be longer and it would be harder to encode the input to the tag). I don't know this language well enough, but i'm guessing that `/\` could very well be enough, since with just those two characters you can build an infinite set of words. \$\endgroup\$ – Leo Feb 24 '17 at 18:11
  • \$\begingroup\$ () are also only used for convenience. You could write the entire thing using only \/. \$\endgroup\$ – ETHproductions Feb 24 '17 at 18:13
  • \$\begingroup\$ Thanks. I am very new to this language, so I wouldn't know this. \$\endgroup\$ – Comrade SparklePony Feb 24 '17 at 19:39
  • 2
    \$\begingroup\$ Hi, author here. Even the . is just for convenience, everything other than slash and backslash is expanded before entering the main loop. \$\endgroup\$ – Ørjan Johansen Feb 27 '17 at 2:52
2
\$\begingroup\$

Skull, 9 characters

[]{}|:NUM

So Skull is an interesting language. You need NUM to set number mode. This adds to the amount of characters you need as you have to use one at the beginning of your programs. Also I mean that is the entire language except for 3 other characters.

{ x [ y ] } Increment or decrement the specified cell (x) by the specified number (y)
{ x { While the specified cell (x) is not 0...
} } End while
| x | Print out the specified cell (x) to the screen

This is a simple program doing addition (4+2)

:NUM:       // set mode to NUM
{0[+4]}      // set cell 0 to 4
{1[+2]}      // set cell 1 to 2
{0{         // while cell 0 is not 0
  {0[-1]}   // subtract cell 0 by 1
  {1[+1]}   // add 1 to cell 1
}}          // end while
|1|         // print cell 1 (6)
\$\endgroup\$
  • \$\begingroup\$ ASCII or ASKII? \$\endgroup\$ – NoOneIsHere Feb 20 '17 at 17:23
  • \$\begingroup\$ @NoOneIsHere Opps! Thanks for that. \$\endgroup\$ – Christopher Feb 20 '17 at 21:31
  • \$\begingroup\$ You don't need to print something to be Turing complete, so I think you can drop the ASC. \$\endgroup\$ – Laikoni Feb 20 '17 at 22:25
  • \$\begingroup\$ @Laikoni nice! That will cut this down! \$\endgroup\$ – Christopher Feb 20 '17 at 22:26
  • \$\begingroup\$ This also needs a Turing proof. \$\endgroup\$ – Brian Minton Feb 24 '17 at 13:48
1
\$\begingroup\$

Unlambda, 3 characters

sk`

It's a turing tarpit of course.

\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 9 charcaters

(space)$+=@GOT[]

$ - required for string variables
+ - for concatenating strings
= - assignment
@ - labels and label string literals (@ABC = "@ABC", when used in an expression)
GOT - used for GOTO, variable names, and label names
[] - accessing characters in strings
space - separator

Here is a Bitwise Cyclic Tag interpreter (some spaces replaced with line breaks for readability)

Program is encoded as G=0, O=10, T=11, and the data string uses T and O as 1 and 0.

G$=@<program here>
G$[O]=O$
T$=@<initial data here>
T$[O]=O$

GOTO @G
@TO @OO
G$=G$+G$[O]
G$[O]=O$
@G
GOTO O$+@G[O]+G$[O]+T$[O]
@GO @GT
T$[O]=O$
GOTO @OO
@TT @OT
T$=T$+G$[O]
GOTO @OO
\$\endgroup\$
  • \$\begingroup\$ Using these constructs, can you create unbounded data structures? They could be in the form of arrays, lists, strings, or even integers, as long as they're not limited in size by the implementation. If not, the language isn't Turing-complete. For example, in QBasic, trying to DIM an array larger than 64KB (that's 16384 SINGLE numbers) gives a Subscript out of range error. (This is different from running out of memory, which will happen with any language and is considered an implementation difficulty rather than a limitation of the language.) \$\endgroup\$ – DLosc Feb 24 '17 at 21:21
1
\$\begingroup\$

Perl 6, 9 characters

~^<>.EVAL

The goal here is to EVALuate arbitrary strings. To do this, we can use the ~^ to bitwise xor strings into other strings, as long as we have enough characters, as well as the <<>> to delimit the actual strings themselves. There's some fiddling in avoiding syntax errors when using <>, but we can generally use the characters .EVAL~^ to produce more characters.

For example, if you wanted to create the string 4*9, you could do:

<<...>>~^<<VEV>>~^<<LAA>>

And to evaluate that, you wrap it in more <<>>s and EVAL it a few times:

say <<<<...>>~^<<VEV>>~^<<LAA>>>>.EVAL.EVAL

Try it online!

Unfortunately, we can't get the full range of ASCII with just xors, so we can use ~& inside the evaluated strings, in the form 'string'~&'string'. This gets us a Turing complete subset of ASCII, but not all of it, so for convenience we can xor it once more to get a full subset.

For reference, a full program will go through 5 EVAL stages before executing:

<<<<........EE............................>>~^<<.E.....EVVE.....E.....................>>~^<<.V.....VLLVEEE..V....E.....E..EEEE..E.>>~^<<EL.....L~~LLVV.ELE.VEL.....L..LLLL.ELE>>~^<<L^.....^^^^~~L.V^L~^L~EE..E~~^~~~~^^~L>>>>
(<< ........EE............................ >>~^<< .E.....EVVE.....E..................... >>~^<< .V.....VLLVEEE..V....E.....E..EEEE..E. >>~^<< EL.....L~~LLVV.ELE.VEL.....L..LLLL.ELE >>~^<< L^.....^^^^~~L.V^L~^L~EE..E~~^~~~~^^~L >>)
'/.....//wm_.=/'~&'wEE..Ew~^wwww^5w'
'..'~^'weW5'
say 1

Here is a full program generator that can handle ASCII characters, and an example Hello World! program.

\$\endgroup\$
0
\$\begingroup\$

Tildehyph, 2 characters

~-

The language uses only two characters a tilde and a hyphen. The easy answer why Tildehyph is Turing-complete is the fact that there is a Brainfuck interpreter created in it and Brainfuck is proven to be Turing-complete.

\$\endgroup\$
  • 1
    \$\begingroup\$ I'd like to know who downvoted this. \$\endgroup\$ – Esolanging Fruit Feb 21 '17 at 22:09
  • 3
    \$\begingroup\$ @Challenger5 I don't see any point in an answer that removes no characters from the languages existing character set. Its just as boring as the Unary answer. \$\endgroup\$ – Sriotchilism O'Zaic Feb 22 '17 at 1:22
  • 1
    \$\begingroup\$ And yet the Unary answer gets 21 upvotes? \$\endgroup\$ – G B Feb 22 '17 at 8:47
  • 3
    \$\begingroup\$ @GB: The Unary answer shouldn't have been upvoted according to the normal advice. However, SE rules also say you shouldn't downvote something just because it's been incorrectly upvoted. \$\endgroup\$ – user62131 Feb 22 '17 at 9:36
  • \$\begingroup\$ @user62131 Then why downvote this answer? \$\endgroup\$ – MilkyWay90 Mar 3 at 3:54
0
\$\begingroup\$

HSPAL, 6 characters

012346

The BF interpreter linked in the esolangs article uses only the digits 0-4, plus 6, for all tokens except number literals and label IDs. It uses at most 116 distinct label IDs [the actual number is probably slightly lower, but I don't feel like counting them right now], which can be reassigned to use only the reduced 6-digit alphabet; and the BF instructions can likewise be reassigned to different code points; therefore all other digits can be excluded from the alphabet while leaving the language turing complete.

\$\endgroup\$
0
\$\begingroup\$

Decimal, 7 characters

012345D

These three commands are necessary to be Turing-complete:

  • 3 - I/O
  • 4 - MATH
  • 5 - COND

0, 1 and 2 are used as arguments to the commands. D is like the closing parenthesis for some commands.

How it's Turing-complete:

  • 310 reads a character to the stack (3=I/O, 1=from input, 2=to stack)
  • The first four arguments to command 4 MATH are +, -, *, / (1, 2, 3, 4). For example, calling 41D takes DSI and DSI-1, pops them, and pushes the result of adding them together.
  • Command 5 COND is a conditional. Jumps to the next COND if the DSI value is falsy.
\$\endgroup\$
  • \$\begingroup\$ I don't think you need 3. I/O isn't required for turing completeness \$\endgroup\$ – 12Me21 Feb 8 at 17:27
0
\$\begingroup\$

Turing Machine But Way Worse - 4 characters

0 1\n (The \n should be replaced with an actual newline)

States can be represented in binary and everything else uses a 0 or 1.

Spaces separate different parts of a command and newlines separate commands.

\$\endgroup\$
-2
\$\begingroup\$

Binary Lambda Calculus, 2 characters (with specified encoding).

http://www.ioccc.org/2012/tromp/hint.html

The programming "word" size is two bits long. All possible symbols are used. However the program is passed as a string in which only the low bit of each symbol is significant. Therefore the only symbols we need are 0 and 1.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.