7
4
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);
7
4
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);
23
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;}
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. – sundar - Reinstate Monica – 2013-08-17T09:50:43.637
1@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 :) – saeedn – 2013-08-19T03:36:31.460
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).1Another idea - maybe exploit the fact that printf
returns a higher value at 100? – ugoren – 2013-08-19T05:02:54.343
@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. :) – sundar - Reinstate Monica – 2013-08-19T18:29:54.780
@ugoren Thanks for suggestions. I tried them. The printf
return value idea was nice :) – saeedn – 2013-08-19T21:08:44.480
6^printf()&4
saves 2 chars. At 100, it changes b[2]
, which is a harmless overflow on my machine. P.S. Why don't you change the character count in the title? – ugoren – 2013-08-20T07:00:12.167
The net step is to get rid of a
. main
can do the work alone. – ugoren – 2013-08-20T07:27:45.457
@ugoren I worked on main
and updated the answer ;) – saeedn – 2013-08-20T17:40:01.423
Nice. I used -=4
, not -=7
, but I guess both work. I was going to say it's done, but I saw your \n
-> change, and decided not to give up. How about making
b
a parameter (and using &b[...]
)? – ugoren – 2013-08-20T18:10:25.343
@ugoren Yeah, that was good point. Unfortunately the parentheses around &b
were necessary. I think it can't be shorter than this, can it? – saeedn – 2013-08-20T18:33:12.920
It can be shorter. Try %2d
. I couldn't get rid of the parenthesis. I tried the x[y]
->y[x]
trick, but it stays the same length (but even weirder than now). – ugoren – 2013-08-21T07:08:14.213
@ugoren is there documentation somewhere for why that x[y] -> y[x]
trick works? I remember that it works, but I don't remember why, and I can't find what to search for. (I know that it's equivalent to something which is the same as something else, which is the same as y[x]
, but idr what those 2 middle ones are.) – mbomb007 – 2017-02-14T22:57:55.807
@mbomb007, both are equivalent to x[y]
= *(x+y)
= *(y+x)
= y[x]
. – ugoren – 2017-02-15T09:57:38.240
14
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.
4Using the preprocessor is almost like hard coding it. – Martin Thoma – 2013-01-01T16:52:09.847
agreed. This is hardwired. – Cong Hui – 2013-11-09T08:01:14.007
10
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>();}
2s/class/struct/;s/public://;s/static //;s/::/()./g
saves 11 characters. – ceased to turn counterclockwis – 2012-12-30T19:49:36.417
7
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++);)))}
?
is a conditional is it not? – Claudiu – 2013-01-10T14:40:58.350
vignesh4303 disallowed "if conditions", not all conditionals, which is why I included both answers. – cardboard_box – 2013-01-11T02:24:58.410
3
1..100
Equally doable, though no shorter, with 1..1e2
. – Iszi – 2013-11-16T16:21:38.300
3
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;}
3
#include <cstdio>
template<int i>void p(){printf("%d ",i);p<i+1>();}
template<>void p<101>(){}
int main(){p<1>();}
Isn't this recursion? – Bryan Chen – 2013-09-18T05:47:28.830
3
1:100
7Doesn't the question want a code in C? – saeedn – 2013-08-15T21:40:17.360
2
>>> 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]
2
print(list(range(1,101)))
it is a inbuilt function that uses recursion! – Pranit Bauva – 2013-01-05T14:21:16.677
5I 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. – Martin Thoma – 2013-01-05T14:46:42.687
9The garbage collector uses recursion! So does the kernel of your OS. So, computers are not allowed. – boothby – 2013-08-15T17:20:52.637
1
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)
2p *(1..100)
saves 3 characters. – histocrat – 2013-01-05T00:05:37.923
@histocrat Thanks, I updated my answer. – knut – 2013-01-05T00:08:07.117
Can't you remove the brackets too? – dingledooper – 2019-10-01T04:17:33.867
0
p=lambda d:print d+1,;map(p,range(100))
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)))
0
echo {1..100}
Some more text so it posts.
0
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 alert
s the numbers within the array.
0
Scala (22)
1 to 100 foreach print
@downvoter care to comment ? – lhk – 2015-12-22T10:51:20.233
0
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
0
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.
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? – sundar - Reinstate Monica – 2013-08-20T18:49:37.783
1
@sundar see the "Special Backtracking Control Verbs" section in perlre :)
– memowe – 2013-08-22T00:39:03.2070
alert|[1 100].fill
-1
IntStream.range(1,101).forEach(System.out::println);
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. – Post Rock Garf Hunter – 2017-02-14T21:42:05.703
It's a stream function. It performs an operation on each element of the stream. It's not a loop. – aglassman – 2017-02-15T21:22:01.317
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. – Post Rock Garf Hunter – 2017-02-15T21:25:44.577
This challenge restricts to C/C++, so Java is probably unacceptable here. – Erik the Outgolfer – 2017-05-15T16:59:57.030
-1
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]
That's stretching the "no hardcoding" pretty hard. – ugoren – 2013-08-16T07:02:30.827
print "1 to 100"
orprint 1 2 3 4 5 6 (etc)
? – beary605 – 2012-12-27T07:19:17.440@beary605 print 1 2 3..100 – BlueBerry - Vignesh4303 – 2012-12-27T07:21:12.047
4Does
goto
count? – Ry- – 2012-12-31T00:50:11.423Do preprocessor conditions count? – ApproachingDarknessFish – 2014-01-16T21:50:03.377