Sign up ×
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.

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.

share|improve this question
For what it's worth, this is equivalent to one of the more widely studied Pell equations. – Peter Taylor Apr 21 '14 at 8:35
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. – steveverrill Apr 21 '14 at 9:48
@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. – Peter Taylor Apr 21 '14 at 11:19
i liked the restriction @KyleKanos, even though left-handed people disgust me – ardnew Apr 21 '14 at 15:21
@ardnew Of all the places, this is one of the least likely I would expect such handist comments :P – Digital Trauma Apr 21 '14 at 16:16

8 Answers 8

up vote 6 down vote accepted

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.

share|improve this answer
(⍳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. – algorithmshark Apr 21 '14 at 18:45
@algorithmshark Thank you for those ideas. Unfortunately ⍳3 yields 1 2 3 and thus is one char longer. – Howard Apr 22 '14 at 5:09

Mathematica, 26, 22, 16 18 chars

share|improve this answer
A bit too much golfing: it won't work in the current form (see here). – Howard Apr 22 '14 at 5:13
@Howard Thanks. – alephalpha Apr 22 '14 at 7:27

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.

share|improve this answer
Using (.).)] is two chars shorter. Moreover, if you start with 2 4 you can replace \; with ; and save an additional one. – Howard Apr 21 '14 at 14:09
@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. – Peter Taylor Apr 21 '14 at 14:27

GNU dc, 30 19 bytes


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


$ dc -e '9k3v2+?^0k1/p1+p1+p' <<< 1
$ dc -e '9k3v2+?^0k1/p1+p1+p' <<< 2
$ dc -e '9k3v2+?^0k1/p1+p1+p' <<< 3
share|improve this answer

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]
share|improve this answer
Are we supposed to replace N with a value? Your program doesn't ask for any input. – golfer9338 Apr 21 '14 at 22:17

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.)

share|improve this answer

JavaScript (ECMAScript 6) - 52 Characters


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


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

share|improve this answer

CJam, 13 bytes


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.


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.
share|improve this answer

Your Answer


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.