Count from 1 to 1000 without using loops
-
Hey Guys, I read this article regarding " Count from 1 to 1000 without using loops " ============================================================================= #include #include void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } The only other method to count 1 to 1000 is using recursion. According to C language, j has ‘1’as its value at the beginning. When 1 <= j < 1000, &main + (&exit - &main)*(j/1000) always evaluated to &main, which is the memory address of main. (&main)(j+1) is the next iteration we want to get, which would print ‘2’ on the screen, etc. The stop condition of this recursion is that When j hits 1000, &main + (&exit - &main)*(j/1000) evaluates to &exit, which will elegantly exit this process, and has the error code 1001 returned to the operating system. ============================================================================= I tried to run this code and its perfectly working, but I am not getting how its works. How it calculates the end condition and all. Please help me out. Regards, Amrit
-
Hey Guys, I read this article regarding " Count from 1 to 1000 without using loops " ============================================================================= #include #include void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } The only other method to count 1 to 1000 is using recursion. According to C language, j has ‘1’as its value at the beginning. When 1 <= j < 1000, &main + (&exit - &main)*(j/1000) always evaluated to &main, which is the memory address of main. (&main)(j+1) is the next iteration we want to get, which would print ‘2’ on the screen, etc. The stop condition of this recursion is that When j hits 1000, &main + (&exit - &main)*(j/1000) evaluates to &exit, which will elegantly exit this process, and has the error code 1001 returned to the operating system. ============================================================================= I tried to run this code and its perfectly working, but I am not getting how its works. How it calculates the end condition and all. Please help me out. Regards, Amrit
The line
(&main + (&exit - &main)*(j/1000))(j+1);
is evaluated each time thus:
&exit - &main
is the offset from the start ofmain
to the start ofexit
j/1000
will evaluate to zero for all vaules ofj
less than 1000 Multiplying those two values together gives zero ifj
is less than 1000. Add that to&main
(the address ofmain
) and you get the same address, somain
gets called with the parameter valuej+1
. This continues until the value ofj
reaches 1000 at which time:j/1000
results in the value 1 That is multiplied by the offset ofexit
which returns that offset. Add that value tomain
and the next call will go toexit
rather thanmain
. -
The line
(&main + (&exit - &main)*(j/1000))(j+1);
is evaluated each time thus:
&exit - &main
is the offset from the start ofmain
to the start ofexit
j/1000
will evaluate to zero for all vaules ofj
less than 1000 Multiplying those two values together gives zero ifj
is less than 1000. Add that to&main
(the address ofmain
) and you get the same address, somain
gets called with the parameter valuej+1
. This continues until the value ofj
reaches 1000 at which time:j/1000
results in the value 1 That is multiplied by the offset ofexit
which returns that offset. Add that value tomain
and the next call will go toexit
rather thanmain
. -
The line
(&main + (&exit - &main)*(j/1000))(j+1);
is evaluated each time thus:
&exit - &main
is the offset from the start ofmain
to the start ofexit
j/1000
will evaluate to zero for all vaules ofj
less than 1000 Multiplying those two values together gives zero ifj
is less than 1000. Add that to&main
(the address ofmain
) and you get the same address, somain
gets called with the parameter valuej+1
. This continues until the value ofj
reaches 1000 at which time:j/1000
results in the value 1 That is multiplied by the offset ofexit
which returns that offset. Add that value tomain
and the next call will go toexit
rather thanmain
. -
Very interesting! I've never seen this code. If I wanna plus all numbers between 1 to 1000 with out loops, how can I do it like above method? Thank you for you advice!