The Shortest (again!) [Problem of the Week]

Once that a blogger says “i am too busy to post and i will start posting regularly once i am free” he never actually becomes free and never it happens that you see a post in his blog again..

so i wont say that .. but since i am busy a bit .. i will leave you with a strange problem this time.. famous among hackers.. its about..

The Shortest Program That Can Print It Self

..ignoring header files..

so lets say you write this program

#include <stdio.h>void main(){}

your program must print to the console this

void main(){} press any key to exit..
 
and off course .. any other letter you add to the code (e.g. the printing code inside the main) must be printed also along with the code
 
Rules and Hints:
  1. the code may be in C or C++
  2. you may consider using either printf or cout to print the program any thing is fine, printf is recommended.
  3. if you can also print the #include lines.. (honestly I couldn’t).. then you are the man.
  4. “almost right” answers might be accepted as long as it RUNS and at least prints 95% of the program!
  5. Anonymous answers wont be approved, at least provide a nick name and don’t use m8 cause its mine!
  6. if you still don’t get the problem, try writing a program that prints “int main(){}” and then look at your code .. and then try to add what you added to the printed “int main(){}” and you will get the problem.
    in other words .. i need to copy the console output and place it in a cpp and run it and then take its output place it in a cpp and .. bla bla
Advertisements

22 thoughts on “The Shortest (again!) [Problem of the Week]

  1. We have a first hacker

    First Place

    Mohamed Samy

    and he included the #include line in the printing (Y) πŸ˜€

    guys you can use this site “pointed by Mohamed Samy” to get a link for your code, because wordpress destroys the formatting
    http://rafb.net/paste/

  2. To Mohamed Abd El-Mon’em (Harry Potter)
    first:
    replace while(str) cout..*str++;
    with cout..str; only .. it will work then you wont need the tmp var
    second:
    try reading about Printf because cout is not that cool
    if u use printf you will be able to use the same trick you used with the other characters πŸ˜‰

  3. To Abdalla (Jaqoup)
    well, that was new πŸ˜€ .. very good solution in deed πŸ˜€ .. i would have never thought about it that way πŸ˜€

    First Place

    Mohamed Samy
    Mohamed Abdelghani
    Jaqoup

  4. To Mohamed Abd El-Mon’em (Harry Potter)
    Man, that is very very good (Y)
    thanks

    First Place

    Mohamed Samy
    Mohamed Abdelghani
    Mohamed Abd El-Mon’em (Harry Potter)
    Jaqoup

  5. mesh yenfa3 deh ??
    #define STR(str) cout<<#str

    we a7ot kol el code fe STR ……ana faker eny shoft el so2al dah fe site 2abl keda bas mesh kont fehemt el 7al

  6. #include
    using namespace std;

    #define STR(str)\
    cout <<“int main(){\n” << #str<<“\n}”;\
    str
    int main()
    {
    STR(
    cout << “this is koko program”;
    return 0;)
    }

  7. To Tasniem
    if you can .. then it would be great .. but we can always write the code in one line πŸ˜€ so it matches the output .. can’t we ? πŸ˜€
    ..
    waiting for your solution ..

  8. we have other three winners πŸ˜€
    AlaaShaker (wait till you see his solution) πŸ˜€
    Hatem Abdelghani
    and a 97% right solution form Tecnoyotta which actually is the first to use this method πŸ˜€

    thanks guys

  9. Pingback: The Shortest.. Solved Again [Problem of the Week] « more about the 8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s