8
\$\begingroup\$

can we print 1 to 100 without using any if conditions and loops in c&c++?

Conditon: main point is you must not use recursion...and doesnt hardcode code in it for e.g

print(1 2 3..etc);

\$\endgroup\$
4
  • \$\begingroup\$ print "1 to 100" or print 1 2 3 4 5 6 (etc)? \$\endgroup\$
    – beary605
    Dec 27 '12 at 7:19
  • \$\begingroup\$ @beary605 print 1 2 3..100 \$\endgroup\$ Dec 27 '12 at 7:21
  • 4
    \$\begingroup\$ Does goto count? \$\endgroup\$
    – Ry-
    Dec 31 '12 at 0:50
  • \$\begingroup\$ Do preprocessor conditions count? \$\endgroup\$ Jan 16 '14 at 21:50

21 Answers 21

23
\$\begingroup\$

C (90) (79) (59) (47) (42) (40)

static int

x=1;a(){char b[8];printf("%d\n",x++);b[24]-=5*(1-x/101);}main(){a();return 0;}

The function a which prints the numbers does not call itself! I exploited a buffer overflow and changed the return address to make the program counter go over function a again as long as I need.

I don't know if this is considered to be a recursion, but I thought it would worth trying. This code works on my 64-bit machines with gcc 4.6, for other platforms the last statement of function a, could be a little different.

Exp1: I allocated a dummy buffer on stack b, and then addressed a passed-by-end location, which is the location of return address. I anticipated the distance between start of buffer and return address location from disassembly of function a.

Exp2: Expression 5*(1-x/101), is 5 for all x<=100 and 0 for x=101. By looking at disassembly of main (in my case), if you decrease the return address by 5, you will set the PC to calling point of a again. In the updated codes, the return value of printf is used for checking loop condition.

Update: After applying ugoren's suggestions and some other changes:

x;a(){int b[2];b[3*(printf("%d\n",++x)&2)]-=5;}main(){a();}

Update2: After Removing function a:

x;main(){int b[2];b[6^printf("%d ",++x)&4]-=7;}

Update3:

x;main(b){(&b)[1|printf("%d ",++x)&2]-=7;}

Update4: Thanks to mbz :)

x;main(b){(&b)[3|printf("%d ",++x)]-=7;}
\$\endgroup\$
14
  • \$\begingroup\$ Perhaps the most interesting answer here! Could you explain a bit about how it's supposed to work? How did you come with b[24] for the location of the PC and how can I determine what I should use instead (32-bit cygwin, gcc 4.7.3)? What is the rationale behind the magical expression 5*(1-x/101)? Also, is the static part of x's declaration needed? I'm not fully clear on how this works but since we aren't explicitly returning from a() wouldn't even an auto variable x remain in memory? And in this case it's even a global already. Thanks again for enlivening this dull repetitive thread. \$\endgroup\$ Aug 17 '13 at 9:50
  • 1
    \$\begingroup\$ @sundar Thank you for pointing out static. You're right, It was necessary for my previous code and I forgot to remove it. I added some small explanations, please check them out in answer :) \$\endgroup\$
    – saeedn
    Aug 19 '13 at 3:36
  • 1
    \$\begingroup\$ 1. int b[2] would be shorter. 2. return 0; isn't needed. 3. Changing the index on b instead of the subtracted value would be shorter. (of course, every such change could change machine code and break the solution, but I think it should be OK). \$\endgroup\$
    – ugoren
    Aug 19 '13 at 5:01
  • 1
    \$\begingroup\$ Another idea - maybe exploit the fact that printf returns a higher value at 100? \$\endgroup\$
    – ugoren
    Aug 19 '13 at 5:02
  • \$\begingroup\$ @saeedn Thanks for the explanations, it's very clear now. I felt stupid after reading Exp2 though, I'd been trying to decode it as some overcomplicated int-to-float bit pattern thing like the famous "inverse square root" formula, missed this much simpler explanation. :) \$\endgroup\$ Aug 19 '13 at 18:29
14
\$\begingroup\$

85

C (gcc)

#define c printf("%d ",i++);
#define b c c c c c
#define a b b b b b
main(i){a a a a}

