The challenge

The program must return a valid sequence of numbers into some rules


  • x is the size of the sequence;
  • x cannot be less than 4 or greater than 9 (Thanks David Carraher);
  • Digits of the sentence can't repeat, each one must appear once;
  • All digits between 1 to x must be in the sequence;
  • A valid sequence never begins nor ends with 1 or x;
  • Into the sequence, numbers can never appear "together";


If x=4:
2413 valid
3142 valid

1423 not valid, because begins with 1 and 23 are "together".
2134 not valid, because ends with x and 34 are "together".

Still don't get it? Other example:

If x=8:
28375146 valid, because don't start with 1 nor x(8), don't end with 1 nor x, and no numbers are "together"(no numbers touches, no numbers are the next or previous)

Sorry, I really don't know how to explain the last rule. Hope the example make it understandable.

Additional Information

  • x does not need to be an input, you can show the outputs for x=4 and x=9 working;
  • Since any language will be accepted and, I don't know how all of them work, I will ask you people to help me with this;
  • The winner will be the one who got more up-votes in 1 week, so please, vote the answer you find the best; I will mark it as the Correct one;

Good, luck. =)

  • \$\begingroup\$ Is it correct to assume that x can not be greater than 9? \$\endgroup\$
    – DavidC
    Sep 17, 2012 at 2:59
  • \$\begingroup\$ Sure, forgot it but already edited. Thanks! \$\endgroup\$ Sep 17, 2012 at 3:01
  • \$\begingroup\$ 23 are together; Are 32 together? \$\endgroup\$
    – DavidC
    Sep 17, 2012 at 3:02
  • 1
    \$\begingroup\$ I recommend you amend the challenge to allow for numbers greater than 9. (Otherwise it is trivial to solve. Consider 952647183, for example.) You can make the challenge much harder by requiring that the answer be given as a list. E.g. {2,4,1,3} \$\endgroup\$
    – DavidC
    Sep 17, 2012 at 3:35
  • 1
    \$\begingroup\$ How is input provided and what is the winning criteria? \$\endgroup\$
    – ardnew
    Sep 17, 2012 at 16:15

9 Answers 9


GolfScript (17 16 chars)

Assuming input on stdin and output on stdout.


Almost alphanumeric-free...

  • \$\begingroup\$ Oct 31, 2011: Happy Halloween! '?' operator now works correctly for finding substrings in strings, or strings in arrays. Thank you Peter Taylor! Omg Who are you? \$\endgroup\$ Sep 17, 2012 at 22:23
  • \$\begingroup\$ @BernaMariano, that was a pretty small patch. I just happened to notice someone else's question on StackOverflow and realise that string string ? or string array ? could never return anything useful, so I proposed making them do something slightly different. \$\endgroup\$ Sep 18, 2012 at 5:26
  • \$\begingroup\$ @PeterTaylor Hahahah my next challenge I will add a rule: "Peter Taylor is not allowed to answer" hahaha \$\endgroup\$ Sep 20, 2012 at 20:13

Mathematica 38

This simply returns the digits less than or equal to x, in the order in which they appear below.

