Factor
- Not to be confused with catlangwiki:Factor, the practical, concatenative language..
Factor (also known as Factors) is a zero-dimensional, cell-based, imperative programming language invented by User:Bangyen in 2020.
Paradigm(s) | Imperative |
---|---|
Designed by | Bangyen Pham |
Appeared in | 2020 |
Memory system | Cell-based |
Dimensions | Zero-dimensional |
Computational class | Turing complete |
Major implementations | Java interpreter |
Influenced by | Brainfuck |
File extension(s) | .fact |
Language overview
Factor is based on the prime factorization of numbers. Every program is a number, and the instructions of each program are determined by the number's prime factors and their multiplicity. Each factor is a different instruction (based on it's residue modulo 11), and each factor's multiplicity is the number of times the instruction is carried out. The order of the instructions is determined by sorting the factors in ascending order. The different instructions are as follows:
Move right | Move left | Increment | Decrement | Output | Input | Start loop | End loop | |
---|---|---|---|---|---|---|---|---|
Z₁₁ Residue | 1 |
2 |
3 |
4 |
5 |
6 |
7 |
8
|
Brainfuck equivalent | > |
< |
+ |
- |
. |
, |
[ |
]
|
Left and right refer to moving the pointer, whereas increment and decrement refer to changing the cell designated by the pointer. Loops are started if the current cell value is nonzero, and loops are ended if the current cell is zero. The cells are right unbounded and wrap at 0 and 256. All residue values other than 12345678
should be ignored. All characters other than 0123456789
should be considered comments and ignored.
Examples
Hello, World!
This program prints out the words Hello World!:
1655681263349701521084659680611551719864071403625859675993155360184979650875317924075071663014170796398214200089605837256575759246478855815981943506169969378179918285035832792782321874423879673381143676538661836790083866016752674868707301142092304365222517116382208838942082995905598124019955549
Cat
The Cat program, on the other hand, is represented as the number 310861643
. The brainfuck equivalent would be ,[.,]
, since its prime factorization is 17 * 29 * 71 * 83 * 107
. The equivalence is a result of the following congruencies:
Modular Equation | Instruction |
---|---|
17 ≡ 6 (mod 11) | Input |
29 ≡ 7 (mod 11) | Start loop |
71 ≡ 5 (mod 11) | Output |
83 ≡ 6 (mod 11) | Input |
107 ≡ 8 (mod 11) | End Loop |
Truth-machine
A polyglot truth-machine by User:Salpynx.
233915737501853959241591127266540514014498928384925170744745371936977107366667491950094954248611898080571424768
Brainfuck interpreter
A brainfuck interpreter—original brainfuck code by Daniel B. Cristofani.
Because dbfi is not public domain, the code has been moved here.
Translations
Brainfuck to Factor
# Ruby program require 'prime' def translate(code) res = 1; i = 2; str = '0><+-.,[]' for char in code.chars while i % 11 != str.index(char) || !i.prime? i += 1 end res *= i end res end
Factor to brainfuck
# Ruby program require 'prime' def translate(num) res = ""; i = 2; str = '0><+-.,[]' while num != 1 while num % i != 0 i += 1 end num /= i res += str[i % 11] end res end
Computational class
The number 11 was chosen because it is the smallest number such that there exists at least 8 (the number of instructions) natural numbers less than and coprime to it. The reason why coprimality is important is because Dirichlet's theorem ensures that there are an infinite number of primes that are congruent to each coprime integer modulo 11. As a result, because Factor is equivalent to Brainfuck, a Turing-complete language, Factor is also Turing complete.
External Resources
- Modular arithmetic
- Coprime integers
- Dirichlet's theorem
- Java interpreter
- Python interpreter (The link actually takes you to the talk page of this article, since that is where the interpreter's source code is.) by User:None1.