Assuming no command line arguments were passed.

\$\endgroup\$
2
  • 4
    \$\begingroup\$ Using the preprocessor is almost like hard coding it. \$\endgroup\$ Jan 1 '13 at 16:52
  • \$\begingroup\$ agreed. This is hardwired. \$\endgroup\$
    – Cong Hui
    Nov 9 '13 at 8:01
10
\$\begingroup\$

C++ (159 136)

With templates.

#include<cstdio>
#define Z(A,B,C,D)template<A>struct P B{P(){C;printf("%d ",D);}};
Z(int N,,P<N-1>(),N)Z(,<1>,0,1)int main(){P<100>();}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ s/class/struct/;s/public://;s/static //;s/::/()./g saves 11 characters. \$\endgroup\$ Dec 30 '12 at 19:49
7
\$\begingroup\$

C 71 70

Assuming the ? operator is allowed.

#define f(a)a a a a a
int main(i){f(f(f(printf(i<102?"%d ":0,i++);)))}

Edit: ""->0

If ? is too similar to an if statement, then use this instead (78)

#define f(a)a a a a
#define g(a)f(a)a
int main(i){f(g(g(printf("%d ",i++);)))}
\$\endgroup\$
2
  • \$\begingroup\$ ? is a conditional is it not? \$\endgroup\$
    – Claudiu
    Jan 10 '13 at 14:40
  • \$\begingroup\$ vignesh4303 disallowed "if conditions", not all conditionals, which is why I included both answers. \$\endgroup\$ Jan 11 '13 at 2:24
3
\$\begingroup\$

267

this is the best I can think of, assuming using the preprocessor is fine.

#include <stdio.h>
#define a(i)i,i+1,i+2,i+3
#define b(i)a(i),a(i+4),a(i+8),a(i+12)
#define c(i)b(i),b(i+16)
#define e c(1),c(33)
#define f %d %d %d %d
#define g f f f f f f f f
#define r(m) #m
#define s(m) r(m)
int main(){printf(s(g g g f),e,c(65),a(97));return 0;}
\$\endgroup\$
3
\$\begingroup\$

C++ (115)

#include <cstdio>
template<int i>void p(){printf("%d ",i);p<i+1>();}
template<>void p<101>(){}
int main(){p<1>();}
\$\endgroup\$
1
  • \$\begingroup\$ Isn't this recursion? \$\endgroup\$
    – Bryan Chen
    Sep 18 '13 at 5:47
3
\$\begingroup\$

MATLAB/Octave, (5 chars)

1:100
\$\endgroup\$
1
  • 7
    \$\begingroup\$ Doesn't the question want a code in C? \$\endgroup\$
    – saeedn
    Aug 15 '13 at 21:40
3
\$\begingroup\$

PowerShell (6 characters)

1..100

\$\endgroup\$
1
  • \$\begingroup\$ Equally doable, though no shorter, with 1..1e2. \$\endgroup\$
    – Iszi
    Nov 16 '13 at 16:21
2
\$\begingroup\$

Python 3 (25)

print(list(range(1,101)))
\$\endgroup\$
3
  • \$\begingroup\$ it is a inbuilt function that uses recursion! \$\endgroup\$ Jan 5 '13 at 14:21
  • 5
    \$\begingroup\$ I guess it depends on the interpreter if range uses recursion. I don't use recursion in my solution. I don't care if the inbuilt functions use recursion. If you want to take a look at every possible interpreter / compiler, you can't answer this question with anything else than assembly. \$\endgroup\$ Jan 5 '13 at 14:46
  • 9
    \$\begingroup\$ The garbage collector uses recursion! So does the kernel of your OS. So, computers are not allowed. \$\endgroup\$
    – boothby
    Aug 15 '13 at 17:20
2
\$\begingroup\$

Python 2 (12)

>>> range(1,101)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
\$\endgroup\$
1
\$\begingroup\$

Ruby (11) [non-competitive]

p *(1..100)

(Thanks to histocrat)

Previous 14-character solution:

p *1.upto(100)

This is a non-competitive answer (not C/C++ as requested)

