It's time for awesome classic golf!

So, we have a function in C # or a similar language:

void Print(int min, int max) { if (min > max) throw new ArgumentException(); checked // бросаем исключение при переполнении { for (int i = min /*----------------*/) Console.WriteLine(i); } } 

It is necessary to replace the comment /*----------------*/ with a single-line code so that as a result of the execution of this function, all numbers from min to max inclusive are output to the console. More can not be changed!

Wins the shortest decision on the number of characters added . It is allowed to use not only C #, but a for loop must be present in the language (in this case, replace the code with the same one). The function should work without errors.

Yes, solutions based on overflows are prohibited (using checked ) so that there is not much difference between C # and C ++.

Notice special cases like min == int.Min , max == int.Max , min == max and the like. We can assume that min <= max when calling.


Final result:

The @kmv, @pavel and @Zealint solutions are correct, and they essentially implement one idea. The minimum size is 17 characters.

The second solution @kmv is longer (about 50 characters), but with a completely crazy (in a positive sense) idea.

An excellent non-standard first @kodv solution took 38 characters, the second solution of the same author is similar to the @Qwertiy solution, at least 30 characters.

Another solution @Qwertiy more traditional is also unusual, currently 26 characters.

The decisions of the other participants so far do not seem to fit the condition.


Unfortunately, due to the insufficiently unambiguous formulation of the condition, the @jfs decision on Python caused a discussion, and in the end was withdrawn by the author. Error in the condition completely on my conscience, I apologize.

  • one
    and the procedure for issuing numbers is important? - Grundy
  • @Grundy Yes important, in conditions: "all numbers from min to max inclusive" - Cerbo
  • one
    @Grundy Such subtleties are not indicated, so I think it should be understood as written. However, in your answer, you can bend a little in this direction if you show something interesting. - Cerbo
  • one
    It’s a pity that you don’t use functional languages, something like for i in min .. max -> printfn "%d" i - in general cheating :) - kmv
  • one
    @Zealint, what's the difference? if on the basis of one answer another good result turned out, that’s good too - Grundy

9 answers 9

 for (int i=min,d=~i&1;(i^d)&1; i+=i<max, d^=1) 

long, but so far the first thing that came to mind :)

looked answer @kmv

 for (int i = min,d=1;d;i+=d=i<max) 

already seems like something :)

C ++, 17 characters.

An option for perverts, but someone can make it more compact.

 for (int i=min;;i<max?i++:(exit(0),0)) 
  • one
    here you are a hardcore;) - Vasily Barbashev
  • one
    Is it C ++? If yes, please indicate. And you can still number of characters, eh? Golf after all. - VladD
  • The second option for Sharp will look something like this: for (int i = min,d=1;d!=0;i+=d=i<max?1:0) - αλεχολυτ
  • alexolut! = replace with> and yes. But essentially the same at @kmv in the last answer. - pavel
  • one
    1. ?1: => || ? 2. I would like to somehow reorganize the conditional operator so that exit can do without brackets. You can swap branches - then the brackets are not necessary, but < will turn into >= - as a result, the same 1 character. PS: Experimentally did not check, but it should work out. - Qwertiy

C #, 23 characters without spaces, will work on C / C ++ / Java.

 for (int i = min, j = 1; j > 0; i += (j = i < max ? 1 : 0)) 

 for (int i = min, j = 1; j > 0; i += j = i < max ? 1 : 0) 
  • The brackets can be removed, there will be 23 characters - Pavel Mayorov
  • On the pros ? 1 : 0 ? 1 : 0 can be removed. But in Sharpe and Java - no. - Qwertiy

For C ++, you need to insert such code ,min=1;min;min=(i<max),i+=min (starts with a comma, this is not a typo).

That is, my cycle looks like this:

 void Print(int min, int max) { for (int i = min,min=1;min;min=(i<max),i+=min) fprintf (stderr, "%i ", i); } 

