Programing Puzzles

Wyverex wyverex.cypher at gmail.com
Wed Aug 6 15:00:09 PDT 2008


Wyverex wrote:
> just some fun little programming puzzles I found around online...
> 
> 
> 
> Write a "Hello World" program in 'C' without using a semicolon.
> (Note: #include in C doesn't need a semicolon but import does)
> 
> 
> 
> Problem #1 Write a "Hello World" program in D with only a semicolon on 
> import statement.
> 
> Problem #2 Test if an int is even or odd without looping or if statement 
> (Cant use: do, while, for, foreach, if).
> 
> Problem #3 Write a program without using any loop (if, for, while etc) 
> to print numbers from 1 to 100 and 100 to 1;
> 
> Problem #4 Find if the given number is a power of 2.
> 
> 
> 
> Post Solutions to this root, comments to someones solution in that thread.

Solutions, Don't Peek if you havn't tried them yet!


Problem #1********************************************
import tango.io.Stdout;

void main()
{
   if(Stdout("Hello World")) {}
}


Problem #2*********************************************
import tango.io.Stdout;

void main()
{
   int x = 2;

   char[][2] ans = ["Even", "Odd"];
   Stdout( ans[ x & 1 ] ).newline;
}

Problem #3*******************************************
import tango.io.Stdout;

void countUP( int i )()
{
   Stdout(i)(" ");
   countUP!(i+1);
}

void countUP( int i : 101 )() {}

void countDN( int i )()
{
   Stdout(i)(" ");
   countDN!(i-1);
}

void countDN( int i : 0 )() {}

void main()
{
   countUP!(1);
   Stdout.newline;
   countDN!(100);
}

Problem#4******************************************
import tango.io.Stdout;

bool test( in uint i )
{
   int c = 0;
   while( i > 0 )
   {
     c += i & 1;
     i = i >> 1;
   }
   return ( c == 1? true : false );
}

void main()
{
    for(uint x = 0; x < uint.max ; ++x)
       if(test(x)) Stdout(x).newline;
}


More information about the Digitalmars-d-learn mailing list