\$\endgroup\$
3
  • 2
    \$\begingroup\$ p *(1..100) saves 3 characters. \$\endgroup\$
    – histocrat
    Jan 5 '13 at 0:05
  • \$\begingroup\$ @histocrat Thanks, I updated my answer. \$\endgroup\$
    – knut
    Jan 5 '13 at 0:08
  • \$\begingroup\$ Can't you remove the brackets too? \$\endgroup\$ Oct 1 '19 at 4:17
0
\$\begingroup\$

Scala (22)

1 to 100 foreach print
\$\endgroup\$
1
  • \$\begingroup\$ @downvoter care to comment ? \$\endgroup\$
    – lhk
    Dec 22 '15 at 10:51
0
\$\begingroup\$

Perl 6 (10)

print ^101

If one wants to be able to read the numbers and therefore spaces between the numbers would be nice, the following will do the trick.

say ~ ^101
\$\endgroup\$
0
\$\begingroup\$

Perl (65)

Here's a non-trivial Perl approach (not like say for 1 .. 100). The program uses perl's regular expression engine to count its own characters (two times), and that's the reason why I couldn't golfify the content of $k. It tries to find a (minimal, non-greedy) group of arbitrary single characters until (*FAIL).

$k='$k =~ /^(.+?)(?{print length($1) . "\n"})(*FAIL)/#'x2;eval$k;

The output is a list of all integers from 0 to 100 with newlines.

\$\endgroup\$
2
  • \$\begingroup\$ Clever solution on many levels, nice. I don't understand the *FAIL though, is it some special string to the regex engine? If not, won't the * have its usual metacharacter meaning here and mess things up? \$\endgroup\$ Aug 20 '13 at 18:49
  • 1
    \$\begingroup\$ @sundar see the "Special Backtracking Control Verbs" section in perlre :) \$\endgroup\$
    – memowe
    Aug 22 '13 at 0:39
0
\$\begingroup\$

Pygmy, (18 characters)

alert|[1 100].fill
\$\endgroup\$
0
\$\begingroup\$

Python 2 (39 chars)

p=lambda d:print d+1,;map(p,range(100))

Python 3 (53 chars)

p=lambda d:print(d+1,end=' ');set(map(p,range(100)))

or print numbers on separate lines (28 char)

set(map(print,range(1,101)))

or (tested in Python 3.3.2, set items printing in ascending order - 24 chars)

print(set(range(1,101)))
\$\endgroup\$
0
\$\begingroup\$

Bash 13

echo {1..100}

Some more text so it posts.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 38 bytes

alert([...Array(101).keys()].slice(1))

Creates an array of length 101, uses the spread operator to put the array keys (0 to 100) into an array, removes the first element (0) then alerts the numbers within the array.

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

Python - 61

Looping? No this is list comprehension.

d=[0,1,2,3,4,5,6,7,8,9];print[x*10+y+1 for x in d for y in d]
\$\endgroup\$
1
  • \$\begingroup\$ That's stretching the "no hardcoding" pretty hard. \$\endgroup\$
    – ugoren
    Aug 16 '13 at 7:02
-1
\$\begingroup\$

Hassium, 31 Bytes

func main()print(range(1, 100))

Run online and see expanded here

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

JAVA, 52 chars

IntStream.range(1,101).forEach(System.out::println);
\$\endgroup\$
4
  • \$\begingroup\$ Welcome to the site. I am not familiar with java but forEach looks an awful lot like a loop which seems to be forbidden by the challenge. \$\endgroup\$
    – Grain Ghost
    Feb 14 '17 at 21:42
  • \$\begingroup\$ It's a stream function. It performs an operation on each element of the stream. It's not a loop. \$\endgroup\$
    – aglassman
    Feb 15 '17 at 21:22
  • \$\begingroup\$ I would disagree that a stream function is distinct from a loop but I don't think this argument is worthwhile. This question has been closed as unclear for the very reason that "loop" is not very well defined. \$\endgroup\$
    – Grain Ghost
    Feb 15 '17 at 21:25
  • \$\begingroup\$ This challenge restricts to C/C++, so Java is probably unacceptable here. \$\endgroup\$ May 15 '17 at 16:59

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