15
\$\begingroup\$

An Almost Equilateral Heronian Triangle is a triangle with integer lengths of the form n-1, n, and n+1 and also has integer area. The first few are:

 3,  4,  5 ->    6
13, 14, 15 ->   84
51, 52, 53 -> 1170

Quest: Generate the shortest program that outputs the nth such triple. (Hint: this is a known sequence).

Winner will be selected on May 2, 2014.

\$\endgroup\$
7
  • \$\begingroup\$ For what it's worth, this is equivalent to one of the more widely studied Pell equations. \$\endgroup\$ Apr 21, 2014 at 8:35
  • 3
    \$\begingroup\$ I don't really understand the downvotes. This is a fairly simple task but I don't see any problem with the question. But I have to say (as a left handed person) that the restriction is one of the weirdest and easiest to comply with I have ever seen. \$\endgroup\$ Apr 21, 2014 at 9:48
  • 2
    \$\begingroup\$ @steveverrill, although I didn't downvote I did choose not to upvote because of the pointless restriction. I suspect that the close votes are also because of the restriction: I bet that less than 10% of the world's population use the same keyboard layout as Kyle. \$\endgroup\$ Apr 21, 2014 at 11:19
  • 2
    \$\begingroup\$ i liked the restriction @KyleKanos, even though left-handed people disgust me \$\endgroup\$
    – ardnew
    Apr 21, 2014 at 15:21
  • 2
    \$\begingroup\$ @ardnew Of all the places, this is one of the least likely I would expect such handist comments :P \$\endgroup\$ Apr 21, 2014 at 16:16

8 Answers 8

8
\$\begingroup\$

Mathematica, 26, 22, 16 18 chars

{0,1,2}+⌊(2+√3)^n⌋
\$\endgroup\$
1
  • \$\begingroup\$ A bit too much golfing: it won't work in the current form (see here). \$\endgroup\$
    – Howard
    Apr 22, 2014 at 5:13
6
\$\begingroup\$

APL, 15 14 charaters

0 1 2+⌊⎕*⍨2+√3

Same approach as alephalpha's solution but uses floor instead of the correction term.

Thank you to algorithmshark for pointing out that the commute operator saves one char.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ (⍳3) saves a char over 0 1 2, and I'm pretty sure you can use Commute to make it ⎕*⍨2+√3 and save another. \$\endgroup\$ Apr 21, 2014 at 18:45
  • \$\begingroup\$ @algorithmshark Thank you for those ideas. Unfortunately ⍳3 yields 1 2 3 and thus is one char longer. \$\endgroup\$
    – Howard
    Apr 22, 2014 at 5:09
5
\$\begingroup\$

GolfScript (24 21 chars)

2 4@~{.4*@-}*;.(\.)]p

Takes input on stdin, gives output to stdout in the form

[3 4 5]

Online demo

Note that I've assumed that the 0th element of the sequence is [1 2 3] (with area 0), which I think is consistent with OEIS A003500.

With thanks to Howard for a 3-char saving.

\$\endgroup\$
2
  • \$\begingroup\$ Using (.).)] is two chars shorter. Moreover, if you start with 2 4 you can replace \; with ; and save an additional one. \$\endgroup\$
    – Howard
    Apr 21, 2014 at 14:09
  • \$\begingroup\$ @Howard, I originally had 2 4 and treated [3 4 5] as the 0th element, so I'm embarrassed not to have spotted that alternative way to exploit the offset. Thanks. \$\endgroup\$ Apr 21, 2014 at 14:27
2
\$\begingroup\$

GNU dc, 30 19 bytes

9k3v2+?^0k1/p1+p1+p

This uses the same trick as @Howard's APL answer so only one term has to be calculated. Takes input for n from stdin.

Output:

$ dc -e '9k3v2+?^0k1/p1+p1+p' <<< 1
3
4
5
$ dc -e '9k3v2+?^0k1/p1+p1+p' <<< 2
13
14
15
$ dc -e '9k3v2+?^0k1/p1+p1+p' <<< 3
51
52
53
$ 
\$\endgroup\$
1
\$\begingroup\$

Python 77

Quite a verbose implementation in Python

[(a-1,a,a+1)for a in(int((2+3**.5)**t+(2-3**.5)**t+.1)for t in range(N))][-1]
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Are we supposed to replace N with a value? Your program doesn't ask for any input. \$\endgroup\$
    – golfer9338
    Apr 21, 2014 at 22:17
1
\$\begingroup\$

Python 3, 83 characters

f=lambda t:4*f(t-1)-f(t-2)if t>2 else(4,14)[t-1];n=f(int(input()));print(n-1,n,n+1)

This uses a recursive solution, taking advantage of the fact that (quote from Wikipedia):

Subsequent values of n can be found by multiplying the previous value by 4, then subtracting the value prior to that one (52 = 4 × 14 − 4, 194 = 4 × 52 − 14, etc.)

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ECMAScript 6) - 52 Characters

f=x=>x?--x?4*f(x)-f(x-1):4:2
g=x=>[a=f(x)-1,a+1,a+2]

Defines a recursive function f which returns the nth term and a function g which returns an array containing the corresponding triple.

JavaScript - 41 Characters

for(a=2,b=4;--x;)b=-a+4*(a=b);[a-1,a,a+1]

Expects the term to be calculated to be stored in the global variable x and outputs the triple to the console.

\$\endgroup\$
1
\$\begingroup\$

CJam, 13 bytes

3,3mq))ri#if+p

The first version of CJam is 10 days older than this challenge, but I don't know if all of the features I'm using have been present back then. The challenge is officially closed anyway though, so...

Test it here.

Explanation

3mq            e# Push √3.
   ))          e# Increment twice.
     ri        e# Read input and convert to integer.
       #       e# Raise 2+√3 to that power.
        i      e# Convert to integer, truncating the result.
         3,    e# Push [0 1 2]
           f+  e# Add the previous number to each of these.
             p e# Pretty-print the result.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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