Problem of the Day
A new programming or logic puzzle every Mon-Fri

No Plus

Let's start Thursday off with a tricky problem. Your goal is to great a function that takes in 2 integers and returns the sum of them. The trick is that your program can not use the '+' operator.

Good luck!

Permalink: http://problemotd.com/problem/no-plus/

Comments:

  • Mark - 10 years, 1 month ago

    private int Sum(int a, int b) { var array = new int[] { a, b };

       return array.Sum();
    

    }

    reply permalink

  • Jordi - 10 years, 1 month ago

    with nummern (a,b) as                                         
    (select 12, 89 from sysibm/sysdummy1)                         
    select trim(char(a)) concat '+' concat                        
           trim(char(b)) concat '=' concat                        
           trim(char(length(repeat('*',a) concat repeat('*',b)))) 
    from nummern                                                  
    

    ==>

    CONCAT
    12+89=101

    reply permalink

  • dpim - 10 years, 1 month ago

    public static int sumInt(int a, int b){
        return a - (-b);
    }
    

    reply permalink

  • Anon - 10 years, 1 month ago

      public static int add (int n1, int n2) {
        BigInteger x = BigInteger.valueOf(n1);
        BigInteger y = BigInteger.valueOf(n2);
        BigInteger sum = x.add(y);
        return sum.intValue();
      }
    

    reply permalink

  • Anonymous - 10 years, 1 month ago

    Add a negative instead.

    def add(n1,n2):
        return (n1 - (-n2))
    
    print (add(int(input('First number: ')),int(input('Second number: '))))
    

    reply permalink

  • Anonymous - 10 years, 1 month ago

    *Subtract a negative, I mean

    reply permalink

  • Anonymous - 10 years, 1 month ago

    def add(n1,n2):
        answer = []
        for x in range(n1):
            answer.append(' ')
        for x in range(n2):
            answer.append(' ')
        return(len(answer))
    
    print(add(int(input('First number: ')),int(input('Second number: '))))
    

    reply permalink

  • David - 10 years, 1 month ago

    Wow. I really don't even need to take any real effort to circumvent "typical" languages to do this with out explicitly using the build in addition functionality of a given language...

    I will none the less do this in verilog. Because I haven't used it in about a year and a half. Not having a compiler handy on this computer, I really hope my syntax is correct... o.o

    module full_adder(sout, cout, a, b, cin);
      input a, b, cin;
      output sout, cout;
      wire a, b, cin, sout, cout;
      assign sout = (a ^ b) ^ cin;
      assign cout = ((a & b) | (a & cin)) | (b & cin);
    endmodule
    
    module int_adder(Sout, Cout, A, B, Cin);
      input[31:0] A, B;
      input Cin;
      output[31:0] Sout;
      output Cout;
      wire [31:0] A, B, Sout;
      wire Cin, Cout;
      wire[30:0] cout_internal;
      full_adder fa0(   .sout   (Sout[0]),
                        .cout   (cout_internal[0]),
                        .a      (A[0]),
                        .b      (B[0]),
                        .cin    (Cin));
      full_adder fa1(   .sout   (Sout[1]),
                        .cout   (cout_internal[1]),
                        .a      (A[1]),
                        .b      (B[1]),
                        .cin    (cout_internal[0]));
      full_adder fa2(   .sout   (Sout[2]),
                        .cout   (cout_internal[2]),
                        .a      (A[2]),
                        .b      (B[2]),
                        .cin    (cout_internal[1]));
      full_adder fa3(   .sout   (Sout[3]),
                        .cout   (cout_internal[3]),
                        .a      (A[3]),
                        .b      (B[3]),
                        .cin    (cout_internal[2]));
      full_adder fa4(   .sout   (Sout[4]),
                        .cout   (cout_internal[4]),
                        .a      (A[4]),
                        .b      (B[4]),
                        .cin    (cout_internal[3]));
    [...etc...]  
      full_adder fa31(   .sout   (Sout[0]),
                        .cout   (Cout),
                        .a      (A[0]),
                        .b      (B[0]),
                        .cin    (COut));
    
    endmodule //int_adder
    
    

    The output wasn't working conveniently with he online compiler and I lost interest in this specifically. I'm sure there's a bug in here somewhere. And the comment box is complaining at me for posting the full thing in one box -_-
    Here is a code generator in Python:

    for x in range(32):
        if x == 0:
            print """full_adder fa0(   .sout   (Sout[0]),
                        .cout   (cout_internal[0]),
                        .a      (A[0]),
                        .b      (B[0]),
                        .cin    (Cin));"""
        else:
            if x == 31:
                print """  full_adder fa31(   .sout   (Sout[0]),
                        .cout   (Cout),
                        .a      (A[0]),
                        .b      (B[0]),
                        .cin    (COut));"""
            else:
                print """  full_adder fa%i(   .sout   (Sout[%i]),
                        .cout   (cout_internal[%i]),
                        .a      (A[%i]),
                        .b      (B[%i]),
                        .cin    (cout_internal[%i]));""" % (x, x, x, x, x, x-1)
    

    reply permalink

Content curated by @MaxBurstein