Row@Select[{2, 9, 6, 4, 7, 1, 8, 5, 3},#<x+1&]

The results from 4 through 9:


  • \$\begingroup\$ When x=9 the output breaks a rule, the sequence starts with 9 \$\endgroup\$ Sep 18, 2012 at 22:25
  • \$\begingroup\$ Good catch. I missed that rule. That means that the answer for 5 also doesn't work. \$\endgroup\$
    – DavidC
    Sep 18, 2012 at 23:18

May I try to answer my own challenge?

Javascript (35 chars)




4 => 2413
5 => 24153
6 => 246153
7 => 2461753
8 => 24681753
9 => 246819753
  • \$\begingroup\$ Surely i%2?a+=i:a=i+a can be shortened to a=i%2?a+i:i+a? And i>=2 is trivially i>1 \$\endgroup\$ Sep 19, 2012 at 9:45
  • \$\begingroup\$ @PeterTaylor Thanks! Actually I tried a=i%2?a+i:i+a but maybe I was doing something wrong because it didn't work, but I did tried this way... The i>1 you are totally right, my bad :P Already edited, now you can vote it up haha \$\endgroup\$ Sep 20, 2012 at 3:11

Haskell 78

a 4=[3,1,4,2]
a x=let b=filter even [1..x]++filter odd[1..x]in tail b++[b!!0]

This defines a function a that takes one parameter, the desired length of the sequence. This function returns a list of numbers in that sequence. I believe this works for x >= 4

This can probably be golfed further (I don't have that much experience in haskell)

Example (in ghci)

ghci> a 4
ghci> a 9

Haskell 25

This version is from before a rule change that now requires that every digit from 1 to x be used exactly once in the sequence

This should work for any sequence of length > 2 (even though the rules only required 4 <= x <= 9)

putStrLn$take x$cycle"02"


The number of digits in the sequence is stored in x.


x = 6 :: Int


x = 4 :: Int

  • \$\begingroup\$ A recent rule change made this solution wrong. I will fix it soon. \$\endgroup\$
    – Matt
    Sep 17, 2012 at 12:25
  • \$\begingroup\$ Looking forward your fix, @Matt! \$\endgroup\$ Sep 17, 2012 at 12:28

PHP, 64

If I understand the rules correctly, this should work:


If x is odd, this simply cycles through multiples of 2 % x + 1 to produce sequences like 357924681. If x is even, this does the same, but halfway through subtracts 1+x because 2i%x+1 would otherwise never produce even values.

  • \$\begingroup\$ sequences like 357924681 - that one's invalid, because it ends in 1. \$\endgroup\$ Sep 17, 2012 at 14:48
  • \$\begingroup\$ Ah, ok. I thought it could only not begin in 1. In that case, this type of solution will not work. \$\endgroup\$
    – scleaver
    Sep 17, 2012 at 14:49
  • \$\begingroup\$ There is a similar approach which works: count down in odd numbers to 1; then have the largest number; then count up in even numbers from 2. \$\endgroup\$ Sep 17, 2012 at 14:51
  • \$\begingroup\$ I was hoping to utilize non-trivial generators of the set Z/xZ so that I didn't have to use too many branches. \$\endgroup\$
    – scleaver
    Sep 17, 2012 at 14:56

GolfScript, 22 20 chars


Outputs the following sequences for the inputs 4 through 9:


For even inputs, just ~),.+2%(; would suffice. I'm not too happy with the kluge for handling odd input values; there's got to be a more efficient way to do it.

Edit: Saved two chars by changing the output for odd inputs a bit and playing silly stack manipulation tricks. Still not too happy with the length.

  • \$\begingroup\$ I really don't get this GolfScript, how can I compile and run this? \$\endgroup\$ Sep 17, 2012 at 16:07
  • \$\begingroup\$ Download the interpreter (a Ruby script) from this site and run it e.g. like this: echo 9 | ruby golfscript.rb program.gs, where program.gs contains the code above. \$\endgroup\$ Sep 17, 2012 at 16:16
  • \$\begingroup\$ You can also check out the GolfScript Interpreter too. \$\endgroup\$
    – anon
    Sep 17, 2012 at 20:38
  • \$\begingroup\$ @Mike: ...but if you do, you'll need to hardcode the input into the program (e.g. by prepending ;"INPUT GOES HERE" to the code), since that interpreter doesn't seem to provide a separate field for input. \$\endgroup\$ Sep 17, 2012 at 20:47
  • \$\begingroup\$ @IlmariKaronen, I still don't get it, the echo 9 | ruby golfscript.rb program.gs thing... =( \$\endgroup\$ Sep 17, 2012 at 22:28

Python, 37




Perl 96 chars (obsolete solution since the new rules)

For what I can understand from the rules, we can assume that X is predefined. Then this solution should work :


which is 96 characters long, although the algorithm is very naive.

It gives the following solutions :


Thus, it seems obvious than there is room to a much simpler program which directly computes a solution of this form...

The following program outputs all valid sequences with any given length X (takes X in STDIN)


If you want to output ALL possible solutions (for any X), then the program becomes (102 chars) :

  • \$\begingroup\$ You updated your solution, but it doesn't follow all the rules. (Your old solution was posted before the rule change) specifically All digits between 1 to x must be in the sequence \$\endgroup\$
    – Matt
    Sep 18, 2012 at 11:30
  • \$\begingroup\$ Hey, you're right. This is a huge change ! How are we supposed to play on a moving ground ? ^^ Ok, I'll update my solution if I get some time... \$\endgroup\$
    – Orabîg
    Sep 18, 2012 at 17:29
  • \$\begingroup\$ Yeah I'm sorry for not pointing this rule, actually I thought that by just mentioning some examples it would be visible, my fault. \$\endgroup\$ Sep 18, 2012 at 18:01


' 3579'[:(1+x)/2]+'1468'[:x/2]+'2' 



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