# Math without numbers [duplicate]

You must write functions for addition, subtraction, multiplication, and division; however, you can never use any of your language's number types / primitives -- this means no ints, no floats, no Integers; no nothing. You cannot reference these types at all; not even implicitly, e.g. no pushing and popping numbers on a stack.

Valid statically-typed pseudocode (does not solve the problem):

boolean result = true && false
string helloWorld = "hello" + " world"
Object[] things = [new Object(), "foo", true]


Invalid statically-typed pseudocode (does not solve the problem):

float num = 42F
Object foo = "100".toInt()


To make things simpler, pointers are allowed.

This is code-golf, so make it short and sweet.

• @Sylwester not a duplicate; here you are allowed to use anything but number primitives, and this is also a code golf, not a popularity contest. Mar 26, 2014 at 23:22
• So basically it's a code golf where the specification is to make the four math operations and the user gets to define how numbers are to be represented since you cannot use numbers not numeric functions? Mar 26, 2014 at 23:29
• If we can't refer to built-in number types, the input has to be some other type. We just declare that it's some other type that already supports the operations we want - for example, NumPy array types in Python - and then all the work is shoved into that code. Our code degenerates to add=lambda x,y:x+y. This question needs more work. Mar 26, 2014 at 23:34
• @user2357112 NumPy code, for example, would be invalid because it seems that you must explicitly use integer literals to initialize the arrays/matrices, am I correct? Mar 26, 2014 at 23:51
• Whoa, whoa, whoa, you just completely changed the question. Not cool. Mar 27, 2014 at 0:15

This is algebraic data types 101.

data N=E|Z|P N
a x Z=x
a x (P y)=a(P x)y
s x Z=x
s Z _=E
s (P x)(P y)=s x y
m _ Z=Z
m Z _=Z
m x (P y)=a x$m x y d _ Z=E d x (P Z)=x d Z _=Z d x y=a(P Z)$d(s x y)y

• I think it is cheating to use Haskell for this. It is too awesome. Mar 27, 2014 at 1:15

# Javascript

Example: 24.5-20.5+2*1 = bd.e-bz.e+b*a = 6

 f="bd.e-bz.e+b*a";
n=function(a){
if(a.match('^[+/*.-]$')) { return a; } if(a=="z") {a="";} return a.charCodeAt()-"".charCodeAt(); } o=[]; tx=f.split(""); tx.forEach(function(t,idx){ o.push(n(t)); }); o=o.join(""); eval(o);  ## REBEL 3 + 9 + 23 + 29 = 64 Addition: /;/  Subtraction: /0;0/;/;/  Multiplication: /0(0+;(0*))/$1:$2/0;|:/  Division: /(0+)(0*;\1$)/1$2/10|1;/1/1/0  Input format: 2 strings of 0s, separated by a semicolon. The value is the length of the string. 0000;00  Output format: 1 string of 0s, with the same semantics. 000000  Since this works with natural numbers (positive integers), subtraction and division will not always yield the correct answer. Subtraction yields the absolute value of the result, and division rounds down. These are not complete programs, but they use the current state of the program as input. Similarly, they leave the program in the result state when they are done. Here is an example of how to use them (using division as an example): 000000;00/(0+)(0*;\1$)/1\$2/10|1;/1/1/0


When this terminates, the current state will be 000.

• Since REBEL has no primitive number types, this effectively became "Implement math any way you can". Mar 27, 2014 at 0:12
• Just rewrote the question; you will need to update your answer. Mar 27, 2014 at 0:16
• @Jwosty I probably won't change it. This answer was valid when it was posted, and changing it to the new question would be several orders of magnitude harder. Mar 27, 2014 at 0:28
• Ok, on second thought, I shouldn't change it so dramatically (that's what the sandbox is for :/). I'll roll it back. Mar 27, 2014 at 0:56