Slashdot Mirror


Programming Challenge: Triangles Puzzle

Frank Buss writes "Last week was a challenge to write a program, which solves a simple geometric problem. There was nothing to win, only the solutions at the end are the win for all readers, but nevertheless the response was great (some thousands of web-hits) and there are some nice solutions."

40 comments

  1. lots of Lisp people out there. by sgant · · Score: 2, Interesting

    Interesting contest. I was wondering though, (as I have no experience or knowledge of programming and my math is laughed at by my 11 year old) wouldn't a program like Mathematica or Maple be able to handle something like this with ease? Don't they have programming interfaces?

    Or is the problem so simple that it's kinda overkill for them? Or is it just plain easier to do it with Lisp or Python?

    --

    "Leo Fender was in a 'state of grace' when he designed the Stratocaster." -- Paul Reed Smith
    1. Re:lots of Lisp people out there. by evilmousse · · Score: 4, Informative


      No, I'm pretty sure mathematica would work fine.
      It does indeed have it's own language, and plenty of permutation/combination logistics.
      I'm guessing it'd be more useful to someone seeking a mathematical proof/theorem regarding the problem, listing complex genral-case equations at each step.
      The more common programming languages are likely more productive, being concerned more with just the result than the theory at each step. tho I do know many math majors that would have an easier time programming it in mathematica, just because that's what they're used to.
      Lisp is the logical language choice for this or any other heavily-combinational/AI task, though you'll never catch me programming in it.

  2. Looky... by maunleon · · Score: 4, Funny

    Shhh... I see dead languages.

    1. Re:Looky... by ArrayMac · · Score: 1

      There's only one thing dead about APL, and I'm sad about that.

      It's more of a Mark Twain language: reports are greatly exaggerated.

  3. Only one solution by Anonymous Coward · · Score: 2, Interesting

    Most of the entries are complex programs that calculate the answer. I only saw one person that solved the problem purely at the mathematical level.

    That is the "J" entry, and in fact that same solution would work in all the other languages in 1 line of code (for the most part).

    1. Re:Only one solution by erykjj · · Score: 2, Interesting

      The point is to have the computer figure it out, rather than providing a formula that a human being came up with. I guess there is always going to be a fine line as to how much "human" logic goes into a program, hence the variety of solutions.

    2. Re:Only one solution by Anonymous Coward · · Score: 0

      Eh? What you're describing is the difference between counting on your fingers and executing an algorithm.

    3. Re:Only one solution by erykjj · · Score: 1

      No, actually, you'd follow an algorithm to determine a "formula". In the code, you could tell the computer to follow an algorithm or to calculate the result of the formula, bypassing the first step altogether.

    4. Re:Only one solution by Evil+Pete · · Score: 3, Interesting

      What's more it is in a language ('J') that is derived from APL ! He gets high geek points for even understanding anything derived from APL much less writing quick solutions in it.

      --
      Bitter and proud of it.
    5. Re:Only one solution by Anonymous Coward · · Score: 0

      An interresting charateristic of J is that the same code that solves for one triangle, can solve for any number of triangles, without any looping constructs!

      Try this with your favorite language:
      Write the formula:

      nt =: *-:@*+

      Now solve simultaneously for triangles of 8,6; 9,5; 6,7; 5,8; 4,5; 4,4; 3,3; and 2,5.

      Answer:
      (8 9 6 5 4 4 3 2) nt (6 5 7 8 5 4 3 5)
      336 315 273 260 90 64 27 35

      better yet, solve for any order triangle from 0 order to n order (all combinations)

      n =. 4 **** Set the max order four

      y =. (],]) $ i. **** generate all the permutations (including zero)

      (,|:y n) nt (,y n) ***** calc all the tri counts

      0 0 0 0 0 1 3 6 10 3 8 15 0 6 15 27 *** answer

      n =. 6 ****set the max order 6
      0 0 0 0 0 0 0 1 3 6 10 15 0 3 8 15 24 35 0 6 15 27 42 60 0 10 24 42 64 90 0 15 35 60 90 125

  4. I wonder... by Sheetrock · · Score: 2, Interesting

    If the fastest way to compute this is really the mathematical method, or if a technique like evolutionary programming might expose a quicker means of arriving at the result (i.e. a simpler mathematical method).

    --

    Try not. Do or do not, there is no try.
    -- Dr. Spock, stardate 2822-3.




    1. Re:I wonder... by Anonymous Coward · · Score: 0

      See the post right above yours. Or do you read at +5 or something?

    2. Re:I wonder... by Anonymous Coward · · Score: 0

      The one that was posted two minutes later? I'm a bit tired today; must have missed it.

    3. Re:I wonder... by TwistedSquare · · Score: 2, Interesting

      By the time you've used EP on the problem, I think quicker would be out of the window ;) I gather from other comments than manual rearranging is possible (I didn't read the problem too closely admittedly).

  5. More simple solution by eric2hill · · Score: 2, Informative

    Using any handheld calculator with an "x^y" key...

    Take the number of divisions coming from a base vertex of the triangle and raise it to the power of the number of divisions coming from the opposite vertex. In the case given, 3 divisons to the power of 3 divisons = 27.

    --
    LOAD "SIG",8,1
    LOADING...
    READY.
    RUN
    1. Re:More simple solution by gauchopuro · · Score: 1

      What if I add a line between P8 and P9? Your solution no longer works.

    2. Re:More simple solution by Photar · · Score: 3, Interesting

      Except if the number of divisions isn't the same on both sides. In which case its
      ((x*y)*(x+y))/2
      x = left side divisions
      y = right side divisions

      --
      He who knows not and knows he knows not is a wise man. He who knows not and knows not he knows not is a fool.
    3. Re:More simple solution by blankman · · Score: 2, Informative

      What if I add a line between P8 and P9? Your solution no longer works.

      Notice that all the additional lines in the problem intersect either P0 or P1. Adding a line between P8 and P9 significantly changes the problem.

      You are correct though, in saying that x^y is not the solution. A few of the solutions did work out general formulae, the simplest I saw being (1/2)*(m*n)*(m+n)

  6. Dirty pseudoalgorithm easyly implemented in C by Anonymous Coward · · Score: 0
    set_of_solutions->init();
    int a,b,c;
    for (a=0;a<=8;a++) {
    for (b=a+1;b<=9;b++) {
    for (c=b+1;c<=10;c++) {
    if (triangle_with_area_gt_0(a,b,c)) {
    set_of_solutions->add(a,b,c);
    // it doesn't add repeated solutions
    }}}}
    printf("# of solutions = %d\n",set_of_solutions->size());

    open4free ©

  7. Why not Prolog? by ameoba · · Score: 4, Interesting

    Lots of Lisp, why no Prolog? Looks like a textbook problem for an intro prolog class.


    % ameoba@girl:~/triangle$ gprolog
    % GNU Prolog 1.2.18
    % By Daniel Diaz
    % Copyright (C) 1999-2004 Daniel Diaz
    % | ?- consult('tri.pl').
    % compiling /home/ameoba/triangle/tri.pl for byte code...
    % /home/ameoba/triangle/tri.pl compiled, 33 lines read - 4628 bytes written, 10 ms
    %
    % yes
    % | ?- numtri(X).
    %
    % X = 27
    %
    % yes
    % | ?-

    line([0,5,8,10]).
    line([0,1]).
    line([1,6,9,10] ).
    line([0,3,7,9]).
    line([0,2,4,6]).
    line([1,2, 3,5]).
    line([1,4,7,8]).

    edge(X,Y) :-
    line(L),
    member(X,L),
    member(Y,L),
    X > Y.

    colinear(X,Y,Z) :-
    line(L),
    member(X,L),
    member(Y,L),
    member(Z,L).

    tri(X,Y,Z) :-
    edge(X,Y),
    edge(X,Z),
    edge(Y,Z),
    X > Y,
    Y > Z,
    \+ colinear(X,Y,Z).

    numtri(X) :-
    setof([X,Y,Z], tri(X,Y,Z), Tris),
    length(Tris,X).


    35 lines of code in about 45min (mostly remembering syntax & predicates) and it's definately simpler than any of the other solutions.

    --
    my sig's at the bottom of the page.
    1. Re:Why not Prolog? by p3d0 · · Score: 1

      ...and not a single comment.

      --
      Patrick Doyle
      I mod down every jackass who puts his moderation policy in his sig. Oh, wait a sec....
    2. Re:Why not Prolog? by gauchopuro · · Score: 1
      ...it's definately simpler than any of the other solutions.

      Have you looked at the Haskell solution? It is very similar to yours. But with that said, your Prolog solution does seem to be just a bit clearer than the Haskell solution. Although Haskells lists and list comprehensions do a good job of modelling the problem, the use of lists is an implementation detail that is explicitly coded into the solution. The Prolog solution looks like a pure specification for the problem.

    3. Re:Why not Prolog? by Anonymous Coward · · Score: 0

      comments are evil in languages like lisp and prolog: the language is sufficiently high-level that if you feel the need for a comment, you know your code isn't clear/abstracted enough.

    4. Re:Why not Prolog? by p3d0 · · Score: 1
      Uh, that's total bullshit. Code can only say what it's doing. It can't describe why. It can't explain implementation limitations (eg. "this is an O(n^2) algorithm, but I have chosen it for simplicity because I don't expect n to be larger than 25"). It can't contain TODOs (Fred Brooks' "purple wires").

      If you think you can get away with no comments in lisp or prolog, then you haven't written very large lisp or prolog programs.

      --
      Patrick Doyle
      I mod down every jackass who puts his moderation policy in his sig. Oh, wait a sec....
    5. Re:Why not Prolog? by fatphil · · Score: 1

      Are the two '>' predicates needed in tri()?

      Note, I've never even _seen_ any prolog before, I'm trying to work out how it might work (prolog, that is) from your code.

      It just that you have those conditions in edge(), so it looks like they're redundant in tri()

      FP.

      --
      Also FatPhil on SoylentNews, id 863
    6. Re:Why not Prolog? by Anonymous Coward · · Score: 0

      Thanks, really nice solution, I've added it to the challenge page.

      Frank

  8. Need more challenges by miyako · · Score: 4, Interesting

    We need more challenges like this. I remember seeing this when it was posted on usenet a while ago, and my interest was peaked. Not sure if it was my math or programming skills that were lacking, but I wasn't able to immediately see a solution, and ended up forgetting to go back to the problem, but I would love to see more challenges like this.
    I remember there was a time when I would spend days coding, now it seems like I spend days trying to think of something interesting to try to code, and usually end up getting distracted by something else.
    I guess it could probably be laziness on my part, but I would like to see more challenges like this (perhaps a website that posts a monthly programming challenge, maybe cycling through different types of challenges like math, text procesing, optimization, etc) for people to take on. Does something like this already exist?
    Somewhat off topic, but I would also like to see a site that every couple of weeks or something posts a different open source project that looks promising and needs help getting something specific to work.
    As a matter of fact, the above two problems sound like something that would make a good and interesting website, maybe even offering a chance at doing some overly complex and unnessecary PHP programming.
    If anyone is interested in working on a website like this, send me an email at:miyako at gmail dot com

    --
    Famous Last Words: "hmm...wikipedia says it's edible"
    1. Re:Need more challenges by erlando · · Score: 1

      TopCoder.com. 'Nuff said..

      --
      Remember, there are no stupid questions. But there are a lot of inquisitive idiots.
    2. Re:Need more challenges by Anonymous Coward · · Score: 2, Informative
    3. Re:Need more challenges by meme_police · · Score: 2, Informative

      Pedant mode: it's piqued, meaning aroused, not peaked.

      --

      The meme police, They live inside of my head

  9. Quote fromYoda not Spock!! by Frans+Faase · · Score: 1

    I thought that quote in the sig was from Yoda not from Spock.

    1. Re:Quote fromYoda not Spock!! by Anonymous Coward · · Score: 0

      As noted before, he's trolling with his sig. Aside from it being yoda, mr. spock is the star trek philosopher, dr. spock wrote books on child rearing.

  10. One problem I've always wanted to solve... by tjlsmith · · Score: 1

    If you have a triangle on a plain with three points x1, y1 x2, y2 and x3, y3 and you select a point on the plane x4, y4, how do you prove the point is in the triangle or not?

    --
    Mumia Abu-Jamal is *laughably guilty*. Check the evidence.
    1. Re:One problem I've always wanted to solve... by DeanAsh · · Score: 1
      Find the angle from pt1 to pt4 to pt2 (+ve if clockwise, -ve if not). Find the angle from pt2 to pt4 to pt3. And again from pt3 to pt4 to pt 1.

      If the angles sum to 360 degrees, p4 is within the triangle. If they sum to 0, it is not.

      --
      What is the shortest sig that cannot be expressed in fewer than 20 words?
    2. Re:One problem I've always wanted to solve... by Anonymous Coward · · Score: 1, Insightful

      The three points (call them p1, p2, p3) form three lines - any two of the points describe a line on the plain. Now determine, with respect to each of those lines, if p4 is on the same side of the line as the third point. So, if p4 is on the same side of p1-p2 as p3, and on the same side of p1-p3 as p2, and on the same side of p2-p3 as p1 - then it is in the triangle.

    3. Re:One problem I've always wanted to solve... by Anonymous Coward · · Score: 0

      suppose you already have triangle ABC, and you want to know if D lies in the triangle. create triangles ABD, ACD, and BCD. if area(ABC) = area(ABD) + area(ACD) + area(BCD), then it lies in the triangle.

  11. Challenge 1b: Provably Correct Solutions by Philip+Dorrell · · Score: 1

    For Lisp you could use ACL2. For Java you could use JML , together with Krakatoa, Why (which could also be used to program the solution) and Coq. For all the other languages that appear in the solutions list I think you're on your own.

    --
    Music: a super-stimulus for the perception of musicality. Musicality: a perceived aspect of speech.
  12. Problem statement is flawed. by cakoose · · Score: 1

    The problem is to create a function that takes NO input and produces 27.

    echo 27

    There's some hand-waving about making the program general-purpose, but it's too imprecise to be of any use. You need inputs.

  13. Good performance from Java by pfafrich · · Score: 1

    I was quite supried to see how small in terms of line count the Java solution was. I was expecting to see a much higher disparity between the lisp like solutions and more standard languages.

    --
    There are four sorts of people in the world: fools, lunatics, idiots and morons. - Umberto Eco, Foucaut's pendulum.
  14. The most clever solution by Anonymous Coward · · Score: 0

    Was the one written in SQL. A very innovative use of a database language.