Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The Seven-Eleven problem is find 4 positive numbers (with two decimal digits, A.BC) such that their sum is equal to their product and is equal to 7.11.

a + b + c + d = a * b * c * d = 7.11
0 < a <= b <= c <= d

Write the shortest program to compute all non-decreasing quadruples. Precomputing the values is not allowed.

share|improve this question
    
Since both addition and multiplication are commutative, the last constraint of "0 < a <= b <= c <= d" is somewhat redundant; one simply has to solve for any a, b, c, and d that satisfies the other constraints and then sort the values in ascending order. –  ESultanik Jun 17 '11 at 15:41
    
At first sight, two negative numbers would lead to an positive product as well, and could lead to a sum which is a solution. So (a,b,c,d) >= 0 would be fine. (!= 0 follows for some other reason from the question). –  user unknown Jun 17 '11 at 17:20
    
Maybe, Alexandru, you like to accept some answers? 0% accept rate doesn't look good. –  user unknown Jun 17 '11 at 17:21
    
@user unknown: I need to follow up on how to consider accepted solution since I don't want to accept only CodeGolf scripts. –  Alexandru Jun 17 '11 at 18:05
    
If you are posting code-golf tasks, then the accepted answer should be the shortest correct solution to the task. Not much to argue there, actually. As for me, I don't particularly care about accept rate, much less on this site, but for code-golf finding an answer to accept is very easy and very non-subjective ;-) –  Joey Jun 20 '11 at 10:13
add comment

7 Answers 7

up vote 4 down vote accepted

Ruby, 105 characters

Unfortunately I didn't get it below the 100 mark. It just brute forces all possible tuples and checks afterwards. Nevertheless it still finishes in finite time.

(1..m=711).map{|a|(a..m).map{|b|(b..m).map{|c|d=m-a-b-c;p [a/u=1e2,b/u,c/u,d/u]if d>=c&&a*b*c*d==m*1e6}}}

The formatted version looks like this:

(1..m=711).map{ |a|
  (a..m).map{ |b|
    (b..m).map{ |c|
      d=m-a-b-c
      p [a/u=1e2,b/u,c/u,d/u] if d>=c && a*b*c*d==m*1e6
    }
  }
}
share|improve this answer
add comment

Wolfram Alpha (51 characters)

You can also run it yourself.

solve(a+b+c+d=7.11,a*b*c*d=7.11,a>0,b>=a,c>=b,d>=c)
share|improve this answer
    
@ESultanik: While that is very clever, I don't think the OP wanted an exact solution. –  mellamokb Jun 17 '11 at 13:52
1  
Well, Wolfram Alpha is built atop Mathematica, which is a Turing complete programming language, so I figured it was fair game ;-) –  ESultanik Jun 17 '11 at 14:55
    
(a,b,c,d) | x=> x*100 € N isn't fulfilled, is it? –  user unknown Jun 17 '11 at 17:24
1  
I don't think this solution meets the requirement "with two decimal digits, A.BC". –  Joey Adams Jun 17 '11 at 18:44
    
@Joey: The problem just asked to "print out all non-decreasing quadruples"; it didn't say that was the only thing the program was allowed to output! Simply filter out all of the (infinite) other reals that don't have two decimal digits ;-) –  ESultanik Jun 17 '11 at 19:16
show 3 more comments

Haskell - 121

(extra newline added for readability)

h=711
main=mapM(print.map((/100).realToFrac))
     [[a,b,c,d]|a<-[1..h],b<-[a..h],c<-[b..h],let d=h-a-b-c,c<=d,a*b*c*d==h*10^6]

This meets the requirements by only working with numbers with two decimal digits. It computes the solution (there's only one of them) using integer arithmetic, scaled up by 100. Floating-point arithmetic is too untrustworthy for this.

share|improve this answer
add comment

Python (146 152 characters)

Here's a "real" answer, if my Wolfram Alpha one doesn't please:

n=711
r=lambda v:range(v,n)
print [[a*0.01,b*0.01,c*0.01,d*0.01]for a in r(1)for b in r(a)for c in r(b)for d in r(c)if a+b+c+d==n and a*b*c*d==n]

It's very, very inefficient, but it works.

share|improve this answer
    
It looks like I've used basically the same method as Joey, just in a more verbose language :-) –  ESultanik Jun 17 '11 at 19:13
    
My solution (Haskell) was too slow until I computed d rather than brute-forcing it. –  Joey Adams Jun 17 '11 at 22:13
    
Also, I think you can replace def r(v):... with r=lambda v:range(v,n). –  Joey Adams Jun 17 '11 at 22:13
    
@Joey: You're right. Thanks! –  ESultanik Jun 18 '11 at 0:41
add comment

C (229 characters)

Sacrificial offering:

#include<stdio.h>
int main(void){int a,b,c,d;for(a=1;a<=711/4;++a)for(b=a;b<=711/4;++b)for(c=b;c<=711/4;++c){d=711-(a+b+c);if(d<c)break;if(a*b*c*d==711000000)printf("a=%.2f,b=%.2f,c=%.2f,d=%.2f\n",a/100.,b/100.,c/100.,d/100.);}}

Re-formatted for readability:

#include <stdio.h>

int main()
{
    int a, b, c, d;

    for (a = 1; a <= 711 / 4; ++a)
        for (b = a; b <= 711 / 4; ++b)
            for (c = b; c <= 711 / 4; ++c)
            {
                d = 711 - (a + b + c);
                if (d < c) break;
                if (a * b * c * d == 711000000)
                    printf("a = %.2f, b = %.2f, c = %.2f, d = %.2f\n", a / 100., b / 100., c / 100., d / 100.);
            }
}
share|improve this answer
    
You can make it shorter by abusing implicitly-declared arguments, rather than explicitly declaring variables: main(a,b,c,d){ instead of int main(void){int a,b,c,d; –  Joey Adams Jun 17 '11 at 22:10
    
@Joey: thanks, I could do that but I think I'd feel dirty afterwards. ;-) –  Paul R Jun 18 '11 at 7:59
1  
If you feel dirty doing that sort of thing, then you will have some critical limitations in code golf. –  Peter Olson Jun 19 '11 at 6:25
    
@Peter: good point - I may have to work on this –  Paul R Jun 19 '11 at 7:43
1  
b can run to 711/3, c to 711/2. (a,b,c) can then be precomputed (see my scala-solution). By optimising for speed, one would let b run to (711-a)/3, c to (711-a-b)/2 but not for golfing. –  user unknown Jul 12 '11 at 18:35
add comment

Scala 168 139

def h(i:Int)=i/100.0
val x=711
for(a<-1 to x;
b<-a to x;
c<-b to x;
d=x-a-b-c
if(d>c&&h(a)*h(b)*h(c)*h(d)==7.11))yield(h(a),h(b),h(c),h(d))
share|improve this answer
add comment

Mathematica

Solve[
    a + b + c + d == a * b * c * d / 10^6 == 711
        && 0 < a <= b <= c <= d,
    {a, b, c, d},
    Integers
]

Basically, solve it as an integer Diophantine equation. The advantage is that this avoids possible floating-point error.

On my computer, Mathematica runs out of memory, but it might work in principle...

It might be possible to run this on Wolfram Alpha, but I'm not familiar enough with its syntax.

In Mathematica, this is 58 characters + 1 fraction line + 1 exponentiation box.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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