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.

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 at 8:35
3  
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 at 9:48
2  
@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 at 11:19
2  
i liked the restriction @KyleKanos, even though left-handed people disgust me –  ardnew Apr 21 at 15:21
2  
@ardnew Of all the places, this is one of the least likely I would expect such handist comments :P –  DigitalTrauma Apr 21 at 16:16
show 2 more comments

7 Answers 7

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

Mathematica, 26, 22, 16 18 chars

{0,1,2}+⌊(2+√3)^n⌋
share|improve this answer
    
A bit too much golfing: it won't work in the current form (see here). –  Howard Apr 22 at 5:13
    
@Howard Thanks. –  alephalpha Apr 22 at 7:27
add comment

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 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 at 14:27
add comment

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
$ 
share|improve this answer
add comment

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
add comment

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

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.

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.