29 characters added, and I basically do not want to add a new variable, reducing the size.

  • worth adding all the code in the collection - Grundy
  • internal brackets can be thrown out - VladD
  • one
    And you added a new variable. min inside the loop is a new variable. - VladD
  • Here the whole thing is this: I found the idea and showed it, but other participants have already thought of how to make it shorter. Therefore, I see no reason to refine. Need something else to invent. Yes, I don’t know the standard of the language in this place ... - Zealint
  • In any case, with the same length, the one who post the first decision will win. Well this is a little sport. - VladD

You can still twist like this (C # only). You can reduce it, but the general idea is important:

 for (int i = min; ((Func<bool>)(() => { for (long j = min; j <= max; ++j) Console.WriteLine(j); return false; }))();) 
  • 2
    Audience Award from me :) - VladD

C ++, 34 characters

For C # is not suitable - you can not declare variables of the same name in nested areas.

 &0;!i++;)for(long i=min;i<=max;++i 

http://ideone.com/Mdn48E

 #include <iostream> using namespace std; template <typename typed> void print(typed min, typed max) { if (min > max) throw 0; for (typed i = min&0;!i++;)for(long i=min;i<=max;++i) cout << i << ' '; } int main() { print<signed char>(-3, 5); cout << '\n'; print<signed char>(-128, -128); cout << '\n'; print<signed char>(-128, 127); cout << '\n'; print<signed char>(127, 127); cout << '\n'; return 0; } 

    As a new idea, because it is very long (38 characters):

     for(int i=min;i<max;cout<<i++<<endl);int i=max;if(1) cout<<i<<endl; 

    UPD: As a development of the last idea @Qwertiy (30 characters)

     for(int i=min;0;);for(long i=min;i<=max;++i) cout<<i<<endl; 
    • Suddenly, yes. :) - VladD
    • @VladD, it was necessary to do the body of the cycle long, so that I did not want to copy-paste it. And so - yes, beautiful. - Qwertiy
     for (int i = min; i <= max; i++) for (int i = min-1; i++ < max;) for (int i = min; min <= max; i = ++min) 
    • In the second variant, min - 1 will throw an exception when min = int.MinValue - VladD
    • Yes, for sure, did not take into account - Ksenia
    • And in the first i++ will throw an exception at the last iteration if max = int.MaxValue . - VladD
    • Correct :-) - VladD
    • @VladD, it turned out only longer, but this time it seems to be safe? - Ksenia

    C ++, 30 26 characters

     ;i<=max;max&(i==max)?--max:++i /* 30 символов */ ;i<=max;i&i==max?--max:++i /* 26 символов */ 

    It works if int.min is even, and int.max is odd, which is done for built-in integer types :)

    http://ideone.com/F8FYHx
    http://ideone.com/Yougv2

     #include <iostream> using namespace std; template <typename typed> void print(typed min, typed max) { if (min > max) throw 0; for (typed i = min; i <= max; i&i==max ? --max : ++i) cout << (int)i << ' '; } int main() { print<signed char>(-3, 5); cout << '\n'; print<signed char>(-128, -128); cout << '\n'; print<signed char>(-128, 127); cout << '\n'; print<signed char>(127, 127); cout << '\n'; return 0; } 
    • You can save a couple of characters if you have a single-letter synonym for max ( for typed i = min, j = max; ). - VladD
    • And there is no overflow when subtracting - max? When is max = Int.Min? - pavel
    • @pavel: when max = int.min and i = int.min , then i==max gives 1, i&i==max gives int.min & 1 , that is, 0, int.min even. Therefore, in this case, ++i is executed, and not - --max . - VladD
    • @VladD thank you) the code is not immediately clear in the priority of operations is confused) - pavel
    • 2
      @pavel: Any golf solution is not very suitable for production. Ce la vie! - VladD

    Hmm, and if, offhand, such an option?

     for (int i = min; i % max != 0; i++) 
    • 2
      max = int.max => overflow. - Qwertiy
    • And in general, is wrong. min=-1, max=10 . - Qwertiy