-3
\$\begingroup\$

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.

\$\endgroup\$
11
  • \$\begingroup\$ @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. \$\endgroup\$
    – Jwosty
    Mar 26, 2014 at 23:22
  • \$\begingroup\$ 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? \$\endgroup\$
    – Sylwester
    Mar 26, 2014 at 23:29
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Mar 26, 2014 at 23:34
  • \$\begingroup\$ @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? \$\endgroup\$
    – Jwosty
    Mar 26, 2014 at 23:51
  • 4
    \$\begingroup\$ Whoa, whoa, whoa, you just completely changed the question. Not cool. \$\endgroup\$ Mar 27, 2014 at 0:15

3 Answers 3

3
\$\begingroup\$

Haskell: 162 characters

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
\$\endgroup\$
1
  • 3
    \$\begingroup\$ I think it is cheating to use Haskell for this. It is too awesome. \$\endgroup\$ Mar 27, 2014 at 1:15
2
\$\begingroup\$

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);
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Since REBEL has no primitive number types, this effectively became "Implement math any way you can". \$\endgroup\$ Mar 27, 2014 at 0:12
  • \$\begingroup\$ Just rewrote the question; you will need to update your answer. \$\endgroup\$
    – Jwosty
    Mar 27, 2014 at 0:16
  • \$\begingroup\$ @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. \$\endgroup\$ Mar 27, 2014 at 0:28
  • \$\begingroup\$ Ok, on second thought, I shouldn't change it so dramatically (that's what the sandbox is for :/). I'll roll it back. \$\endgroup\$
    – Jwosty
    Mar 27, 2014 at 0:56

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